-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
84 lines (60 loc) · 1.83 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"regexp"
"strings"
"github.com/Masterminds/semver"
"github.com/google/go-github/v32/github"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"golang.org/x/net/context"
)
var repoNameRegex = regexp.MustCompile(`^terraform-provider-(.+)$`)
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "version",
Value: "*",
Usage: "Semver matcher for the versions to consider. Always the highest version matching will be used.",
},
},
Action: func(c *cli.Context) error {
if c.Args().Len() == 0 {
return errors.New("repository name must be specified")
}
repositoryName := c.Args().First()
version, err := semver.NewConstraint(c.String("version"))
if err != nil {
return errors.Wrapf(err, "while parsing version constraint %s", c.String("version"))
}
parts := strings.Split(repositoryName, "/")
if len(parts) != 2 {
return errors.New("repository must be in the format: owner/name")
}
owner := parts[0]
repo := parts[1]
match := repoNameRegex.FindStringSubmatch(repo)
if match == nil {
return errors.New("repository name must start with 'terraform-provider-' ")
}
pluginName := match[1]
gcl := github.NewClient(nil)
ctx := context.Background()
release, err := findLatestReleaseWithPluginAssets(ctx, gcl, owner, repo, version)
if err != nil {
return err
}
return release.generateShims("terraform.d", pluginName)
},
}
app.RunAndExitOnError()
}
// terraform-provider-linuxbox_v0.0.13_darwin_amd64.tar.gz
var assetNamePattern = regexp.MustCompile(`^terraform-provider-[^_]+_(v?[0-9]+\.[0-9]+\.[0-9]+)_([^_]+_[^_]+)(.tar.gz|.zip)$`)
func matchTerraformProviderAssetName(s string) (bool, string) {
r := assetNamePattern.FindStringSubmatch(s)
if r == nil {
return false, ""
}
return true, r[2]
}