1
1
package main
2
2
3
3
import (
4
- "bufio"
5
4
"fmt"
6
5
"log"
7
6
"net/url"
@@ -17,6 +16,7 @@ import (
17
16
18
17
"github.com/github/codeql-go/extractor/autobuilder"
19
18
"github.com/github/codeql-go/extractor/diagnostics"
19
+ "github.com/github/codeql-go/extractor/toolchain"
20
20
"github.com/github/codeql-go/extractor/util"
21
21
)
22
22
@@ -56,43 +56,9 @@ Build behavior:
56
56
fmt .Fprintf (os .Stderr , "Usage:\n \n %s\n " , os .Args [0 ])
57
57
}
58
58
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
-
93
59
// Returns the current Go version in semver format, e.g. v1.14.4
94
60
func getEnvGoSemVer () string {
95
- goVersion := getEnvGoVersion ()
61
+ goVersion := toolchain . GetEnvGoVersion ()
96
62
if ! strings .HasPrefix (goVersion , "go" ) {
97
63
log .Fatalf ("Expected 'go version' output of the form 'go1.2.3'; got '%s'" , goVersion )
98
64
}
@@ -780,7 +746,7 @@ func getBuildInfo(emitDiagnostics bool) BuildInfo {
780
746
781
747
// Build the project and run the extractor.
782
748
func 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 ())
784
750
785
751
srcdir := getSourceDir ()
786
752
@@ -1110,22 +1076,16 @@ func (v versionInfo) String() string {
1110
1076
v .goModVersion , v .goModVersionFound , v .goEnvVersion , v .goEnvVersionFound )
1111
1077
}
1112
1078
1113
- // Check if Go is installed in the environment.
1114
- func isGoInstalled () bool {
1115
- _ , err := exec .LookPath ("go" )
1116
- return err == nil
1117
- }
1118
-
1119
1079
// Get the version of Go to install and output it to stdout as json.
1120
1080
func identifyEnvironment () {
1121
1081
var v versionInfo
1122
1082
buildInfo := getBuildInfo (false )
1123
1083
goVersionInfo := tryReadGoDirective (buildInfo )
1124
1084
v .goModVersion , v .goModVersionFound = goVersionInfo .Version , goVersionInfo .Found
1125
1085
1126
- v .goEnvVersionFound = isGoInstalled ()
1086
+ v .goEnvVersionFound = toolchain . IsInstalled ()
1127
1087
if v .goEnvVersionFound {
1128
- v .goEnvVersion = getEnvGoVersion ()[2 :]
1088
+ v .goEnvVersion = toolchain . GetEnvGoVersion ()[2 :]
1129
1089
}
1130
1090
1131
1091
msg , versionToInstall := getVersionToInstall (v )
0 commit comments