Skip to content

Commit af86e31

Browse files
committed
feat: derive build version from git tag + cleanup
1 parent 0ed1c79 commit af86e31

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# Go workspace file
2121
go.work
2222

23+
.env
24+
2325
bin/
2426

2527
dist/

.goreleaser.yaml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
version: 2
12
before:
23
hooks:
34
- go mod tidy
45
builds:
56
- env:
67
- CGO_ENABLED=0
8+
flags:
9+
- -trimpath
10+
ldflags:
11+
- -s -w -X github.com/AzraelSec/glock/cmd/cli/commands.Version={{ .Version }}
12+
mod_timestamp: "{{ .CommitTimestamp }}"
713
goos:
814
- linux
915
# - windows
@@ -12,25 +18,22 @@ builds:
1218
binary: glock
1319

1420
archives:
15-
- format: tar.gz
21+
- formats: ["tar.gz"]
1622
name_template: >-
1723
{{ .ProjectName }}_
24+
{{- .Version }}_
1825
{{- title .Os }}_
1926
{{- if eq .Arch "amd64" }}x86_64
2027
{{- else if eq .Arch "386" }}i386
2128
{{- else }}{{ .Arch }}{{ end }}
2229
{{- if .Arm }}v{{ .Arm }}{{ end }}
23-
# # use zip for windows archives
24-
# format_overrides:
25-
# - goos: windows
26-
# format: zip
2730
checksum:
28-
name_template: 'checksums.txt'
31+
name_template: "{{ .ProjectName }}_{{ .Version }}_checksums.txt"
2932
snapshot:
30-
name_template: "{{ incpatch .Version }}-next"
33+
version_template: "{{ incpatch .Version }}-next"
3134
changelog:
3235
sort: asc
3336
filters:
3437
exclude:
35-
- '^docs:'
36-
- '^test:'
38+
- "^docs:"
39+
- "^test:"

Makefile

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
1-
.PHONY: test build install run compile
1+
include .env
2+
3+
.PHONY: test build install run clean compile release
4+
5+
VERSION ?= $(patsubst v%,%,$(shell git describe --tags --exact-match 2>/dev/null || echo dev))
6+
LD_FLAGS := -X github.com/AzraelSec/glock/cmd/cli/commands.Version=$(VERSION)
7+
RELEASE_TAG := $(if $(filter v%,$(VERSION)),$(VERSION),v$(VERSION))
28

39
test:
410
go test ./...
511

612
build:
7-
go build -o glock cmd/cli/main.go
13+
go build -ldflags "$(LD_FLAGS)" -o glock ./cmd/cli
814

915
install: build
1016
cp glock ~/.local/bin/glock
1117

1218
run:
13-
go run main.go
19+
go run -ldflags "$(LD_FLAGS)" ./cmd/cli
1420

1521
clean:
16-
rm glock
22+
rm -f glock
1723

1824
compile:
1925
echo "Compiling for every OS and Platform"
20-
GOOS=linux GOARCH=arm go build -o bin/glock-linux-arm cmd/cli/main.go
21-
GOOS=linux GOARCH=arm64 go build -o bin/main-linux-arm64 cmd/cli/main.go
26+
GOOS=linux GOARCH=arm go build -ldflags "$(LD_FLAGS)" -o bin/glock-linux-arm ./cmd/cli
27+
GOOS=linux GOARCH=arm64 go build -ldflags "$(LD_FLAGS)" -o bin/glock-linux-arm64 ./cmd/cli
2228

29+
release:
30+
@test -n "$(VERSION)" || (printf "Usage: make release VERSION=0.1.0\n" >&2; exit 1)
31+
@test "$$(git rev-parse --abbrev-ref HEAD)" = "main" || (printf "Release must run from main\n" >&2; exit 1)
32+
@test -z "$$(git status --porcelain)" || (printf "Working tree must be clean\n" >&2; exit 1)
33+
@git fetch origin main --tags
34+
@git pull --ff-only origin main
35+
@if git rev-parse "$(RELEASE_TAG)" >/dev/null 2>&1; then printf "Tag %s already exists\n" "$(RELEASE_TAG)" >&2; exit 1; fi
36+
@if git ls-remote --exit-code --tags origin "refs/tags/$(RELEASE_TAG)" >/dev/null 2>&1; then printf "Remote tag %s already exists\n" "$(RELEASE_TAG)" >&2; exit 1; fi
37+
@go test ./...
38+
@git tag "$(RELEASE_TAG)"
39+
@git push origin main "$(RELEASE_TAG)"
40+
@goreleaser release --clean

cmd/cli/commands/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ import (
1616

1717
const (
1818
CONFIG_PATH_ENV = "GLOCK_CONFIG_PATH"
19-
VERSION = "0.0.1"
2019
CONFIG_FILE_NAME = "glock.yml"
2120
MAX_CONFIG_FILE_DEPTH = 20
2221
)
2322

23+
var Version = "dev"
24+
2425
var rootCmd = &cobra.Command{
2526
Use: "glock",
2627
Short: "Shooting flies with a bazooka \U0001f680",
2728
SilenceUsage: true,
28-
Version: VERSION,
29+
Version: Version,
2930
}
3031

3132
type RedWriter struct {

0 commit comments

Comments
 (0)