Skip to content

Commit cefcc33

Browse files
committed
Add versioning support and detailed CLI version command
- Introduced embedded version metadata (version, commit, and build date) during build time using `-ldflags`. - Updated README with instructions for generating release binaries with version info. - Added `version` command to display detailed build metadata. - Updated root command to support `--version` and provide a summarized version output. - Refactored internal version handling into a dedicated `internal/version.go` package.
1 parent 10514c4 commit cefcc33

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,35 @@ A practical CLI to manage your `~/.puma-dev` domain mappings. Because editing ra
1616
## Install
1717

1818
```bash
19-
go install github.com/yourusername/pumadevctl@latest
19+
go install github.com/rolling-space/pumadevctl@latest
20+
```
21+
22+
## Versioning and releases
23+
24+
This CLI embeds version metadata at build time. By default, local builds display `dev`.
25+
26+
To produce a release binary with version, commit, and build date:
27+
28+
```bash
29+
VERSION=v0.4.0
30+
COMMIT=$(git rev-parse --short HEAD)
31+
DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
32+
33+
# Build to ./exe as per project guidelines
34+
mkdir -p ./exe
35+
GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) \
36+
go build -ldflags "-s -w \
37+
-X github.com/rolling-space/pumadevctl/internal.Version=$VERSION \
38+
-X github.com/rolling-space/pumadevctl/internal.Commit=$COMMIT \
39+
-X github.com/rolling-space/pumadevctl/internal.Date=$DATE" \
40+
-o ./exe/pumadevctl .
41+
```
42+
43+
Check the result:
44+
45+
```bash
46+
./exe/pumadevctl --version # short summary
47+
./exe/pumadevctl version # detailed info
2048
```
2149

2250
## Usage

cmd/root.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"runtime"
88
"time"
99

10-
"github.com/spf13/cobra"
1110
"github.com/rolling-space/pumadevctl/internal"
11+
"github.com/spf13/cobra"
1212
)
1313

1414
var (
@@ -19,13 +19,11 @@ var (
1919
portMinFlag int
2020
portMaxFlag int
2121
portBlockSize int
22-
version = "0.3.0"
2322
)
2423

2524
var rootCmd = &cobra.Command{
26-
Use: "pumadevctl",
27-
Short: "Manage puma-dev mappings (~/.puma-dev) with CRUD, list, validate, cleanup",
28-
Version: version,
25+
Use: "pumadevctl",
26+
Short: "Manage puma-dev mappings (~/.puma-dev) with CRUD, list, validate, cleanup",
2927
}
3028

3129
func Execute() {
@@ -36,6 +34,10 @@ func Execute() {
3634
}
3735

3836
func init() {
37+
// Configure version for Cobra (supports `--version` and `pumadevctl version`)
38+
rootCmd.Version = internal.VersionSummary()
39+
rootCmd.SetVersionTemplate("{{.Version}}\n")
40+
3941
home, _ := os.UserHomeDir()
4042
defaultDir := filepath.Join(home, ".puma-dev")
4143

cmd/version.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/rolling-space/pumadevctl/internal"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func init() {
11+
cmd := &cobra.Command{
12+
Use: "version",
13+
Short: "Show detailed version information",
14+
Run: func(cmd *cobra.Command, args []string) {
15+
fmt.Println(internal.VersionLong())
16+
},
17+
}
18+
rootCmd.AddCommand(cmd)
19+
}

internal/version.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package internal
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
// Version metadata populated at build time via -ldflags. Defaults are for dev builds.
9+
var (
10+
// Version is the semver for the release. e.g. v0.4.0. Defaults to "dev".
11+
Version = "dev"
12+
// Commit is the short git commit SHA for reproducibility.
13+
Commit = ""
14+
// Date is the build timestamp in UTC (ISO-8601), e.g. 2025-10-20T04:21:00Z.
15+
Date = ""
16+
)
17+
18+
// VersionSummary returns a human-friendly single-line version suitable for Cobra's Version field.
19+
func VersionSummary() string {
20+
base := Version
21+
meta := ""
22+
if Commit != "" {
23+
meta = Commit
24+
}
25+
if Date != "" {
26+
if meta != "" {
27+
meta = fmt.Sprintf("%s, %s", meta, Date)
28+
} else {
29+
meta = Date
30+
}
31+
}
32+
if meta != "" {
33+
return fmt.Sprintf("%s (%s, %s/%s)", base, meta, runtime.GOOS, runtime.GOARCH)
34+
}
35+
return fmt.Sprintf("%s (%s/%s)", base, runtime.GOOS, runtime.GOARCH)
36+
}
37+
38+
// VersionLong returns a multi-line detailed version string.
39+
func VersionLong() string {
40+
return fmt.Sprintf("version: %s\ncommit: %s\ndate: %s\ngo: %s %s/%s",
41+
Version,
42+
valueOrDash(Commit),
43+
valueOrDash(Date),
44+
runtime.Version(),
45+
runtime.GOOS,
46+
runtime.GOARCH,
47+
)
48+
}
49+
50+
func valueOrDash(s string) string {
51+
if s == "" {
52+
return "-"
53+
}
54+
return s
55+
}

0 commit comments

Comments
 (0)