From c8f4c09003f6be605362256bc419f304481353fa Mon Sep 17 00:00:00 2001 From: Sai Sathvik Date: Sat, 6 Jun 2026 18:54:27 +0530 Subject: [PATCH 1/3] feat(version): Enhance CLI version information * Embed commit hash, build date, Go version, and OS/architecture into binaries. * Update `Makefile` and `.goreleaser.yaml` to inject build metadata via `ldflags`. * Implement custom `--version` flag handling with a verbose option for detailed output. * Improve debuggability and traceability of deployed binaries. --- .goreleaser.yaml | 5 ++++- Makefile | 11 ++++++++++- cmd/optiqor/main.go | 43 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index fa4ab06..407ebad 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -15,7 +15,10 @@ builds: flags: - -trimpath ldflags: - - -s -w -X main.version={{.Version}} + - -s -w + - -X main.version={{.Version}} + - -X main.commit={{.ShortCommit}} + - -X main.buildDate={{.Date}} goos: - linux - darwin diff --git a/Makefile b/Makefile index 3ad30aa..39aae47 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,16 @@ .DEFAULT_GOAL := help GO ?= go -LDFLAGS ?= -s -w -X main.version=$(shell git describe --tags --always --dirty 2>/dev/null || echo dev) +VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev) +COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo none) +BUILD_DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") + +LDFLAGS ?= \ + -s -w \ + -X main.version=$(VERSION) \ + -X main.commit=$(COMMIT) \ + -X main.buildDate=$(BUILD_DATE) + BIN_DIR := bin .PHONY: help diff --git a/cmd/optiqor/main.go b/cmd/optiqor/main.go index 477d675..977c0c0 100644 --- a/cmd/optiqor/main.go +++ b/cmd/optiqor/main.go @@ -10,6 +10,7 @@ import ( "io" "os" "path/filepath" + "runtime" "github.com/spf13/cobra" @@ -34,13 +35,38 @@ const ( // errFindings is a sentinel returned from RunE so main can map it to exitFindings. var errFindings = errors.New("optiqor: findings exceed threshold") -var version = "dev" +var ( + version = "dev" + commit = "none" + buildDate = "unknown" +) // accuracyDisclosure is the mandatory line every command's help and // output must contain (hard rule per CLAUDE.md). const accuracyDisclosure = "Sandbox accuracy: ±40%. Install the Optiqor agent for exact numbers (optiqor.dev/get)." func main() { + var showVersion bool + var verbose bool + + for _, arg := range os.Args[1:] { + switch arg { + case "--version", "-v": + showVersion = true + case "--verbose": + verbose = true + } + } + + if showVersion { + if verbose { + fmt.Println(versionTemplate()) + } else { + fmt.Printf("optiqor %s\n", version) + } + os.Exit(0) + } + err := newRootCmd().Execute() switch { case err == nil: @@ -59,6 +85,7 @@ func newRootCmd() *cobra.Command { noColor bool configPath string ) + var verboseVersion bool root := &cobra.Command{ Use: "optiqor", @@ -90,7 +117,7 @@ namespaces, etc.). Cost is the headline; security is a side-effect. root.PersistentFlags().BoolVar(&noColor, "no-color", false, "disable colored output (also: NO_COLOR env)") root.PersistentFlags().StringVar(&configPath, "config", "", "path to .optiqor.yaml (default: ./.optiqor.yaml or $OPTIQOR_CONFIG)") - + root.PersistentFlags().BoolVar(&verboseVersion,"verbose",false,"show detailed version information") root.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { cfg, err := config.Load(configPath) if err != nil { @@ -105,8 +132,6 @@ namespaces, etc.). Cost is the headline; security is a side-effect. return nil } - root.SetVersionTemplate(versionTemplate()) - root.AddCommand( newAnalyzeCmd(), newDemoCmd(), @@ -121,7 +146,15 @@ namespaces, etc.). Cost is the headline; security is a side-effect. } func versionTemplate() string { - return fmt.Sprintf("optiqor %s — %s\n", version, "Helm chart cost analysis (security bonus)") + return fmt.Sprintf( + "optiqor %s\ncommit: %s\nbuilt: %s\ngo version: %s\nos/arch: %s/%s", + version, + commit, + buildDate, + runtime.Version(), + runtime.GOOS, + runtime.GOARCH, + ) } func newAnalyzeCmd() *cobra.Command { From b87f90cf3ea3af6c34f738d7c6f252cf9ec83d9f Mon Sep 17 00:00:00 2001 From: Sai Sathvik Date: Sat, 6 Jun 2026 19:37:52 +0530 Subject: [PATCH 2/3] docs(readme): Document `--version --verbose` flag and clarify `--version` entry * Add `--version --verbose` to the list of available commands. * Include `--version --verbose` in the table of general flags. * Explicitly list `--version` in the general flags table. * Enhance clarity regarding CLI options in the README. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 463654f..bb7eb93 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ The pipeline is deterministic. The same input always produces the same output. T | `compare ` | Currently an alias for `diff` (richer output ships in Phase 7) | Beta | | `watch [chart]` | Re-analyze on file change | Coming soon | | `--version` | Print version and exit | Stable | +| `--version --verbose` | Print full version and exit | Stable | | `--help` | Help for any command | Stable | ### Filter and exit-code flags @@ -439,6 +440,8 @@ flowchart TD | Flag | Default | Description | | --- | --- | --- | +| `--version` | true | Print version and exit | +| `--version --verbose` | true | Print full version and exit | | `--json` | false | Emit machine-readable JSON | | `--offline` | true | Do not perform any network calls | | `--share` | false | Upload sanitized analysis to optiqor.dev (opt-in) | From 1dc9c02e28c3e4d973acdb43424053cfbe6cbf53 Mon Sep 17 00:00:00 2001 From: Sai Sathvik Date: Sat, 6 Jun 2026 19:58:13 +0530 Subject: [PATCH 3/3] style(main): Standardize indentation by replacing tabs with spaces * Replaced tab characters with spaces for variable declarations. * Corrected indentation within the `main` function. * Adjusted indentation for flags in the `newRootCmd` function. * Ensured consistent code formatting throughout the file. --- cmd/optiqor/main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/optiqor/main.go b/cmd/optiqor/main.go index 977c0c0..d93a841 100644 --- a/cmd/optiqor/main.go +++ b/cmd/optiqor/main.go @@ -36,9 +36,9 @@ const ( var errFindings = errors.New("optiqor: findings exceed threshold") var ( - version = "dev" - commit = "none" - buildDate = "unknown" + version = "dev" + commit = "none" + buildDate = "unknown" ) // accuracyDisclosure is the mandatory line every command's help and @@ -66,7 +66,7 @@ func main() { } os.Exit(0) } - + err := newRootCmd().Execute() switch { case err == nil: @@ -85,7 +85,7 @@ func newRootCmd() *cobra.Command { noColor bool configPath string ) - var verboseVersion bool + var verboseVersion bool root := &cobra.Command{ Use: "optiqor", @@ -117,7 +117,7 @@ namespaces, etc.). Cost is the headline; security is a side-effect. root.PersistentFlags().BoolVar(&noColor, "no-color", false, "disable colored output (also: NO_COLOR env)") root.PersistentFlags().StringVar(&configPath, "config", "", "path to .optiqor.yaml (default: ./.optiqor.yaml or $OPTIQOR_CONFIG)") - root.PersistentFlags().BoolVar(&verboseVersion,"verbose",false,"show detailed version information") + root.PersistentFlags().BoolVar(&verboseVersion, "verbose", false, "show detailed version information") root.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { cfg, err := config.Load(configPath) if err != nil {