Skip to content

Commit 730a67f

Browse files
Copilotfrjcomp
andauthored
Add global --version flag with GoReleaser integration (#358)
* Add global --version flag with GoReleaser integration --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: frjcomp <107982661+frjcomp@users.noreply.github.com>
1 parent 4e5100a commit 730a67f

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

goreleaser.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ builds:
1010
- windows
1111
goarch:
1212
- amd64
13-
- arm64
13+
- arm64
14+
ldflags:
15+
- -s -w
16+
- -X github.com/CompassSecurity/pipeleak/cmd.Version={{.Version}}
17+
- -X github.com/CompassSecurity/pipeleak/cmd.Commit={{.Commit}}
18+
- -X github.com/CompassSecurity/pipeleak/cmd.Date={{.Date}}

src/pipeleak/cmd/root.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ import (
2323
// TerminalRestorer is a function that can be called to restore terminal state
2424
var TerminalRestorer func()
2525

26+
// Version information - set via ldflags during build
27+
var (
28+
Version = "dev"
29+
Commit = "none"
30+
Date = "unknown"
31+
)
32+
2633
var (
2734
rootCmd = &cobra.Command{
2835
Use: "pipeleak",
2936
Short: "Scan job logs and artifacts for secrets",
3037
Long: "Pipeleak is a tool designed to scan CI/CD job output logs and artifacts for potential secrets.",
3138
Example: "pipeleak gl scan --token glpat-xxxxxxxxxxx --gitlab https://gitlab.com",
39+
Version: getVersion(),
3240
PersistentPreRun: func(cmd *cobra.Command, args []string) {
3341
initLogger(cmd)
3442
setGlobalLogLevel(cmd)
@@ -46,6 +54,11 @@ func Execute() error {
4654
return rootCmd.Execute()
4755
}
4856

57+
// getVersion returns the version string in the format: version (commit) built on date
58+
func getVersion() string {
59+
return Version
60+
}
61+
4962
func init() {
5063
rootCmd.AddCommand(github.NewGitHubRootCmd())
5164
rootCmd.AddCommand(gitlab.NewGitLabRootCmd())
@@ -60,6 +73,10 @@ func init() {
6073
rootCmd.PersistentFlags().StringVar(&LogLevel, "log-level", "", "Set log level globally (debug, info, warn, error). Example: --log-level=warn")
6174
rootCmd.PersistentFlags().BoolVar(&LogColor, "color", true, "Enable colored log output (auto-disabled when using --logfile)")
6275

76+
// Set custom version template to show detailed version info
77+
rootCmd.SetVersionTemplate(`{{.Version}}
78+
`)
79+
6380
rootCmd.AddGroup(&cobra.Group{ID: "GitHub", Title: "GitHub Commands"})
6481
rootCmd.AddGroup(&cobra.Group{ID: "GitLab", Title: "GitLab Commands"})
6582
rootCmd.AddGroup(&cobra.Group{ID: "Helper", Title: "Various Helper Commands"})

src/pipeleak/cmd/root_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,49 @@ func TestCustomWriter_WritesCorrectly(t *testing.T) {
227227
assert.Contains(t, string(content), "test", "Log content should be written")
228228
})
229229
}
230+
231+
func TestVersionFlagRegistered(t *testing.T) {
232+
// Version flag is automatically added by Cobra when Version is set on the command
233+
// Check that the rootCmd has a version set
234+
assert.NotEmpty(t, rootCmd.Version, "rootCmd should have a version set")
235+
}
236+
237+
func TestGetVersion(t *testing.T) {
238+
// Save original values
239+
originalVersion := Version
240+
originalCommit := Commit
241+
originalDate := Date
242+
defer func() {
243+
Version = originalVersion
244+
Commit = originalCommit
245+
Date = originalDate
246+
}()
247+
248+
t.Run("returns default dev version", func(t *testing.T) {
249+
Version = "dev"
250+
Commit = "none"
251+
Date = "unknown"
252+
result := getVersion()
253+
assert.Equal(t, "dev", result)
254+
})
255+
256+
t.Run("returns git tag version", func(t *testing.T) {
257+
Version = "v1.2.3"
258+
Commit = "abc123"
259+
Date = "2025-11-17"
260+
result := getVersion()
261+
assert.Equal(t, "v1.2.3", result)
262+
})
263+
264+
t.Run("returns commit hash when not on tag", func(t *testing.T) {
265+
Version = "70926c9"
266+
Commit = "70926c9"
267+
Date = "2025-11-17"
268+
result := getVersion()
269+
assert.Equal(t, "70926c9", result)
270+
})
271+
}
272+
273+
func TestRootCmdHasVersion(t *testing.T) {
274+
assert.NotEmpty(t, rootCmd.Version, "rootCmd should have a version")
275+
}

0 commit comments

Comments
 (0)