11package main
22
33import (
4- "bufio"
54 "fmt"
65 "log"
76 "net/url"
@@ -17,6 +16,7 @@ import (
1716
1817 "github.com/github/codeql-go/extractor/autobuilder"
1918 "github.com/github/codeql-go/extractor/diagnostics"
19+ "github.com/github/codeql-go/extractor/toolchain"
2020 "github.com/github/codeql-go/extractor/util"
2121)
2222
@@ -56,43 +56,9 @@ Build behavior:
5656 fmt .Fprintf (os .Stderr , "Usage:\n \n %s\n " , os .Args [0 ])
5757}
5858
59- var goVersion = ""
60-
61- // Returns the current Go version as returned by 'go version', e.g. go1.14.4
62- func getEnvGoVersion () string {
63- if goVersion == "" {
64- // Since Go 1.21, running 'go version' in a directory with a 'go.mod' file will attempt to
65- // download the version of Go specified in there. That may either fail or result in us just
66- // being told what's already in 'go.mod'. Setting 'GOTOOLCHAIN' to 'local' will force it
67- // to use the local Go toolchain instead.
68- cmd := exec .Command ("go" , "version" )
69- cmd .Env = append (os .Environ (), "GOTOOLCHAIN=local" )
70- out , err := cmd .CombinedOutput ()
71-
72- if err != nil {
73- log .Fatalf ("Unable to run the go command, is it installed?\n Error: %s" , err .Error ())
74- }
75-
76- goVersion = parseGoVersion (string (out ))
77- }
78- return goVersion
79- }
80-
81- // The 'go version' command may output warnings on separate lines before
82- // the actual version string is printed. This function parses the output
83- // to retrieve just the version string.
84- func parseGoVersion (data string ) string {
85- var lastLine string
86- sc := bufio .NewScanner (strings .NewReader (data ))
87- for sc .Scan () {
88- lastLine = sc .Text ()
89- }
90- return strings .Fields (lastLine )[2 ]
91- }
92-
9359// Returns the current Go version in semver format, e.g. v1.14.4
9460func getEnvGoSemVer () string {
95- goVersion := getEnvGoVersion ()
61+ goVersion := toolchain . GetEnvGoVersion ()
9662 if ! strings .HasPrefix (goVersion , "go" ) {
9763 log .Fatalf ("Expected 'go version' output of the form 'go1.2.3'; got '%s'" , goVersion )
9864 }
@@ -780,7 +746,7 @@ func getBuildInfo(emitDiagnostics bool) BuildInfo {
780746
781747// Build the project and run the extractor.
782748func installDependenciesAndBuild () {
783- log .Printf ("Autobuilder was built with %s, environment has %s\n " , runtime .Version (), getEnvGoVersion ())
749+ log .Printf ("Autobuilder was built with %s, environment has %s\n " , runtime .Version (), toolchain . GetEnvGoVersion ())
784750
785751 srcdir := getSourceDir ()
786752
@@ -1110,22 +1076,16 @@ func (v versionInfo) String() string {
11101076 v .goModVersion , v .goModVersionFound , v .goEnvVersion , v .goEnvVersionFound )
11111077}
11121078
1113- // Check if Go is installed in the environment.
1114- func isGoInstalled () bool {
1115- _ , err := exec .LookPath ("go" )
1116- return err == nil
1117- }
1118-
11191079// Get the version of Go to install and output it to stdout as json.
11201080func identifyEnvironment () {
11211081 var v versionInfo
11221082 buildInfo := getBuildInfo (false )
11231083 goVersionInfo := tryReadGoDirective (buildInfo )
11241084 v .goModVersion , v .goModVersionFound = goVersionInfo .Version , goVersionInfo .Found
11251085
1126- v .goEnvVersionFound = isGoInstalled ()
1086+ v .goEnvVersionFound = toolchain . IsInstalled ()
11271087 if v .goEnvVersionFound {
1128- v .goEnvVersion = getEnvGoVersion ()[2 :]
1088+ v .goEnvVersion = toolchain . GetEnvGoVersion ()[2 :]
11291089 }
11301090
11311091 msg , versionToInstall := getVersionToInstall (v )
0 commit comments