diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..5c314e5 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +v0.0.5 diff --git a/cmd/root.go b/cmd/root.go index b76e5c3..8771b6b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,10 +22,13 @@ ENVIRONMENT VARIABLES // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute(version string) { - rootCmd.Version = version +func Execute() { err := rootCmd.Execute() if err != nil { os.Exit(1) } } + +func SetVersion(version string) { + rootCmd.Version = version +} diff --git a/main.go b/main.go index 6db7c1c..491cc14 100644 --- a/main.go +++ b/main.go @@ -1,17 +1,10 @@ package main import ( - "fmt" - "github.com/tnyeanderson/ddns/cmd" ) -// These get populated by goreleaser -var ( - version = "dev" - commit = "none" -) - func main() { - cmd.Execute(fmt.Sprintf("%s commit:%s", version, commit)) + cmd.SetVersion(version()) + cmd.Execute() } diff --git a/version.go b/version.go new file mode 100644 index 0000000..a6ca27c --- /dev/null +++ b/version.go @@ -0,0 +1,30 @@ +package main + +import ( + "runtime/debug" + "strings" + + _ "embed" +) + +//go:embed VERSION +var versionTag string + +func getCommit() string { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + } + return "" +} + +func version() string { + s := strings.TrimSpace(versionTag) + if c := getCommit(); c != "" { + s += " commit:" + c + } + return s +}