diff --git a/cmd/gb/main.go b/cmd/gb/main.go index c5e14fb..de67dcd 100644 --- a/cmd/gb/main.go +++ b/cmd/gb/main.go @@ -2,8 +2,11 @@ package main import ( "flag" + "io/ioutil" "os" "path/filepath" + "runtime" + "strings" "github.com/constabulary/gb" "github.com/constabulary/gb/cmd" @@ -46,6 +49,9 @@ func main() { help(args[2:]) return } + + verifyGoVersion() + command, ok := commands[name] if (command != nil && !command.Runnable()) || !ok { if _, err := lookupPlugin(name); err != nil { @@ -105,3 +111,19 @@ func main() { gb.Fatalf("command %q failed: %v", name, err) } } + +// verify that the version of Go that compiled this binary is still correct. +func verifyGoVersion() { + want := runtime.Version() + filename := filepath.Join(runtime.GOROOT(), "VERSION") + if strings.Contains(want, "devel") { + filename += ".cache" + } + got, err := ioutil.ReadFile(filename) + if err != nil { + gb.Fatalf("cannot validate Go version: %v", err) + } + if want != string(got) { + gb.Fatalf("Go version does not match: gb was compiled with %q, installed Go version from %v is %q", want, runtime.GOROOT(), got) + } +}