Files
at-container-registry/deploy/upcloud/goversion.go
2026-02-12 20:28:00 -06:00

36 lines
911 B
Go

package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
// requiredGoVersion reads the Go version from the root go.mod file.
// Returns a version string like "1.25.7" for use in download URLs.
func requiredGoVersion() (string, error) {
_, thisFile, _, _ := runtime.Caller(0)
rootMod := filepath.Join(filepath.Dir(thisFile), "..", "..", "go.mod")
data, err := os.ReadFile(rootMod)
if err != nil {
return "", fmt.Errorf("read root go.mod: %w", err)
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "go ") {
version := strings.TrimPrefix(line, "go ")
version = strings.TrimSpace(version)
// Validate it looks like a version
if len(version) > 0 && version[0] >= '1' && version[0] <= '9' {
return version, nil
}
}
}
return "", fmt.Errorf("no 'go X.Y.Z' directive found in %s", rootMod)
}