From 4e2b2ed7bac7e20cbb19508cbeaeb468c191a9d0 Mon Sep 17 00:00:00 2001 From: Janez T Date: Tue, 10 Dec 2024 14:03:11 +0100 Subject: [PATCH] Add version command and enhance versioning information in CLI --- .goreleaser.yaml | 4 +++- cmd/root.go | 2 ++ cmd/version.go | 22 ++++++++++++++++++++++ shared/http.go | 2 +- shared/version.go | 7 +++++-- 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 cmd/version.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index a1ba21d..291a0ba 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -17,8 +17,10 @@ builds: goarch: - amd64 - arm64 + gcflags: + - all=-trimpath=${GOPATH} ldflags: - - -s -w -X paretosecurity.com/auditor/shared.Version={{.Version}} + - -s -w -X shared.Version={{.Version}} -X shared.Commit={{.Commit}} -X shared.Date={{.Date}} binary: paretosecurity archives: diff --git a/cmd/root.go b/cmd/root.go index fcdfcc2..567d928 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -13,6 +13,7 @@ var rootCmd = &cobra.Command{ Long: `Pareto Security CLI is a tool for running and reporting audits to paretosecurity.com.`, PersistentPreRun: func(cmd *cobra.Command, args []string) { + if verbose { log.SetLevel(log.DebugLevel) } @@ -21,6 +22,7 @@ to paretosecurity.com.`, func init() { rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "output verbose logs") + } func Execute() { diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..b87aacb --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "os" + + "github.com/caarlos0/log" + "github.com/spf13/cobra" + "paretosecurity.com/auditor/shared" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version information", + Run: func(cmd *cobra.Command, args []string) { + log.Infof("%s@%s %s", shared.Version, shared.Commit, shared.Date) + os.Exit(0) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/shared/http.go b/shared/http.go index 6b9c1dc..64d436e 100644 --- a/shared/http.go +++ b/shared/http.go @@ -55,7 +55,7 @@ func createSafeHTTPClient() *http.Client { // Wrap the transport to insert the User-Agent header client.Transport = &userAgentTransport{ Transport: customTransport, - agent: fmt.Sprintf("Pareto Security/%s (Linux; build:%s)", Version, Hash), + agent: fmt.Sprintf("Pareto Security/%s (Linux; build:%s)", Version, Commit), } return client diff --git a/shared/version.go b/shared/version.go index cf683d8..0d3a07b 100644 --- a/shared/version.go +++ b/shared/version.go @@ -1,4 +1,7 @@ package shared -const Version = "0.1.0" -const Hash = "dev" +var ( + Version = "dev" + Commit = "none" + Date = "unknown" +)