Skip to content

Commit 691b193

Browse files
committed
Reworked the version subcommand
1 parent 7ee026f commit 691b193

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

.goreleaser.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ builds:
3838
flags:
3939
- -trimpath
4040
ldflags:
41-
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} -X main.builtBy=goreleaser -X main.treeState={{ .IsGitDirty }}
41+
- -s
42+
- -w
43+
- -X github.com/hilli/go-kef-w2/cmd/kefw2/cmd.Version={{.Version}}
44+
- -X github.com/hilli/go-kef-w2/cmd/kefw2/cmd.GitCommit={{.Commit}}
45+
- -X github.com/hilli/go-kef-w2/cmd/kefw2/cmd.GitBranch=main
46+
- -X github.com/hilli/go-kef-w2/cmd/kefw2/cmd.BuildDate={{ .Date }}
4247

4348
archives:
4449
- formats: [ tar.gz ]

Taskfile.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,24 @@
55

66
version: '3'
77

8+
vars:
9+
GIT_COMMIT:
10+
sh: git rev-parse HEAD
11+
GIT_SHA:
12+
sh: git rev-parse --short HEAD
13+
GIT_BRANCH:
14+
sh: git branch --show-current
15+
LDFLAGS: >-
16+
-X github.com/hilli/go-kef-w2/cmd/kefw2/cmd.Version=dev-{{.GIT_SHA}}
17+
-X github.com/hilli/go-kef-w2/cmd/kefw2/cmd.GitCommit={{.GIT_COMMIT}}
18+
-X github.com/hilli/go-kef-w2/cmd/kefw2/cmd.GitBranch={{.GIT_BRANCH}}
19+
-X github.com/hilli/go-kef-w2/cmd/kefw2/cmd.BuildDate={{now | date "2006-01-02T15:04:05Z07:00"}}
820
921
tasks:
1022
build:
1123
desc: Build binaries
1224
cmds:
13-
- go build -ldflags="-X github.com/hilli/go-kef-w2/cmd.Version={{.GIT_COMMIT}}" -o bin/kefw2{{exeExt}} cmd/kefw2/kefw2.go
25+
- go build -ldflags='-s -w {{.LDFLAGS}}' -o bin/kefw2{{exeExt}} cmd/kefw2/kefw2.go
1426
aliases: [b]
1527
sources:
1628
- cmd/kefw2/**/*.go
@@ -49,7 +61,7 @@ tasks:
4961
desc: Run kefw2
5062
aliases: [r]
5163
cmds:
52-
- go run -ldflags="-X github.com/hilli/go-kef-w2/cmd.Version=go-kef-w2-git" cmd/kefw2/kefw2.go {{.CLI_ARGS}}
64+
- go run -ldflags='{{.LDFLAGS}}' cmd/kefw2/kefw2.go {{.CLI_ARGS}}
5365

5466
all:
5567
desc: Run all tasks

cmd/kefw2/cmd/root.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"fmt"
2626
"os"
2727
"path/filepath"
28-
"runtime/debug"
2928

3029
"github.com/hilli/go-kef-w2/kefw2"
3130
log "github.com/sirupsen/logrus"
@@ -41,6 +40,10 @@ var (
4140
speakers []kefw2.KEFSpeaker
4241
defaultSpeaker *kefw2.KEFSpeaker
4342
currentSpeaker *kefw2.KEFSpeaker
43+
Version string // Git Tag
44+
GitCommit string // Git commit SHA
45+
GitBranch string // Git branch
46+
BuildDate string // Build date
4447
)
4548

4649
// rootCmd represents the base command when called without any subcommands
@@ -58,26 +61,12 @@ var VersionCmd = &cobra.Command{
5861
Long: "Print the version number of kefw2",
5962
Run: func(cmd *cobra.Command, args []string) {
6063
fmt.Println("kefw2: Command line tool for controlling KEF's W2 platform speakers")
61-
var GitCommit string
62-
var BuildDate string
63-
if info, ok := debug.ReadBuildInfo(); ok {
64-
modified := false
65-
for _, v := range info.Settings {
66-
switch v.Key {
67-
case "vcs.revision":
68-
GitCommit = v.Value
69-
case "vcs.time":
70-
BuildDate = v.Value
71-
case "vcs.modified":
72-
modified = true
73-
}
74-
if modified {
75-
GitCommit += "-dirty"
76-
}
77-
}
78-
}
7964
headerPrinter.Print("Version: ")
65+
contentPrinter.Printf("%s\n", Version)
66+
headerPrinter.Print("Git commit: ")
8067
contentPrinter.Printf("%s\n", GitCommit)
68+
headerPrinter.Print("Git branch: ")
69+
contentPrinter.Printf("%s\n", GitBranch)
8170
headerPrinter.Print("Build date: ")
8271
contentPrinter.Printf("%s\n", BuildDate)
8372
},

0 commit comments

Comments
 (0)