package version import ( "fmt" "runtime" "runtime/debug" ) var version = "dev" type Info struct { GitVersion string GitCommit string GitTreeState string BuildDate string BuiltBy string Clean bool GoVersion string Compiler string ModuleSum string Platform string } func Get() Info { info := Info{ GitVersion: version, GoVersion: runtime.Version(), Compiler: runtime.Compiler, Platform: runtime.GOOS + "/" + runtime.GOARCH, } bi, ok := debug.ReadBuildInfo() if !ok { return info } info.ModuleSum = bi.Main.Sum info.Clean = true for _, s := range bi.Settings { switch s.Key { case "vcs.revision": info.GitCommit = s.Value case "vcs.time": info.BuildDate = s.Value case "vcs.modified": if s.Value == "true" { info.Clean = false info.GitTreeState = "dirty" } else { info.GitTreeState = "clean" } } } return info } func (i Info) String() string { return fmt.Sprintf(`GitVersion: %s GitCommit: %s GitTreeState: %s BuildDate: %s BuiltBy: %s Clean: %t GoVersion: %s Compiler: %s ModuleSum: %s Platform: %s`, val(i.GitVersion), val(i.GitCommit), val(i.GitTreeState), val(i.BuildDate), val(i.BuiltBy), i.Clean, i.GoVersion, i.Compiler, val(i.ModuleSum), i.Platform, ) } func val(s string) string { if s == "" { return "unknown" } return s }