diff --git a/main.go b/main.go index d30f0a78..383935d4 100644 --- a/main.go +++ b/main.go @@ -45,34 +45,38 @@ func parseFlag(args []string) { flag.CommandLine.Parse(args) } -func versionInfo() *debug.BuildInfo { +type versionInfo struct { + airVersion string + goVersion string +} + +func GetVersionInfo() versionInfo { + if len(airVersion) != 0 && len(goVersion) != 0 { + return versionInfo{ + airVersion: airVersion, + goVersion: goVersion, + } + } if info, ok := debug.ReadBuildInfo(); ok { - return info + return versionInfo{ + airVersion: info.Main.Version, + goVersion: runtime.Version(), + } + } + return versionInfo{ + airVersion: "(unknown)", + goVersion: runtime.Version(), } - return nil } func main() { - versionInfo := versionInfo() - var goVersion string - var airVersion string - if versionInfo != nil { - goVersion = versionInfo.GoVersion - airVersion = versionInfo.Main.Version - } else { - airVersion = "(unknown version)" - goVersion = runtime.Version() - } - // TODO: how to get the version of the binary? gofumpt - // https://github.com/cosmtrek/air/issues/434 but just get devel :( - // https: // www.reddit.com/r/golang/comments/udhml7/best_way_to_embed_version_info_into_binary/ - // go embed maybe a good idea + versionInfo := GetVersionInfo() fmt.Printf(` __ _ ___ / /\ | | | |_) /_/--\ |_| |_| \_ %s, built with Go %s -`, airVersion, goVersion) +`, versionInfo.airVersion, versionInfo.goVersion) if showVersion { return