Skip to content

Commit b70e029

Browse files
committed
Get version directly from git tags
1 parent c90818b commit b70e029

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/main.go export-subst

Makefile

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
BIN := yubikey-touch-detector
2-
VERSION := $(shell sed -nr 's/const appVersion\s*=\s*"(.*)"/\1/p' main.go)
2+
VERSION := $(shell git describe --tags)
33

44
PREFIX ?= /usr
55
LIB_DIR = $(DESTDIR)$(PREFIX)/lib
@@ -35,25 +35,15 @@ clean:
3535
rm -rf vendor
3636

3737
.PHONY: dist
38-
dist: clean vendor build
39-
$(eval TMP := $(shell mktemp -d))
40-
mkdir "$(TMP)/$(BIN)-$(VERSION)"
41-
cp -r * "$(TMP)/$(BIN)-$(VERSION)"
42-
(cd "$(TMP)" && tar -cvzf "$(BIN)-$(VERSION)-src.tar.gz" "$(BIN)-$(VERSION)")
43-
44-
mkdir "$(TMP)/$(BIN)-$(VERSION)-linux64"
45-
cp "$(BIN)" "$(BIN).service" "$(BIN).socket" LICENSE README.md "$(TMP)/$(BIN)-$(VERSION)-linux64"
46-
(cd "$(TMP)" && tar -cvzf "$(BIN)-$(VERSION)-linux64.tar.gz" "$(BIN)-$(VERSION)-linux64")
47-
38+
dist: clean vendor
4839
mkdir -p dist
49-
mv "$(TMP)/$(BIN)-$(VERSION)"-*.tar.gz dist
5040
git archive -o "dist/$(BIN)-$(VERSION).tar.gz" --format tar.gz --prefix "$(BIN)-$(VERSION)/" "$(VERSION)"
41+
git archive -o "dist/$(BIN)-$(VERSION)-src.tar.gz" --format tar.gz $$(find vendor -type f -printf '--prefix=$(BIN)-$(VERSION)/%h/ --add-file=%p ') --prefix "$(BIN)-$(VERSION)/" "$(VERSION)"
5142

5243
for file in dist/*; do \
5344
gpg --detach-sign --armor "$$file"; \
5445
done
5546

56-
rm -rf "$(TMP)"
5747
rm -f "dist/$(BIN)-$(VERSION).tar.gz"
5848

5949
.PHONY: install

main.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"fmt"
66
"os"
7+
"os/exec"
78
"os/signal"
89
"path"
910
"path/filepath"
@@ -18,8 +19,6 @@ import (
1819
"github.com/maximbaz/yubikey-touch-detector/notifier"
1920
)
2021

21-
const appVersion = "1.12.3"
22-
2322
func main() {
2423
truthyValues := map[string]bool{"true": true, "yes": true, "1": true}
2524

@@ -42,7 +41,7 @@ func main() {
4241
flag.Parse()
4342

4443
if version {
45-
fmt.Println("YubiKey touch detector version:", appVersion)
44+
fmt.Println("YubiKey touch detector version:", appVersion())
4645
os.Exit(0)
4746
}
4847

@@ -152,3 +151,15 @@ func setupExitSignalWatch(exits *sync.Map) {
152151
log.Debug("Stopping YubiKey touch detector")
153152
os.Exit(0)
154153
}
154+
155+
func appVersion() string {
156+
version := "$Format:%(describe)$"
157+
if strings.HasPrefix(version, "$") {
158+
out, err := exec.Command("git", "describe", "--tags").Output()
159+
if err != nil {
160+
panic(fmt.Sprintf("Failed to determine version using 'git describe': %v", err))
161+
}
162+
version = strings.TrimSpace(string(out))
163+
}
164+
return version
165+
}

0 commit comments

Comments
 (0)