]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/terraform/configs/version_constraint.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / configs / version_constraint.go
1 package configs
2
3 import (
4 "fmt"
5
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"
10 )
11
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
18 DeclRange hcl.Range
19 }
20
21 func decodeVersionConstraint(attr *hcl.Attribute) (VersionConstraint, hcl.Diagnostics) {
22 ret := VersionConstraint{
23 DeclRange: attr.Range,
24 }
25
26 val, diags := attr.Expr.Value(nil)
27 if diags.HasErrors() {
28 return ret, diags
29 }
30 var err error
31 val, err = convert.Convert(val, cty.String)
32 if err != nil {
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(),
38 })
39 return ret, diags
40 }
41
42 if val.IsNull() {
43 // A null version constraint is strange, but we'll just treat it
44 // like an empty constraint set.
45 return ret, diags
46 }
47
48 constraintStr := val.AsString()
49 constraints, err := version.NewConstraint(constraintStr)
50 if err != nil {
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(),
58 })
59 return ret, diags
60 }
61
62 ret.Required = constraints
63 return ret, diags
64 }