Skip to content

Commit 203b5e8

Browse files
authored
Merge pull request #676 from jmpsec/version-cli-admin-fix
Fix for `--version` in `osctrl-admin`
2 parents 4020922 + 2b81c1f commit 203b5e8

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

.goreleaser.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ builds:
4343
- arm64
4444
ldflags:
4545
- -s -w
46-
- -X main.version={{.Version}}
47-
- -X main.commit={{.Commit}}
48-
- -X main.date={{.Date}}
46+
- -X main.buildVersion={{.Version}}
47+
- -X main.buildCommit={{.Commit}}
48+
- -X main.buildDate={{.Date}}
4949

5050
- id: osctrl-api
5151
main: ./cmd/api

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ tls-static:
5252

5353
# Build Admin UI
5454
admin:
55-
go build -o $(OUTPUT)/$(ADMIN_NAME) $(ADMIN_CODE)
55+
go build $(BUILD_ARGS) -o $(OUTPUT)/$(ADMIN_NAME) $(ADMIN_CODE)
5656

5757
# Build Admin UI statically
5858
admin-static:
59-
go build $(STATIC_ARGS) -o $(OUTPUT)/$(ADMIN_NAME) -a $(ADMIN_CODE)
59+
go build $(BUILD_ARGS) $(STATIC_ARGS) -o $(OUTPUT)/$(ADMIN_NAME) -a $(ADMIN_CODE)
6060

6161
# Build API
6262
api:

cmd/admin/main.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ const (
7373
defaultInactive int = 72
7474
)
7575

76+
// Build-time metadata (overridden via -ldflags "-X main.buildVersion=... -X main.buildCommit=... -X main.buildDate=...")
77+
var (
78+
buildVersion = serviceVersion
79+
buildCommit = "unknown"
80+
buildDate = "unknown"
81+
)
82+
7683
// Global general variables
7784
var (
7885
err error
@@ -649,9 +656,19 @@ func main() {
649656
app = cli.NewApp()
650657
app.Name = serviceName
651658
app.Usage = appDescription
652-
app.Version = serviceVersion
659+
app.Version = buildVersion
653660
app.Description = appDescription
654661
app.Flags = flags
662+
// Customize version output (supports `--version` and `version` command)
663+
cli.VersionPrinter = func(c *cli.Context) {
664+
fmt.Printf("%s version=%s commit=%s date=%s\n", serviceName, buildVersion, buildCommit, buildDate)
665+
}
666+
// Add -v alias to the global --version flag
667+
cli.VersionFlag = &cli.BoolFlag{
668+
Name: "version",
669+
Aliases: []string{"v"},
670+
Usage: "Print version information",
671+
}
655672
// Define this command for help to exit when help flag is passed
656673
app.Commands = []*cli.Command{
657674
{
@@ -662,13 +679,19 @@ func main() {
662679
},
663680
},
664681
}
665-
app.Action = cliAction
682+
// Start service only for default action; version/help won't trigger this
683+
app.Action = func(c *cli.Context) error {
684+
if err := cliAction(c); err != nil {
685+
return err
686+
}
687+
// Initialize service logger
688+
initializeLoggers(flagParams.ConfigValues)
689+
// Service starts!
690+
osctrlAdminService()
691+
return nil
692+
}
666693
if err := app.Run(os.Args); err != nil {
667694
fmt.Printf("app.Run error: %s", err.Error())
668695
os.Exit(1)
669696
}
670-
// Initialize service logger
671-
initializeLoggers(flagParams.ConfigValues)
672-
// Service starts!
673-
osctrlAdminService()
674697
}

cmd/tls/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ func main() {
419419
app.Version = buildVersion
420420
app.Description = appDescription
421421
app.Flags = flags
422-
423422
// Customize version output (supports `--version` and `version` command)
424423
cli.VersionPrinter = func(c *cli.Context) {
425424
fmt.Printf("%s version=%s commit=%s date=%s\n", serviceName, buildVersion, buildCommit, buildDate)
@@ -430,7 +429,6 @@ func main() {
430429
Aliases: []string{"v"},
431430
Usage: "Print version information",
432431
}
433-
434432
// Define commands
435433
app.Commands = []*cli.Command{
436434
{

0 commit comments

Comments
 (0)