diff --git a/.goreleaser.yml b/.goreleaser.yml index bb7465a..96543f2 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -17,7 +17,6 @@ builds: ldflags: - -s -w - -X main.version={{.Version}} - - -X main.commit={{.ShortCommit}} archives: - formats: diff --git a/cmd/esq/main.go b/cmd/esq/main.go index faef12a..d457201 100644 --- a/cmd/esq/main.go +++ b/cmd/esq/main.go @@ -6,12 +6,9 @@ import ( "github.com/enthus-appdev/esq-cli/internal/cmd" ) -var ( - version = "dev" - commit = "none" -) +var version = "dev" func main() { - exitCode := cmd.Execute(version, commit) + exitCode := cmd.Execute(version) os.Exit(exitCode) } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 8d41678..290ffc3 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "runtime/debug" "strings" configcmd "github.com/enthus-appdev/esq-cli/internal/cmd/config" @@ -16,7 +17,8 @@ import ( var envOverride string // Execute runs the root command and returns the exit code. -func Execute(ver, commit string) int { +func Execute(ver string) int { + commit, date := vcsInfo() rootCmd := &cobra.Command{ Use: "esq", Short: "Elasticsearch Query CLI", @@ -34,7 +36,7 @@ func Execute(ver, commit string) int { esq health`, SilenceUsage: true, SilenceErrors: true, - Version: fmt.Sprintf("%s (commit: %s)", ver, commit), + Version: fmt.Sprintf("%s\ncommit: %s\nbuilt: %s", ver, commit, date), } rootCmd.PersistentFlags().StringVarP(&envOverride, "env", "e", "", "Override active environment") @@ -128,6 +130,33 @@ func joinStrings(ss []string) string { return result } +func vcsInfo() (commit, date string) { + commit, date = "unknown", "unknown" + var modified bool + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + for _, s := range info.Settings { + switch s.Key { + case "vcs.revision": + if len(s.Value) > 7 { + commit = s.Value[:7] + } else { + commit = s.Value + } + case "vcs.time": + date = s.Value + case "vcs.modified": + modified = s.Value == "true" + } + } + if modified { + commit += "-dirty" + } + return +} + // --- Simple commands (kept in root.go since they're small) --- func newGetCmd() *cobra.Command {