From 4daf4c431c8fbf561c2e79002c6cde17f82d569b Mon Sep 17 00:00:00 2001 From: xiantang Date: Mon, 16 Oct 2023 13:53:36 +0800 Subject: [PATCH] refactor code --- main.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) 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