Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ builds:
- arm64
ldflags:
- -s -w
- -X main.version={{.Version}}
- -X main.commit={{.Commit}}
- -X main.date={{.Date}}
- -X main.buildVersion={{.Version}}
- -X main.buildCommit={{.Commit}}
- -X main.buildDate={{.Date}}

- id: osctrl-cli
main: ./cmd/cli
Expand All @@ -80,9 +80,9 @@ builds:
- arm64
ldflags:
- -s -w
- -X main.version={{.Version}}
- -X main.commit={{.Commit}}
- -X main.date={{.Date}}
- -X main.buildVersion={{.Version}}
- -X main.buildCommit={{.Commit}}
- -X main.buildDate={{.Date}}

archives:
- name_template: >-
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ admin-static:

# Build API
api:
go build -o $(OUTPUT)/$(API_NAME) $(API_CODE)
go build $(BUILD_ARGS) -o $(OUTPUT)/$(API_NAME) $(API_CODE)

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

# Build the CLI
cli:
go build -o $(OUTPUT)/$(CLI_NAME) $(CLI_CODE)
go build $(BUILD_ARGS) -o $(OUTPUT)/$(CLI_NAME) $(CLI_CODE)

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

# Clean the dist directory
clean-dist:
Expand Down Expand Up @@ -292,7 +292,7 @@ release:
release-test:
make release-build
@echo "Testing built binaries..."
@for binary in dist/osctrl-*; do \
@for binary in $(DIST)/osctrl-*; do \
if [ -f "$$binary" ] && [ -x "$$binary" ]; then \
echo "Testing $$binary"; \
$$binary --version || $$binary version || echo "No version flag available"; \
Expand Down
35 changes: 29 additions & 6 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ const (
defaultRefresh int = 300
)

// Build-time metadata (overridden via -ldflags "-X main.buildVersion=... -X main.buildCommit=... -X main.buildDate=...")
var (
buildVersion = serviceVersion
buildCommit = "unknown"
buildDate = "unknown"
)

// Paths
const (
// HTTP health path
Expand Down Expand Up @@ -447,9 +454,19 @@ func main() {
app = cli.NewApp()
app.Name = serviceName
app.Usage = appDescription
app.Version = serviceVersion
app.Version = buildVersion
app.Description = appDescription
app.Flags = flags
// Customize version output (supports `--version` and `version` command)
cli.VersionPrinter = func(c *cli.Context) {
fmt.Printf("%s version=%s commit=%s date=%s\n", serviceName, buildVersion, buildCommit, buildDate)
}
// Add -v alias to the global --version flag
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "Print version information",
}
// Define this command for help to exit when help flag is passed
app.Commands = []*cli.Command{
{
Expand All @@ -460,13 +477,19 @@ func main() {
},
},
}
app.Action = cliAction
// Start service only for default action; version/help won't trigger this
app.Action = func(c *cli.Context) error {
if err := cliAction(c); err != nil {
return err
}
// Initialize service logger
initializeLoggers(flagParams.ConfigValues)
// Run the service
osctrlAPIService()
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("app.Run error: %s", err.Error())
os.Exit(1)
}
// Initialize service logger
initializeLoggers(flagParams.ConfigValues)
// Run the service
osctrlAPIService()
}
19 changes: 18 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ const (
defaultApiConfigFile = projectName + "-api.json"
)

// Build-time metadata (overridden via -ldflags "-X main.buildVersion=... -X main.buildCommit=... -X main.buildDate=...")
var (
buildVersion = appVersion
buildCommit = "unknown"
buildDate = "unknown"
)

const (
// Values for output format
jsonFormat = "json"
Expand Down Expand Up @@ -1875,9 +1882,19 @@ func main() {
app = cli.NewApp()
app.Name = appName
app.Usage = appUsage
app.Version = appVersion
app.Version = buildVersion
app.Description = appDescription
app.Flags = flags
// Customize version output (supports `--version` and `version` command)
cli.VersionPrinter = func(c *cli.Context) {
fmt.Printf("%s version=%s commit=%s date=%s\n", appName, buildVersion, buildCommit, buildDate)
}
// Add -v alias to the global --version flag
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "Print version information",
}
app.Commands = commands
app.Action = cliAction
if err := app.Run(os.Args); err != nil {
Expand Down
Loading