Skip to content

Commit 6f753ca

Browse files
DAcodedBEATArun Philip
authored andcommitted
feat: add --version flag and version resolution
Add GetVersion() with fallback chain: ldflag tag > git short hash > "dev". Wire goreleaser to inject version at build time via ldflags. Add --version CLI flag for programmatic version checks. Closes #157 Signed-off-by: Arun Philip <arun@corkinc.com>
1 parent aab4294 commit 6f753ca

5 files changed

Lines changed: 60 additions & 1 deletion

File tree

.github/workflows/ci-linux.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,10 @@ jobs:
3838
run: |
3939
go build ./...
4040
go test -v ./...
41+
42+
- name: Verify --version flag
43+
run: |
44+
go build -o tsidp-server .
45+
VERSION=$(./tsidp-server --version)
46+
echo "Version: $VERSION"
47+
[ -n "$VERSION" ] || { echo "ERROR: --version produced empty output"; exit 1; }

.github/workflows/release.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,16 @@ jobs:
5050
version: '~> v2'
5151
args: ${{ steps.goreleaser-args.outputs.args }}
5252
env:
53-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
54+
-
55+
name: Verify --version flag
56+
run: |
57+
BINARY=$(find dist -path '*linux*amd64*' -name 'tsidp' -type f | head -1)
58+
[ -n "$BINARY" ] || { echo "ERROR: could not find linux/amd64 binary in dist/"; exit 1; }
59+
VERSION=$("$BINARY" --version)
60+
echo "Version: $VERSION"
61+
[ -n "$VERSION" ] || { echo "ERROR: --version produced empty output"; exit 1; }
62+
if [ "${GITHUB_EVENT_NAME}" != "pull_request" ]; then
63+
EXPECTED="${GITHUB_REF_NAME}"
64+
[ "$VERSION" = "$EXPECTED" ] || { echo "ERROR: expected $EXPECTED got $VERSION"; exit 1; }
65+
fi

.goreleaser.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ version: 2
33
builds:
44
- env:
55
- CGO_ENABLED=0
6+
ldflags:
7+
- -s -w
8+
- -X github.com/tailscale/tsidp/server.version={{.Version}}
69
goos:
710
- linux
811
- darwin

server/version.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Tailscale Inc & AUTHORS
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
package server
5+
6+
import "runtime/debug"
7+
8+
// version is set at build time via ldflags:
9+
// go build -ldflags "-X github.com/tailscale/tsidp/server.version=v1.2.3"
10+
var version string
11+
12+
// GetVersion returns the version string for tsidp.
13+
// Priority: ldflag-injected tag > VCS short hash > "dev".
14+
func GetVersion() string { return cachedVersion }
15+
16+
var cachedVersion = func() string {
17+
if version != "" {
18+
return version
19+
}
20+
if info, ok := debug.ReadBuildInfo(); ok {
21+
for _, s := range info.Settings {
22+
if s.Key == "vcs.revision" && s.Value != "" {
23+
if len(s.Value) > 7 {
24+
return s.Value[:7]
25+
}
26+
return s.Value
27+
}
28+
}
29+
}
30+
return "dev"
31+
}()

tsidp-server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ var (
5252
// extended debugging information
5353
flagDebugAllRequests = flag.Bool("debug-all-requests", envknob.Bool("TSIDP_DEBUG_ALL_REQUESTS"), "capture and print all HTTP requests and responses")
5454
flagDebugTSNet = flag.Bool("debug-tsnet", envknob.Bool("TSIDP_DEBUG_TSNET"), "enable tsnet.Server logging")
55+
56+
flagVersion = flag.Bool("version", false, "print version and exit")
5557
)
5658

5759
// main initializes and starts the tsidp server
5860
func main() {
5961
ctx := context.Background()
6062

6163
flag.Parse()
64+
if *flagVersion {
65+
fmt.Println(server.GetVersion())
66+
os.Exit(0)
67+
}
6268
switch *flagLogLevel {
6369
case "debug":
6470
slog.SetLogLoggerLevel(slog.LevelDebug)

0 commit comments

Comments
 (0)