From 923936bc54bc4cd49e5ee90565eaad35d2e1fcba Mon Sep 17 00:00:00 2001 From: James Elliott Date: Wed, 3 May 2023 12:18:44 +1000 Subject: [PATCH] fix: deprecations --- go.go | 28 ---------------------------- meta.go | 3 +++ version.go | 37 ++++++++++++++++++++++++++++++++++++- version_legacy.go | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 meta.go create mode 100644 version_legacy.go diff --git a/go.go b/go.go index 0c9769b..9caa338 100644 --- a/go.go +++ b/go.go @@ -132,34 +132,6 @@ func GoRoot() (string, error) { return strings.TrimSpace(output), nil } -// GoVersion reads the version of `go` that is on the PATH. This is done -// instead of `runtime.Version()` because it is possible to run gox against -// another Go version. -func GoVersion() (string, error) { - // NOTE: We use `go run` instead of `go version` because the output - // of `go version` might change whereas the source is guaranteed to run - // for some time thanks to Go's compatibility guarantee. - - td, err := os.MkdirTemp("", "gox") - if err != nil { - return "", err - } - defer os.RemoveAll(td) - - // Write the source code for the program that will generate the version - sourcePath := filepath.Join(td, "version.go") - if err := os.WriteFile(sourcePath, []byte(versionSource), 0644); err != nil { - return "", err - } - - // Execute and read the version, which will be the only thing on stdout. - version, err := execGo(gobin, nil, "", "run", sourcePath) - - fmt.Printf("Detected Go Version: %s\n", version) - - return version, err -} - // GoVersionParts parses the version numbers from the version itself // into major and minor: 1.5, 1.4, etc. func GoVersionParts() (result [2]int, err error) { diff --git a/meta.go b/meta.go new file mode 100644 index 0000000..a900207 --- /dev/null +++ b/meta.go @@ -0,0 +1,3 @@ +package main + +const metaVersion = "1.1.1" diff --git a/version.go b/version.go index a900207..4aeb539 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,38 @@ +//go:build go1.16 +// +build go1.16 + package main -const metaVersion = "1.1.1" +import ( + "fmt" + "os" + "path/filepath" +) + +// GoVersion reads the version of `go` that is on the PATH. This is done +// instead of `runtime.Version()` because it is possible to run gox against +// another Go version. +func GoVersion() (string, error) { + // NOTE: We use `go run` instead of `go version` because the output + // of `go version` might change whereas the source is guaranteed to run + // for some time thanks to Go's compatibility guarantee. + + td, err := os.MkdirTemp("", "gox") + if err != nil { + return "", err + } + defer os.RemoveAll(td) + + // Write the source code for the program that will generate the version + sourcePath := filepath.Join(td, "version.go") + if err := os.WriteFile(sourcePath, []byte(versionSource), 0644); err != nil { + return "", err + } + + // Execute and read the version, which will be the only thing on stdout. + version, err := execGo(gobin, nil, "", "run", sourcePath) + + fmt.Printf("Detected Go Version: %s\n", version) + + return version, err +} diff --git a/version_legacy.go b/version_legacy.go new file mode 100644 index 0000000..811f88e --- /dev/null +++ b/version_legacy.go @@ -0,0 +1,38 @@ +//go:build !go1.16 +// +build !go1.16 + +package main + +import ( + "fmt" + "os" + "path/filepath" +) + +// GoVersion reads the version of `go` that is on the PATH. This is done +// instead of `runtime.Version()` because it is possible to run gox against +// another Go version. +func GoVersion() (string, error) { + // NOTE: We use `go run` instead of `go version` because the output + // of `go version` might change whereas the source is guaranteed to run + // for some time thanks to Go's compatibility guarantee. + + td, err := ioutil.TempDir("", "gox") + if err != nil { + return "", err + } + defer os.RemoveAll(td) + + // Write the source code for the program that will generate the version + sourcePath := filepath.Join(td, "version.go") + if err := ioutil.WriteFile(sourcePath, []byte(versionSource), 0644); err != nil { + return "", err + } + + // Execute and read the version, which will be the only thing on stdout. + version, err := execGo(gobin, nil, "", "run", sourcePath) + + fmt.Printf("Detected Go Version: %s\n", version) + + return version, err +}