Skip to content

Commit

Permalink
fix: deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-elliott committed May 3, 2023
1 parent 16cc89c commit 923936b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 29 deletions.
28 changes: 0 additions & 28 deletions go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

const metaVersion = "1.1.1"
37 changes: 36 additions & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions version_legacy.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 923936b

Please sign in to comment.