-
Notifications
You must be signed in to change notification settings - Fork 255
/
plugin_version.go
61 lines (52 loc) · 1.73 KB
/
plugin_version.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
package modconfig
import (
"fmt"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/pipe-fittings/hclhelpers"
"github.com/turbot/steampipe/pkg/ociinstaller"
)
type PluginVersion struct {
// the plugin name, as specified in the mod requires block. , e.g. turbot/mod1, aws
RawName string `cty:"name" hcl:"name,label"`
// the minumum version which satisfies the requirement
MinVersionString string `cty:"min_version" hcl:"min_version,optional"`
Constraint *semver.Constraints
// the org and name which are parsed from the raw name
Org string
Name string
DeclRange hcl.Range
}
func (p *PluginVersion) FullName() string {
if p.MinVersionString == "" {
return p.ShortName()
}
return fmt.Sprintf("%s@%s", p.ShortName(), p.MinVersionString)
}
func (p *PluginVersion) ShortName() string {
return fmt.Sprintf("%s/%s", p.Org, p.Name)
}
func (p *PluginVersion) String() string {
return fmt.Sprintf("plugin %s", p.FullName())
}
// Initialise parses the version and name properties
func (p *PluginVersion) Initialise(block *hcl.Block) hcl.Diagnostics {
var diags hcl.Diagnostics
p.DeclRange = hclhelpers.BlockRange(block)
// convert min version into constraint (including prereleases)
minVersion, err := semver.NewVersion(strings.TrimPrefix(p.MinVersionString, "v"))
if err == nil {
p.Constraint, err = semver.NewConstraint(fmt.Sprintf(">=%s-0", minVersion))
}
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Invalid plugin version %s", p.MinVersionString),
Subject: &p.DeclRange,
})
}
// parse plugin name
p.Org, p.Name, _ = ociinstaller.NewSteampipeImageRef(p.RawName).GetOrgNameAndConstraint()
return diags
}