6 version "github.com/hashicorp/go-version"
7 "github.com/hashicorp/hcl2/hcl"
8 "github.com/zclconf/go-cty/cty"
9 "github.com/zclconf/go-cty/cty/convert"
12 // VersionConstraint represents a version constraint on some resource
13 // (e.g. Terraform Core, a provider, a module, ...) that carries with it
14 // a source range so that a helpful diagnostic can be printed in the event
15 // that a particular constraint does not match.
16 type VersionConstraint struct {
17 Required version.Constraints
21 func decodeVersionConstraint(attr *hcl.Attribute) (VersionConstraint, hcl.Diagnostics) {
22 ret := VersionConstraint{
23 DeclRange: attr.Range,
26 val, diags := attr.Expr.Value(nil)
27 if diags.HasErrors() {
31 val, err = convert.Convert(val, cty.String)
33 diags = append(diags, &hcl.Diagnostic{
34 Severity: hcl.DiagError,
35 Summary: "Invalid version constraint",
36 Detail: fmt.Sprintf("A string value is required for %s.", attr.Name),
37 Subject: attr.Expr.Range().Ptr(),
43 // A null version constraint is strange, but we'll just treat it
44 // like an empty constraint set.
48 constraintStr := val.AsString()
49 constraints, err := version.NewConstraint(constraintStr)
51 // NewConstraint doesn't return user-friendly errors, so we'll just
52 // ignore the provided error and produce our own generic one.
53 diags = append(diags, &hcl.Diagnostic{
54 Severity: hcl.DiagError,
55 Summary: "Invalid version constraint",
56 Detail: "This string does not use correct version constraint syntax.", // Not very actionable :(
57 Subject: attr.Expr.Range().Ptr(),
62 ret.Required = constraints