Skip to content

Commit 2db304e

Browse files
committed
Choose which version to install and write file
1 parent 644d7f1 commit 2db304e

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

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

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,99 @@ func installDependenciesAndBuild() {
696696
extract(depMode, modMode)
697697
}
698698

699-
func identifyEnvironment() {
699+
const minGoVersion = "1.11"
700+
const maxGoVersion = "1.20"
701+
702+
func outsideSupportedRange(version string) bool {
703+
short := semver.MajorMinor("v" + version)
704+
return semver.Compare(short, "v"+minGoVersion) < 0 || semver.Compare(short, "v"+maxGoVersion) > 0
705+
}
706+
707+
func isGoInstalled() bool {
708+
_, err := exec.LookPath("go")
709+
return err == nil
710+
}
711+
712+
func getVersionToInstall() string {
713+
log.Printf("Autobuilder was built with %s, environment has %s\n", runtime.Version(), getEnvGoVersion())
714+
depMode := getDepMode()
715+
goModVersion, goDirectiveFound := tryReadGoDirective(depMode)
716+
717+
if outsideSupportedRange(goModVersion) {
718+
log.Println("The version of Go specified in the go.mod file (" + goModVersion + ") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + ").")
719+
//TODO: emit diagnostic
720+
return ""
721+
}
722+
723+
if !isGoInstalled() {
724+
if goDirectiveFound {
725+
log.Println("No version of Go installed. Writing an `environment.json` file specifying the version of Go from the go.mod file (" + goModVersion + ").")
726+
return goModVersion
727+
} else {
728+
log.Println("No version of Go installed and no `go.mod` file found. Writing an `environment.json` file specifying the maximum supported version of Go (" + maxGoVersion + ").")
729+
return maxGoVersion
730+
}
731+
}
732+
733+
envVersion := getEnvGoVersion()[2:]
734+
735+
if outsideSupportedRange(envVersion) {
736+
log.Println("The version of Go installed in the environment (" + goModVersion + ") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + ").")
737+
//TODO: emit diagnostic
738+
return ""
739+
}
700740

741+
if !goDirectiveFound {
742+
log.Println("No `go.mod` file found. Version " + envVersion + " installed in the environment.")
743+
return ""
744+
}
745+
746+
if semver.Compare("v"+goModVersion, "v"+envVersion) > 0 {
747+
log.Println(
748+
"The version of Go installed in the environment (" + envVersion + ") is lower than the version specified in the go.mod file (" + goModVersion +
749+
").\nWriting an `environment.json` file specifying the version of go from the go.mod file (" + goModVersion + ").")
750+
return goModVersion
751+
}
752+
753+
// no need to install a version of Go
754+
return ""
755+
}
756+
757+
func writeEnvironmentFile(version string) {
758+
var content string
759+
if version == "" {
760+
content = `{ "include": [] }`
761+
} else {
762+
content = `{ "include": [ { "go": { "version": "` + version + `" } } ] }`
763+
}
764+
765+
targetFile, err := os.Create("environment.json")
766+
if err != nil {
767+
log.Println("Failed to create environment.json: ")
768+
log.Println(err)
769+
return
770+
}
771+
defer func() {
772+
if err := targetFile.Close(); err != nil {
773+
log.Println("Failed to close environment.json:")
774+
log.Println(err)
775+
}
776+
}()
777+
778+
_, err = targetFile.WriteString(content)
779+
if err != nil {
780+
log.Println("Failed to write to environment.json: ")
781+
log.Println(err)
782+
}
783+
}
784+
785+
func identifyEnvironment() {
786+
versionToInstall := getVersionToInstall()
787+
writeEnvironmentFile(versionToInstall)
701788
}
702789

703790
func main() {
704-
if len(os.Args) == 0 {
791+
if len(os.Args) == 1 {
705792
installDependenciesAndBuild()
706793
} else if len(os.Args) == 2 && os.Args[1] == "--identify-environment" {
707794
identifyEnvironment()

0 commit comments

Comments
 (0)