Skip to content

Commit 89d6c96

Browse files
committed
build: version/commit/date
1 parent 7c13b1a commit 89d6c96

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
BUILD_TAG := $(shell git describe --tags --exact-match 2> /dev/null || git rev-parse --short HEAD)
2-
31
.PHONY: build
42
build:
5-
@go build -ldflags "-X main.version=$(BUILD_TAG)" -o howto
3+
@go build -o howto
64

75
.PHONY: test
86
test:

howto.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func howto(out io.Writer, ask ai.AskFunc, args []string, history *History) error
2121
case "-h", "--help":
2222
printUsage(out)
2323
case "-v", "--version":
24-
printVersion(out, version, ai.Conf, history)
24+
ver := versionString()
25+
printVersion(out, ver, ai.Conf, history)
2526
case "-run":
2627
err = runCommand(out, history)
2728
default:
@@ -109,3 +110,13 @@ func execCommand(command string) (string, error) {
109110

110111
return strings.TrimSpace(outb.String()), nil
111112
}
113+
114+
func versionString() string {
115+
var ver string
116+
if version == "dev" {
117+
ver = commit
118+
} else {
119+
ver = version
120+
}
121+
return ver + " (" + date + ")"
122+
}

howto_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Test_howto(t *testing.T) {
3737
if err != nil {
3838
t.Fatalf("Unexpected error: %v", err)
3939
}
40-
if !strings.Contains(out.String(), bold("howto")+" "+version) {
40+
if !strings.Contains(out.String(), bold("howto")+" head (now)") {
4141
t.Errorf("Expected version string, got %q", out.String())
4242
}
4343
})

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import (
99
"github.com/nalgeon/howto/ai"
1010
)
1111

12-
var version string = "latest"
12+
var (
13+
version = "dev"
14+
commit = "head"
15+
date = "now"
16+
)
1317

1418
func main() {
1519
history, err := LoadHistory()

0 commit comments

Comments
 (0)