Skip to content

Commit 9af9b32

Browse files
committed
Find the last line of output from go version
1 parent 44213f0 commit 9af9b32

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"fmt"
56
"io/ioutil"
67
"log"
@@ -56,11 +57,23 @@ func getEnvGoVersion() string {
5657
if err != nil {
5758
log.Fatalf("Unable to run the go command, is it installed?\nError: %s", err.Error())
5859
}
59-
goVersion = strings.Fields(string(gover))[2]
60+
goVersion = parseGoVersion(string(gover))
6061
}
6162
return goVersion
6263
}
6364

65+
// The 'go version' command may output warnings on separate lines before
66+
// the actual version string is printed. This function parses the output
67+
// to retrieve just the version string.
68+
func parseGoVersion(data string) string {
69+
var lastLine string
70+
sc := bufio.NewScanner(strings.NewReader(data))
71+
for sc.Scan() {
72+
lastLine = sc.Text()
73+
}
74+
return strings.Fields(lastLine)[2]
75+
}
76+
6477
// Returns the current Go version in semver format, e.g. v1.14.4
6578
func getEnvGoSemVer() string {
6679
goVersion := getEnvGoVersion()

0 commit comments

Comments
 (0)