Skip to content

Commit 4e607bd

Browse files
feat: add --version flag
Add --version flag support using cobra's built-in version functionality. Version and build date are injected via ldflags at build time (already configured in .goreleaser.yaml), with sensible defaults for dev builds. Closes #52
1 parent df9fdda commit 4e607bd

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

cmd/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ var rootCmd = &cobra.Command{
8181
},
8282
}
8383

84+
// SetVersionInfo sets the version information for the root command.
85+
func SetVersionInfo(version, buildDate string) {
86+
rootCmd.Version = version
87+
rootCmd.SetVersionTemplate(fmt.Sprintf("nomore403 version %s (built %s)\n", version, buildDate))
88+
}
89+
8490
// Execute adds all child commands to the root command and sets flags appropriately.
8591
// This is called by main.main(). It only needs to happen once to the rootCmd.
8692
func Execute() {

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ package main
44

55
import "github.com/devploit/nomore403/cmd"
66

7+
// Version and BuildDate are set via ldflags at build time.
8+
var (
9+
Version = "dev"
10+
BuildDate = "unknown"
11+
)
12+
713
func main() {
14+
cmd.SetVersionInfo(Version, BuildDate)
815
cmd.Execute()
916
}

0 commit comments

Comments
 (0)