Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
xiantang committed Oct 16, 2023
1 parent 1c66a49 commit 4daf4c4
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4daf4c4

Please sign in to comment.