Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ builds:
ldflags:
- -s -w
- -X main.version={{.Version}}
- -X main.commit={{.ShortCommit}}

archives:
- formats:
Expand Down
7 changes: 2 additions & 5 deletions cmd/esq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
33 changes: 31 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"runtime/debug"
"strings"

configcmd "github.com/enthus-appdev/esq-cli/internal/cmd/config"
Expand All @@ -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",
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
Comment thread
fank marked this conversation as resolved.

// --- Simple commands (kept in root.go since they're small) ---

func newGetCmd() *cobra.Command {
Expand Down
Loading