-
Notifications
You must be signed in to change notification settings - Fork 3
/
flowpipe_require.go
47 lines (41 loc) · 1.26 KB
/
flowpipe_require.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
package modconfig
import (
"fmt"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/pipe-fittings/hclhelpers"
"github.com/turbot/pipe-fittings/schema"
)
type FlowpipeRequire struct {
MinVersionString string `hcl:"min_version,optional"`
Constraint *semver.Constraints
DeclRange hcl.Range
}
func (r *FlowpipeRequire) initialise(requireBlock *hcl.Block) hcl.Diagnostics {
// find the steampipe block
flowpipeBlock := hclhelpers.FindFirstChildBlock(requireBlock, schema.BlockTypeSteampipe)
if flowpipeBlock == nil {
// can happen if there is a legacy property - just use the parent block
flowpipeBlock = requireBlock
}
// set DeclRange
r.DeclRange = hclhelpers.BlockRange(flowpipeBlock)
if r.MinVersionString == "" {
return nil
}
// convert min version into constraint (including prereleases)
minVersion, err := semver.NewVersion(strings.TrimPrefix(r.MinVersionString, "v"))
if err == nil {
r.Constraint, err = semver.NewConstraint(fmt.Sprintf(">=%s-0", minVersion))
}
if err != nil {
return hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("invalid required flowpipe version %s", r.MinVersionString),
Subject: &r.DeclRange,
}}
}
return nil
}