2020-03-17 04:04:32 +01:00
|
|
|
package hcl2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2021-03-17 14:20:05 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
|
2020-03-17 04:04:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func errorf(subject hcl.Range, f string, args ...interface{}) *hcl.Diagnostic {
|
|
|
|
return diagf(hcl.DiagError, subject, f, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func diagf(severity hcl.DiagnosticSeverity, subject hcl.Range, f string, args ...interface{}) *hcl.Diagnostic {
|
|
|
|
message := fmt.Sprintf(f, args...)
|
|
|
|
return &hcl.Diagnostic{
|
|
|
|
Severity: severity,
|
|
|
|
Summary: message,
|
|
|
|
Detail: message,
|
|
|
|
Subject: &subject,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func labelsErrorf(block *hclsyntax.Block, f string, args ...interface{}) *hcl.Diagnostic {
|
|
|
|
startRange := block.LabelRanges[0]
|
|
|
|
|
|
|
|
diagRange := hcl.Range{
|
|
|
|
Filename: startRange.Filename,
|
|
|
|
Start: startRange.Start,
|
|
|
|
End: block.LabelRanges[len(block.LabelRanges)-1].End,
|
|
|
|
}
|
|
|
|
return errorf(diagRange, f, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func malformedToken(token string, sourceRange hcl.Range) *hcl.Diagnostic {
|
|
|
|
return errorf(sourceRange, "malformed token '%v': expected 'pkg:module:member'", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unknownPackage(pkg string, tokenRange hcl.Range) *hcl.Diagnostic {
|
|
|
|
return errorf(tokenRange, "unknown package '%s'", pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unknownResourceType(token string, tokenRange hcl.Range) *hcl.Diagnostic {
|
|
|
|
return errorf(tokenRange, "unknown resource type '%s'", token)
|
|
|
|
}
|
|
|
|
|
2020-04-07 04:43:16 +02:00
|
|
|
func unknownFunction(token string, tokenRange hcl.Range) *hcl.Diagnostic {
|
|
|
|
return errorf(tokenRange, "unknown function '%s'", token)
|
|
|
|
}
|
|
|
|
|
2020-03-17 04:04:32 +01:00
|
|
|
func unsupportedBlock(blockType string, typeRange hcl.Range) *hcl.Diagnostic {
|
|
|
|
return errorf(typeRange, "unsupported block of type '%v'", blockType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unsupportedAttribute(attrName string, nameRange hcl.Range) *hcl.Diagnostic {
|
|
|
|
return errorf(nameRange, "unsupported attribute '%v'", attrName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func missingRequiredAttribute(attrName string, missingRange hcl.Range) *hcl.Diagnostic {
|
|
|
|
return errorf(missingRange, "missing required attribute '%v'", attrName)
|
|
|
|
}
|
2020-04-07 04:43:16 +02:00
|
|
|
|
|
|
|
func tokenMustBeStringLiteral(tokenExpr model.Expression) *hcl.Diagnostic {
|
|
|
|
return errorf(tokenExpr.SyntaxNode().Range(), "invoke token must be a string literal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func duplicateBlock(blockType string, typeRange hcl.Range) *hcl.Diagnostic {
|
|
|
|
return errorf(typeRange, "duplicate block of type '%v'", blockType)
|
|
|
|
}
|