/
version.go
45 lines (37 loc) 路 1.24 KB
/
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
package goversion
import (
goVersion "github.com/hashicorp/go-version"
"github.com/pkg/errors"
"os/exec"
"strings"
)
var minGoVersion = goVersion.Must(goVersion.NewVersion("1.14.0"))
// CheckMinimumGoVersion checks to make sure we are running at least minGoVersion
func CheckMinimumGoVersion(gobin string) error {
cmd := exec.Command(gobin, "version")
stdout, err := cmd.Output()
if err != nil {
return errors.Wrap(err, "determining go version")
}
return checkMinimumGoVersion(string(stdout))
}
// checkMinimumGoVersion checks to make sure we are running at least go 1.14.0
// expected format of goVersionOutput: go version go<version> <os/arch>
func checkMinimumGoVersion(goVersionOutput string) error {
split := strings.Split(goVersionOutput, " ")
if len(split) <= 2 {
return errors.Errorf("unexpected format for go version output: \"%s\"", goVersionOutput)
}
version := strings.TrimSpace(split[2])
if strings.HasPrefix(version, "go") {
version = version[2:]
}
currVersion, err := goVersion.NewVersion(version)
if err != nil {
return errors.Wrap(err, "parsing go version")
}
if currVersion.LessThan(minGoVersion) {
return errors.Errorf("go version must be %s or higher (%s detected)", minGoVersion.String(), version)
}
return nil
}