Skip to content

Commit

Permalink
use build info as version, now can show version when using go install
Browse files Browse the repository at this point in the history
  • Loading branch information
xiantang committed Oct 16, 2023
1 parent d771611 commit fc9e2b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
29 changes: 28 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"os"
"os/signal"
"runtime"
"runtime/debug"
"syscall"

"github.com/cosmtrek/air/runner"
Expand Down Expand Up @@ -43,13 +45,38 @@ func parseFlag(args []string) {
flag.CommandLine.Parse(args)
}

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 versionInfo{
airVersion: info.Main.Version,
goVersion: runtime.Version(),
}
}
return versionInfo{
airVersion: "(unknown)",
goVersion: runtime.Version(),
}
}

func main() {
versionInfo := GetVersionInfo()
fmt.Printf(`
__ _ ___
/ /\ | | | |_)
/_/--\ |_| |_| \_ %s, built with Go %s
`, airVersion, goVersion)
`, versionInfo.airVersion, versionInfo.goVersion)

if showVersion {
return
Expand Down
6 changes: 4 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package main

var airVersion string
var goVersion string
var (
airVersion string
goVersion string
)

0 comments on commit fc9e2b7

Please sign in to comment.