From c24268c83b9c7c7512cceff576bb0f343d900023 Mon Sep 17 00:00:00 2001 From: xiantang Date: Mon, 16 Oct 2023 16:52:33 +0800 Subject: [PATCH] fallback to buildinfo when airversion and goversion is empty (#474) * use build info as version, now can show version when using go install * fix failed test case --- main.go | 29 ++++++++++++++++++++++++++++- runner/config.go | 2 ++ version.go | 6 ++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 4635ae32..383935d4 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "log" "os" "os/signal" + "runtime" + "runtime/debug" "syscall" "github.com/cosmtrek/air/runner" @@ -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 diff --git a/runner/config.go b/runner/config.go index 5b53f63e..b784cdfb 100644 --- a/runner/config.go +++ b/runner/config.go @@ -214,6 +214,8 @@ func defaultConfig() Config { Log: "build-errors.log", IncludeExt: []string{"go", "tpl", "tmpl", "html"}, IncludeDir: []string{}, + PreCmd: []string{}, + PostCmd: []string{}, ExcludeFile: []string{}, IncludeFile: []string{}, ExcludeDir: []string{"assets", "tmp", "vendor", "testdata"}, diff --git a/version.go b/version.go index a969d1b6..e8904dc4 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,6 @@ package main -var airVersion string -var goVersion string +var ( + airVersion string + goVersion string +)