Skip to content

Commit 02a224c

Browse files
committed
--identify-environment should write json to stdout
1 parent a0a8468 commit 02a224c

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

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

Lines changed: 9 additions & 31 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
@@ -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)