Skip to content

Commit 294dd29

Browse files
committed
Fix for --version in osctrl-api and osctrl-cli
1 parent 203b5e8 commit 294dd29

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

.goreleaser.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ builds:
6161
- arm64
6262
ldflags:
6363
- -s -w
64-
- -X main.version={{.Version}}
65-
- -X main.commit={{.Commit}}
66-
- -X main.date={{.Date}}
64+
- -X main.buildVersion={{.Version}}
65+
- -X main.buildCommit={{.Commit}}
66+
- -X main.buildDate={{.Date}}
6767

6868
- id: osctrl-cli
6969
main: ./cmd/cli
@@ -80,9 +80,9 @@ builds:
8080
- arm64
8181
ldflags:
8282
- -s -w
83-
- -X main.version={{.Version}}
84-
- -X main.commit={{.Commit}}
85-
- -X main.date={{.Date}}
83+
- -X main.buildVersion={{.Version}}
84+
- -X main.buildCommit={{.Commit}}
85+
- -X main.buildDate={{.Date}}
8686

8787
archives:
8888
- name_template: >-

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ admin-static:
6060

6161
# Build API
6262
api:
63-
go build -o $(OUTPUT)/$(API_NAME) $(API_CODE)
63+
go build $(BUILD_ARGS) -o $(OUTPUT)/$(API_NAME) $(API_CODE)
6464

6565
# Build API statically
6666
api-static:
67-
go build $(STATIC_ARGS) -o $(OUTPUT)/$(API_NAME) -a $(API_CODE)
67+
go build $(BUILD_ARGS) $(STATIC_ARGS) -o $(OUTPUT)/$(API_NAME) -a $(API_CODE)
6868

6969
# Build the CLI
7070
cli:
71-
go build -o $(OUTPUT)/$(CLI_NAME) $(CLI_CODE)
71+
go build $(BUILD_ARGS) -o $(OUTPUT)/$(CLI_NAME) $(CLI_CODE)
7272

7373
# Build the CLI statically
7474
cli-static:
75-
go build $(STATIC_ARGS) -o $(OUTPUT)/$(CLI_NAME) -a $(CLI_CODE)
75+
go build $(BUILD_ARGS) $(STATIC_ARGS) -o $(OUTPUT)/$(CLI_NAME) -a $(CLI_CODE)
7676

7777
# Clean the dist directory
7878
clean-dist:
@@ -292,7 +292,7 @@ release:
292292
release-test:
293293
make release-build
294294
@echo "Testing built binaries..."
295-
@for binary in dist/osctrl-*; do \
295+
@for binary in $(DIST)/osctrl-*; do \
296296
if [ -f "$$binary" ] && [ -x "$$binary" ]; then \
297297
echo "Testing $$binary"; \
298298
$$binary --version || $$binary version || echo "No version flag available"; \

cmd/api/main.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4956
const (
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
}

cmd/cli/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ const (
3535
defaultApiConfigFile = projectName + "-api.json"
3636
)
3737

38+
// Build-time metadata (overridden via -ldflags "-X main.buildVersion=... -X main.buildCommit=... -X main.buildDate=...")
39+
var (
40+
buildVersion = appVersion
41+
buildCommit = "unknown"
42+
buildDate = "unknown"
43+
)
44+
3845
const (
3946
// Values for output format
4047
jsonFormat = "json"
@@ -1875,9 +1882,19 @@ func main() {
18751882
app = cli.NewApp()
18761883
app.Name = appName
18771884
app.Usage = appUsage
1878-
app.Version = appVersion
1885+
app.Version = buildVersion
18791886
app.Description = appDescription
18801887
app.Flags = flags
1888+
// Customize version output (supports `--version` and `version` command)
1889+
cli.VersionPrinter = func(c *cli.Context) {
1890+
fmt.Printf("%s version=%s commit=%s date=%s\n", appName, buildVersion, buildCommit, buildDate)
1891+
}
1892+
// Add -v alias to the global --version flag
1893+
cli.VersionFlag = &cli.BoolFlag{
1894+
Name: "version",
1895+
Aliases: []string{"v"},
1896+
Usage: "Print version information",
1897+
}
18811898
app.Commands = commands
18821899
app.Action = cliAction
18831900
if err := app.Run(os.Args); err != nil {

0 commit comments

Comments
 (0)