diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 2a51cc6..f5e0aea 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -6,6 +6,9 @@ before: builds: - main: "./main.go" + ldflags: + - -s -w -X "main.goversion={{.Env.GO_VERSION}}" + - -s -w -X "main.goinitversion={{.Env.GIT_TAG}}" binary: goinit env: - CGO_ENABLED=0 @@ -13,7 +16,6 @@ builds: - linux - windows - darwin - id: goinit archives: - format: tar.gz diff --git a/main.go b/main.go index 3f5ec7a..fe9f23e 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "fmt" "log" "os" + "runtime" + "runtime/debug" "github.com/samluiz/goinit/internal/cli" ) @@ -18,10 +20,26 @@ const banner string = ` ## ## ### ### ## ## ### ## ## ##### ##### ###### ## ## ###### ##` -var ( - displayVersion bool - version string -) +var displayVersion bool + +func GetVersionInfo() versionInfo { + if len(goinitversion) != 0 && len(goversion) != 0 { + return versionInfo{ + goinitversion: goinitversion, + goversion: goversion, + } + } + if info, ok := debug.ReadBuildInfo(); ok { + return versionInfo{ + goinitversion: info.Main.Version, + goversion: runtime.Version(), + } + } + return versionInfo{ + goinitversion: "(unknown)", + goversion: runtime.Version(), + } +} func helpMessage() { fmt.Println("Usage: goinit [flags]") @@ -43,9 +61,10 @@ func init() { } func main() { + versionInfo := GetVersionInfo() fmt.Print(banner) if displayVersion { - fmt.Println(version) + fmt.Printf("\nversion: %s\ngo Version: %s\n", versionInfo.goinitversion, versionInfo.goversion) return } cli.Run(os.Stdin) diff --git a/version.go b/version.go new file mode 100644 index 0000000..895de44 --- /dev/null +++ b/version.go @@ -0,0 +1,8 @@ +package main + +var goinitversion, goversion string + +type versionInfo struct { + goinitversion string + goversion string +}