Skip to content

Commit 6aedc11

Browse files
committed
Add a Makefile with a few targets. The default is 'build'. The build
target calls the new version-at-commit.sh script which will automatically populate the version variable inside the Headscale binary. Once we start tagging releases on the git tree, that will come in handy. The Makefile also has a 'test' target (does nothing yet, no tests yet) and a 'dev' target, which runs linters, tests, and finally builds.
1 parent 6fa8400 commit 6aedc11

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Calculate version
2+
version = $(shell ./scripts/version-at-commit.sh)
3+
4+
build:
5+
go build -ldflags "-s -w -X main.version=$(version)" cmd/headscale/headscale.go
6+
7+
dev: lint test build
8+
9+
test:
10+
go test -coverprofile=coverage.out
11+
12+
coverprofile_func:
13+
go tool cover -func=coverage.out
14+
15+
coverprofile_html:
16+
go tool cover -html=coverage.out
17+
18+
lint:
19+
golint
20+
golangci-lint run
21+
22+
compress: build
23+
upx --brute cmd/headscale/headscale
24+

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ Suggestions/PRs welcomed!
4242

4343
1. Compile the headscale binary
4444
```shell
45-
go build cmd/headscale/headscale.go
45+
make
4646
```
4747

48-
2. Get youself a PostgreSQL DB running (yes, [I know](https://tailscale.com/blog/an-unlikely-database-migration/))
48+
2. Get yourself a PostgreSQL DB running (yes, [I know](https://tailscale.com/blog/an-unlikely-database-migration/))
4949

5050
```shell
5151
docker run --name headscale -e POSTGRES_DB=headscale -e \
5252
POSTGRES_USER=foo -e POSTGRES_PASSWORD=bar -p 5432:5432 -d postgres
5353
```
5454

55-
3. Sort some stuff up (headscale Wireguard keys & the config.json file)
55+
3. Set some stuff up (headscale Wireguard keys & the config.json file)
5656
```shell
5757
wg genkey > private.key
5858
wg pubkey < private.key > public.key # not needed

cmd/headscale/headscale.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"tailscale.com/tailcfg"
1818
)
1919

20-
const version = "0.1"
20+
var version = "dev"
2121

2222
var versionCmd = &cobra.Command{
2323
Use: "version",

scripts/version-at-commit.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
set -e -o pipefail
4+
commit="$1"
5+
versionglob="v[0-9].[0-9]*.[0-9]*"
6+
devsuffix=".dev"
7+
if [ -z "$commit" ]; then
8+
commit=`git log -n1 --first-parent "--format=format:%h"`
9+
fi
10+
11+
# automatically assign version
12+
#
13+
# handles the following cases:
14+
#
15+
# 0. no tags on the repository. Print "dev".
16+
#
17+
# 1. no local modifications and commit is directly tagged. Print tag.
18+
#
19+
# 2. no local modifications and commit is not tagged. Take greatest version tag in repo X.Y.Z and assign X.Y.(Z+1). Print that + $devsuffix + $timestamp.
20+
#
21+
# 3. local modifications. Print "dev".
22+
23+
tags=$(git tag)
24+
if [[ -z "$tags" ]]; then
25+
echo "dev"
26+
elif `git diff --quiet 2>/dev/null`; then
27+
tagged=$(git tag --points-at "$commit")
28+
if [[ -n "$tagged" ]] ; then
29+
echo $tagged
30+
else
31+
nearest_tag=$(git describe --tags --abbrev=0 --match "$versionglob" "$commit")
32+
v=$(echo $nearest_tag | perl -pe 's/(\d+)$/$1+1/e')
33+
isodate=$(TZ=UTC git log -n1 --format=%cd --date=iso "$commit")
34+
ts=$(TZ=UTC date --date="$isodate" "+%Y%m%d%H%M%S")
35+
echo "${v}${devsuffix}${ts}"
36+
fi
37+
else
38+
echo "dev"
39+
fi

0 commit comments

Comments
 (0)