-
Notifications
You must be signed in to change notification settings - Fork 12
/
install.go
133 lines (113 loc) · 3.73 KB
/
install.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package tools
import (
"fmt"
"log"
"runtime"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/carolynvs/magex/mgx"
"github.com/carolynvs/magex/pkg"
"github.com/carolynvs/magex/pkg/archive"
"github.com/carolynvs/magex/pkg/downloads"
"github.com/carolynvs/magex/shx"
)
var (
must = shx.CommandBuilder{StopOnError: true}
// DefaultKindVersion is the default version of KinD that is installed when it's not present
DefaultKindVersion = "v0.12.0"
// DefaultStaticCheckVersion is the default version of StaticCheck that is installed when it's not present
DefaultStaticCheckVersion = "2022.1.2"
)
// Fail if the go version doesn't match the specified constraint
// Examples: >=1.16
func EnforceGoVersion(constraint string) {
log.Printf("Checking go version against constraint %s...", constraint)
value := strings.TrimPrefix(runtime.Version(), "go")
version, err := semver.NewVersion(value)
if err != nil {
mgx.Must(fmt.Errorf("could not parse go version: '%s': %w", value, err))
}
versionCheck, err := semver.NewConstraint(constraint)
if err != nil {
mgx.Must(fmt.Errorf("invalid semver constraint: '%s': %w", constraint, err))
}
ok, _ := versionCheck.Validate(version)
if !ok {
mgx.Must(fmt.Errorf("your version of Go, %s, does not meet the requirement %s", version, versionCheck))
}
}
// Install mage
func EnsureMage() error {
return pkg.EnsureMage("")
}
// Install gh
func EnsureGitHubClient() {
if ok, _ := pkg.IsCommandAvailable("gh", "--version", ""); ok {
return
}
// gh cli unfortunately uses a different archive schema depending on the OS
target := "gh_{{.VERSION}}_{{.GOOS}}_{{.GOARCH}}/bin/gh{{.EXT}}"
if runtime.GOOS == "windows" {
target = "bin/gh.exe"
}
opts := archive.DownloadArchiveOptions{
DownloadOptions: downloads.DownloadOptions{
UrlTemplate: "https://github.com/cli/cli/releases/download/v{{.VERSION}}/gh_{{.VERSION}}_{{.GOOS}}_{{.GOARCH}}{{.EXT}}",
Name: "gh",
Version: "1.8.1",
OsReplacement: map[string]string{
"darwin": "macOS",
},
},
ArchiveExtensions: map[string]string{
"linux": ".tar.gz",
"darwin": ".tar.gz",
"windows": ".zip",
},
TargetFileTemplate: target,
}
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
// github doesn't publish arm64 binaries for mac, so fallback to the amd64 binary
opts.UrlTemplate = "https://github.com/cli/cli/releases/download/v{{.VERSION}}/gh_{{.VERSION}}_{{.GOOS}}_amd64{{.EXT}}"
opts.TargetFileTemplate = "gh_{{.VERSION}}_{{.GOOS}}_amd64/bin/gh{{.EXT}}"
}
err := archive.DownloadToGopathBin(opts)
mgx.Must(err)
}
// Install kind
func EnsureKind() {
EnsureKindAt(DefaultKindVersion)
}
// Install kind at the specified version
func EnsureKindAt(version string) {
if ok, _ := pkg.IsCommandAvailable("kind", "--version", version); ok {
return
}
kindURL := "https://github.com/kubernetes-sigs/kind/releases/download/{{.VERSION}}/kind-{{.GOOS}}-{{.GOARCH}}"
mgx.Must(pkg.DownloadToGopathBin(kindURL, "kind", version))
}
// Install Staticcheck
func EnsureStaticCheck() {
EnsureStaticCheckAt(DefaultStaticCheckVersion)
}
// Install Staticcheck at the specified version
func EnsureStaticCheckAt(version string) {
if ok, _ := pkg.IsCommandAvailable("staticcheck", "--version", version); ok {
return
}
opts := archive.DownloadArchiveOptions{
DownloadOptions: downloads.DownloadOptions{
UrlTemplate: "https://github.com/dominikh/go-tools/releases/download/{{.VERSION}}/staticcheck_{{.GOOS}}_{{.GOARCH}}.tar.gz",
Name: "staticcheck",
Version: version,
},
ArchiveExtensions: map[string]string{
"linux": ".tar.gz",
"darwin": ".tar.gz",
"windows": ".tar.gz",
},
TargetFileTemplate: "staticcheck/staticcheck{{.EXT}}",
}
err := archive.DownloadToGopathBin(opts)
mgx.Must(err)
}