@@ -45,6 +45,13 @@ const (
4545 defaultRefresh int = 300
4646)
4747
48+ // Build-time metadata (overridden via -ldflags "-X main.buildVersion=... -X main.buildCommit=... -X main.buildDate=...")
49+ var (
50+ buildVersion = serviceVersion
51+ buildCommit = "unknown"
52+ buildDate = "unknown"
53+ )
54+
4855// Paths
4956const (
5057 // HTTP health path
@@ -447,9 +454,19 @@ func main() {
447454 app = cli .NewApp ()
448455 app .Name = serviceName
449456 app .Usage = appDescription
450- app .Version = serviceVersion
457+ app .Version = buildVersion
451458 app .Description = appDescription
452459 app .Flags = flags
460+ // Customize version output (supports `--version` and `version` command)
461+ cli .VersionPrinter = func (c * cli.Context ) {
462+ fmt .Printf ("%s version=%s commit=%s date=%s\n " , serviceName , buildVersion , buildCommit , buildDate )
463+ }
464+ // Add -v alias to the global --version flag
465+ cli .VersionFlag = & cli.BoolFlag {
466+ Name : "version" ,
467+ Aliases : []string {"v" },
468+ Usage : "Print version information" ,
469+ }
453470 // Define this command for help to exit when help flag is passed
454471 app .Commands = []* cli.Command {
455472 {
@@ -460,13 +477,19 @@ func main() {
460477 },
461478 },
462479 }
463- app .Action = cliAction
480+ // Start service only for default action; version/help won't trigger this
481+ app .Action = func (c * cli.Context ) error {
482+ if err := cliAction (c ); err != nil {
483+ return err
484+ }
485+ // Initialize service logger
486+ initializeLoggers (flagParams .ConfigValues )
487+ // Run the service
488+ osctrlAPIService ()
489+ return nil
490+ }
464491 if err := app .Run (os .Args ); err != nil {
465492 fmt .Printf ("app.Run error: %s" , err .Error ())
466493 os .Exit (1 )
467494 }
468- // Initialize service logger
469- initializeLoggers (flagParams .ConfigValues )
470- // Run the service
471- osctrlAPIService ()
472495}
0 commit comments