Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit b02a6a0

Browse files
committed
Embed version in binary
1 parent bb36292 commit b02a6a0

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
TAGS: ${{ github.ref_name }},${{ github.sha }},latest
3232
shell: bash
3333
run: |
34+
go generate -x ./...
3435
./build.sh "$VERSION"
3536
export KO_DOCKER_REPO="ghcr.io/${GITHUB_REPOSITORY}"
3637
ko build --bare --platform=all --tags=$TAGS .

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ debug
2222

2323
mikrotik-exporter
2424
test-config.yml
25+
version.go

main.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:generate sh -c "printf 'package main\nconst appVersion = \"%s\"' $VERSION > version.go"
2+
13
package main
24

35
import (
@@ -8,6 +10,7 @@ import (
810
"mikrotik-exporter/config"
911
"net/http"
1012
"os"
13+
"runtime/debug"
1114

1215
"github.com/prometheus/client_golang/prometheus"
1316
"github.com/prometheus/client_golang/prometheus/collectors"
@@ -63,19 +66,26 @@ var (
6366

6467
cfg *config.Config
6568

66-
appVersion = "DEVELOPMENT"
67-
shortSha = "0xDEADBEEF"
69+
vcsRevision = "0xDEADBEEF"
6870
)
6971

7072
func init() {
73+
bi, ok := debug.ReadBuildInfo()
74+
if ok {
75+
for _, s := range bi.Settings {
76+
if s.Key == "vcs.revision" {
77+
vcsRevision = s.Value
78+
}
79+
}
80+
}
7181
prometheus.MustRegister(version.NewCollector("mikrotik_exporter"))
7282
}
7383

7484
func main() {
7585
flag.Parse()
7686

7787
if *ver {
78-
fmt.Printf("\nVersion: %s\nShort SHA: %s\n\n", appVersion, shortSha)
88+
fmt.Printf("Version: %s\nSHA: %s\n", appVersion, vcsRevision)
7989
os.Exit(0)
8090
}
8191

0 commit comments

Comments
 (0)