Skip to content

Commit d570914

Browse files
authored
Merge pull request github#13129 from owen-mc/go/identify-environment-output-to-stdout
Go: --identify-environment output to stdout
2 parents 62f0c64 + 1beb348 commit d570914

File tree

1 file changed

+11
-33
lines changed

1 file changed

+11
-33
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ func usage() {
2525
2626
Options:
2727
--identify-environment
28-
Produce an environment file specifying which Go version should be installed in the environment
29-
so that autobuilding will be successful. The location of this file is controlled by the
30-
environment variable CODEQL_EXTRACTOR_ENVIRONMENT_JSON, or defaults to 'environment.json' if
31-
that is not set.
28+
Output some json on stdout specifying which Go version should be installed in the environment
29+
so that autobuilding will be successful.
3230
3331
Build behavior:
3432
@@ -745,8 +743,8 @@ func getVersionWhenGoModVersionNotFound(v versionInfo) (msg, version string) {
745743
// There is no Go version installed in the environment. We have no indication which version
746744
// was intended to be used to build this project. Go versions are generally backwards
747745
// compatible, so we install the maximum supported version.
748-
msg = "No version of Go installed and no `go.mod` file found. Writing an environment " +
749-
"file specifying the maximum supported version of Go (" + maxGoVersion + ")."
746+
msg = "No version of Go installed and no `go.mod` file found. Requesting the maximum " +
747+
"supported version of Go (" + maxGoVersion + ")."
750748
version = maxGoVersion
751749
diagnostics.EmitNoGoModAndNoGoEnv(msg)
752750
} else if outsideSupportedRange(v.goEnvVersion) {
@@ -928,39 +926,19 @@ func getVersionToInstall(v versionInfo) (msg, version string) {
928926
return getVersionWhenGoModVersionSupported(v)
929927
}
930928

931-
// Write an environment file to the current directory. If `version` is the empty string then
932-
// write an empty environment file, otherwise write an environment file specifying the version
933-
// of Go to install. The path to the environment file is specified by the
934-
// CODEQL_EXTRACTOR_ENVIRONMENT_JSON environment variable, or defaults to `environment.json`.
935-
func writeEnvironmentFile(version string) {
929+
// Output some JSON to stdout specifying the version of Go to install, unless `version` is the
930+
// empty string.
931+
func outputEnvironmentJson(version string) {
936932
var content string
937933
if version == "" {
938934
content = `{ "include": [] }`
939935
} else {
940936
content = `{ "include": [ { "go": { "version": "` + version + `" } } ] }`
941937
}
938+
_, err := fmt.Fprint(os.Stdout, content)
942939

943-
filename, ok := os.LookupEnv("CODEQL_EXTRACTOR_ENVIRONMENT_JSON")
944-
if !ok {
945-
filename = "environment.json"
946-
}
947-
948-
targetFile, err := os.Create(filename)
949-
if err != nil {
950-
log.Println("Failed to create environment file " + filename + ": ")
951-
log.Println(err)
952-
return
953-
}
954-
defer func() {
955-
if err := targetFile.Close(); err != nil {
956-
log.Println("Failed to close environment file " + filename + ":")
957-
log.Println(err)
958-
}
959-
}()
960-
961-
_, err = targetFile.WriteString(content)
962940
if err != nil {
963-
log.Println("Failed to write to environment file " + filename + ": ")
941+
log.Println("Failed to write environment json to stdout: ")
964942
log.Println(err)
965943
}
966944
}
@@ -984,7 +962,7 @@ func isGoInstalled() bool {
984962
return err == nil
985963
}
986964

987-
// Get the version of Go to install and write it to an environment file.
965+
// Get the version of Go to install and output it to stdout as json.
988966
func identifyEnvironment() {
989967
var v versionInfo
990968
depMode := getDepMode()
@@ -998,7 +976,7 @@ func identifyEnvironment() {
998976
msg, versionToInstall := getVersionToInstall(v)
999977
log.Println(msg)
1000978

1001-
writeEnvironmentFile(versionToInstall)
979+
outputEnvironmentJson(versionToInstall)
1002980
}
1003981

1004982
func main() {

0 commit comments

Comments
 (0)