Skip to content

Commit df4c2ba

Browse files
committed
feat: add version flag with ldflags injection and git fallback
1 parent fe5ab44 commit df4c2ba

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ builds:
1111
- amd64
1212
- arm64
1313
ldflags:
14-
- -s -w
14+
- -s -w -X github.com/warriorscode/deck.Version={{ .Version }}
1515

1616
archives:
1717
- formats:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Build binary
22
build:
3-
go build -o bin/deck ./cmd/deck
3+
go build -ldflags="-s -w -X github.com/warriorscode/deck.Version=$$(git describe --tags)" -o bin/deck ./cmd/deck
44

55
## Run tests
66
test:

cmd/deck/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/spf13/cobra"
1414

15+
deck "github.com/warriorscode/deck"
1516
"github.com/warriorscode/deck/config"
1617
"github.com/warriorscode/deck/engine"
1718
"github.com/warriorscode/deck/status"
@@ -21,8 +22,9 @@ var configFile string
2122

2223
func main() {
2324
root := &cobra.Command{
24-
Use: "deck",
25-
Short: "Lightweight local dev orchestrator",
25+
Use: "deck",
26+
Short: "Lightweight local dev orchestrator",
27+
Version: deck.Version,
2628
}
2729

2830
root.PersistentFlags().StringVarP(&configFile, "file", "f", "deck.yaml", "config file path")

version.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package deck
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
)
7+
8+
// Version is injected at build time via ldflags:
9+
//
10+
// go build -ldflags="-s -w -X github.com/warriorscode/deck.Version=$(git describe --tags)" ./cmd/deck
11+
var Version = ""
12+
13+
func init() {
14+
if Version == "" {
15+
if out, _ := exec.Command("git", "describe", "--tags").Output(); len(out) > 0 {
16+
Version = strings.TrimSpace(string(out))
17+
} else {
18+
Version = "dev"
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)