diff --git a/main.go b/main.go index 0bf2648..3d111c2 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "encoding/binary" "encoding/gob" "encoding/json" + "errors" "flag" "fmt" "go/ast" @@ -235,7 +236,7 @@ func main1() int { return 0 } -func goVersionOK() bool { +func checkGoVersion() error { const ( minGoVersion = "v1.15.0" supportedGoVersions = "1.15.x" @@ -247,13 +248,12 @@ func goVersionOK() bool { out, err := exec.Command("go", "version").CombinedOutput() if err != nil { - fmt.Printf(`Can't get go version: %v + return fmt.Errorf(`Can't get Go version: %v This is likely due to go not being installed/setup correctly. -How to install go: https://golang.org/doc/install +How to install Go: https://golang.org/doc/install `, err) - return false } rawVersion := string(bytes.TrimPrefix(bytes.TrimSpace(out), []byte("go version "))) @@ -263,29 +263,31 @@ How to install go: https://golang.org/doc/install if tag == "devel" { commitAndDate := rawVersion[tagIdx+1:] // Remove commit hash and architecture from version - date := commitAndDate[strings.IndexByte(commitAndDate, ' ')+1 : strings.LastIndexByte(commitAndDate, ' ')] + startDateIdx := strings.IndexByte(commitAndDate, ' ') + 1 + endDateIdx := strings.LastIndexByte(commitAndDate, ' ') + if endDateIdx <= 0 { + return errors.New("Can't recognize devel build timestamp") + } + date := commitAndDate[startDateIdx:endDateIdx] versionDate, err := time.Parse(gitTimeFormat, date) if err != nil { - fmt.Printf("Can't recognize devel build timestamp: %v\n", err) - return true + return fmt.Errorf("Can't recognize devel build timestamp: %v", err) } if versionDate.After(minGoVersionDate) { - return true + return nil } - fmt.Printf("You use the old unstable %q Go version, please upgrade Go to %s\n", rawVersion, supportedGoVersions) - return false + return fmt.Errorf("You use the old unstable \"%s\" Go version, please upgrade Go to %s", rawVersion, supportedGoVersions) } version := "v" + strings.TrimPrefix(tag, "go") if semver.Compare(version, minGoVersion) < 0 { - fmt.Printf("Outdated Go version %q is used, please upgrade Go to %s\n", version, supportedGoVersions) - return false + return fmt.Errorf("Outdated Go version %s is used, please upgrade Go to %s", version, supportedGoVersions) } - return true + return nil } func mainErr(args []string) error { @@ -294,8 +296,8 @@ func mainErr(args []string) error { case "help": flagSet.Usage() case "build", "test": - if !goVersionOK() { - return nil + if err := checkGoVersion(); err != nil { + return err } // Split the flags from the package arguments, since we'll need // to run 'go list' on the same set of packages. diff --git a/testdata/scripts/goversion.txt b/testdata/scripts/goversion.txt new file mode 100644 index 0000000..cf8cf91 --- /dev/null +++ b/testdata/scripts/goversion.txt @@ -0,0 +1,48 @@ +[!exec:sh] skip + +cp go .bin/go +chmod 777 .bin/go + +# Check incorrect go install +env GO_VERSION='' +! garble build +stderr 'Can''t get Go version' + +[short] stop + +# Check invalid devel format +env GO_VERSION='go version devel someinvalidversion' +! garble build +stderr 'Can''t recognize devel build timestamp' + +# Check invalid devel date +env GO_VERSION='go version devel +afb5fca Sun Sep 99 99:99:99 9999 +0000 linux/amd64' +! garble build +stderr 'Can''t recognize devel build timestamp: parsing time' + +# Check invalid old devel version +env GO_VERSION='go version devel +afb5fca Sun Aug 07 00:00:00 2020 +0000 linux/amd64' +! garble build +stderr 'You use the old unstable' + +# Check invalid new devel version +env GO_VERSION='go version devel +afb5fca Sun Sep 13 07:54:42 2020 +0000 linux/amd64' +! garble build +! stderr 'You use the old unstable' + +# Check old version +env GO_VERSION='go version go1.14 windows/amd64' +! garble build +stderr 'Outdated Go version' + +# Check supported version +env GO_VERSION='go version go1.15.2 windows/amd64' +! garble build +! stderr 'Outdated Go version' + +-- main.go -- +package main +-- go -- +#!/bin/sh + +[ -z "$GO_VERSION" ] && exit 1 || echo "$GO_VERSION"