/
diagnostics.go
70 lines (55 loc) 路 2.19 KB
/
diagnostics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package hcl2
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/pulumi/pulumi/pkg/codegen/hcl2/model"
)
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)
}
func unknownFunction(token string, tokenRange hcl.Range) *hcl.Diagnostic {
return errorf(tokenRange, "unknown function '%s'", token)
}
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)
}
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)
}