Skip to content

Instantly share code, notes, and snippets.

@sevein
Created February 4, 2021 07:03
Show Gist options
  • Save sevein/c7e3948f35d7f6f86f32dfddf136b37e to your computer and use it in GitHub Desktop.
Save sevein/c7e3948f35d7f6f86f32dfddf136b37e to your computer and use it in GitHub Desktop.
diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go
index fe6b70a..90083d6 100644
--- a/pkg/runner/runner.go
+++ b/pkg/runner/runner.go
@@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
+ "regexp"
"strings"
"github.com/Masterminds/semver"
@@ -26,16 +27,20 @@ type Runner struct {
goVersion *semver.Version
}
+var versionRegexp = regexp.MustCompile(`go?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?`)
+
+// parseGoVersion ignores pre-release identifiers immediately following the
+// patch version since we don't expect goVersionOutput to be SemVer-compliant.
func parseGoVersion(goVersionOutput string) (*semver.Version, error) {
el := strings.Fields(strings.TrimRight(goVersionOutput, "\n"))
if len(el) < 2 {
return nil, errors.Errorf("unexpected go version output; expected 'go version go<semver> ...; found %v", strings.TrimRight(goVersionOutput, "\n"))
}
- goVersion, err := semver.NewVersion(strings.TrimPrefix(el[2], "go"))
- if err != nil {
- return nil, err
+ goVersion := versionRegexp.FindString(el[2])
+ if goVersion == "" {
+ return nil, errors.New("unexpected go version format")
}
- return goVersion, nil
+ return semver.NewVersion(strings.TrimPrefix(goVersion, "go"))
}
func isSupportedVersion(v *semver.Version) error {
@@ -59,7 +64,7 @@ func NewRunner(ctx context.Context, insecure bool, goCmd string) (*Runner, error
goVersion, err := parseGoVersion(output.String())
if err != nil {
- return nil, err
+ return nil, errors.Wrap(err, "failed to parse go version")
}
r.goVersion = goVersion
@@ -215,5 +220,4 @@ func (r *runnable) Build(pkg, out string) error {
fmt.Println(trimmed)
}
return nil
-
}
diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go
index f749ed5..254f25f 100644
--- a/pkg/runner/runner_test.go
+++ b/pkg/runner/runner_test.go
@@ -20,6 +20,7 @@ func TestParseAndIsSupportedVersion(t *testing.T) {
{output: "go version go1.1 linux/amd64", errs: errors.New("found unsupported go version: 1.1.0; requires go 1.14.x or higher")},
{output: "go version go1 linux/amd64", errs: errors.New("found unsupported go version: 1.0.0; requires go 1.14.x or higher")},
{output: "go version go1.1.2 linux/amd64", errs: errors.New("found unsupported go version: 1.1.2; requires go 1.14.x or higher")},
+ {output: "go version go1.12rc1 linux/amd64", errs: errors.New("found unsupported go version: 1.12.0; requires go 1.14.x or higher")},
{output: "go version go1.12 linux/amd64", errs: errors.New("found unsupported go version: 1.12.0; requires go 1.14.x or higher")},
{output: "go version go1.13 linux/amd64", errs: errors.New("found unsupported go version: 1.13.0; requires go 1.14.x or higher")},
{output: "go version go1.13.2 linux/amd64", errs: errors.New("found unsupported go version: 1.13.2; requires go 1.14.x or higher")},
@@ -27,6 +28,8 @@ func TestParseAndIsSupportedVersion(t *testing.T) {
{output: "go version go1.14.2 linux/amd64"},
{output: "go version go1.15 linux/amd64"},
{output: "go version go1.15.44 linux/amd64"},
+ {output: "go version go1.16beta1 linux/amd64"},
+ {output: "go version go1.16rc1 linux/amd64"},
{output: "go version go2 linux/amd64"},
{output: "go version go2.1 linux/amd64"},
} {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment