From ffa1028a6eef7295012242cff6e8b879d1435bc2 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Mon, 17 Mar 2025 21:05:29 -0400 Subject: [PATCH] print the current version and build in verbose mode When specifying the `--verbose` command line option, the application will now attempt to add the current application version and build number to the output. If running from source, it will now print the current commit. Having this information will aid in troublehsooting bug tickets going forward. --- cmd/root.go | 11 +++++++++++ internal/metadata/metadata.go | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index e5c01a4d..8ca630af 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -78,6 +78,17 @@ func Execute() int { cfg := config.NewConfig(nil, nil, "", "", "") logger.InitializeLogger(cfg) + if metadata.Version != "" && metadata.Build != "" { + logger.Info("ipctl %s (%s)", metadata.Version, metadata.Build) + } else { + sha, err := metadata.GetCurrentSha() + if err == nil { + logger.Info("ipctl running from commit %s", sha) + } else { + logger.Info("ipctl unable to determine source") + } + } + profile, err := cfg.ActiveProfile() if err != nil { cmdutils.CheckError(err, cfg.TerminalNoColor) diff --git a/internal/metadata/metadata.go b/internal/metadata/metadata.go index 6b0598c3..cc78bd95 100644 --- a/internal/metadata/metadata.go +++ b/internal/metadata/metadata.go @@ -4,8 +4,44 @@ package metadata +import ( + "fmt" + "os" + + "github.com/go-git/go-git/v5" +) + // Represents the Git SHA (short) the build was compiled against var Build string // Represent the version of the build in the binary var Version string + +func GetCurrentSha() (string, error) { + cwd, err := os.Getwd() + if err != nil { + return "", err + } + + // Open the Git repository + repo, err := git.PlainOpen(cwd) + if err != nil { + return "", fmt.Errorf("failed to open repository: %w", err) + } + + // Get the HEAD reference + head, err := repo.Head() + if err != nil { + return "", fmt.Errorf("failed to get HEAD: %w", err) + } + + // Resolve the commit + commit, err := repo.CommitObject(head.Hash()) + if err != nil { + return "", fmt.Errorf("failed to get commit object: %w", err) + } + + // Extract the SHA + sha := commit.Hash.String() + return sha, nil +}