From d66a8d462a5bd7a60eb008ef6a1ec994d34e1d78 Mon Sep 17 00:00:00 2001 From: AndrewLester Date: Sat, 15 Jul 2023 18:14:39 -0500 Subject: [PATCH] Build with version number --- .github/workflows/build.yml | 2 ++ Makefile | 4 ++-- cmd/ntpal/main.go | 10 ++++++++++ cmd/site/main.go | 5 ++++- internal/templates/templates/index.tmpl.html | 4 +++- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b0841e0..0623445 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,6 +27,8 @@ jobs: with: name: ntpal-binary-${{ matrix.os }} path: ${{ github.workspace }}/bin/ntpal-${{ matrix.os }} + env: + VERSION: ${github.ref/v/} release: runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index 3b81eaa..9a48098 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,10 @@ SHELL := /bin/bash -f all: ntpal site ntpal: - go build -o bin/ntpal github.com/AndrewLester/ntpal/cmd/ntpal + go build -o bin/ntpal -ldflags "-X main.version=${VERSION}" github.com/AndrewLester/ntpal/cmd/ntpal site: - go build -o bin/site github.com/AndrewLester/ntpal/cmd/site + go build -o bin/site -ldflags "-X main.version=${VERSION}" github.com/AndrewLester/ntpal/cmd/site clean: rm -rf bin diff --git a/cmd/ntpal/main.go b/cmd/ntpal/main.go index 92a45a4..cacf34f 100644 --- a/cmd/ntpal/main.go +++ b/cmd/ntpal/main.go @@ -11,6 +11,8 @@ import ( "github.com/AndrewLester/ntpal/pkg/ntpal" ) +var version string // This is set by the linker + const defaultConfigPath = "/etc/ntp.conf" const defaultDriftPath = "/etc/ntp.drift" @@ -22,6 +24,7 @@ func main() { var query string var noDaemon bool var stop bool + var isVersion bool flag.StringVar(&config, "config", defaultConfigPath, "Path to the NTP config file.") flag.StringVar(&drift, "drift", defaultDriftPath, "Path to the NTP drift file.") flag.StringVar(&query, "query", "", "Address to query.") @@ -29,8 +32,15 @@ func main() { flag.BoolVar(&noDaemon, "no-daemon", false, "Don't run ntpal as a daemon.") flag.BoolVar(&stop, "stop", false, "Stop the ntpal daemon.") flag.BoolVar(&stop, "s", stop, "Stop the ntpal daemon.") + flag.BoolVar(&isVersion, "version", false, "Check the NTPal version.") + flag.BoolVar(&isVersion, "v", isVersion, "Check the NTPal version.") flag.Parse() + if isVersion { + fmt.Println("NTPal", version) + return + } + port := os.Getenv("NTP_PORT") if port == "" { if query != "" { diff --git a/cmd/site/main.go b/cmd/site/main.go index afed9b3..06383bc 100644 --- a/cmd/site/main.go +++ b/cmd/site/main.go @@ -13,6 +13,8 @@ import ( "github.com/AndrewLester/ntpal/pkg/ntpal" ) +var version string // This is set by the linker + type SyncRequest struct { Orig string } @@ -29,7 +31,8 @@ func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { data := map[string]string{ - "Region": strings.ToUpper(os.Getenv("FLY_REGION")), + "Region": strings.ToUpper(os.Getenv("FLY_REGION")), + "Version": version, } // Set these headers to bump performance.now() precision to 5 microseconds diff --git a/internal/templates/templates/index.tmpl.html b/internal/templates/templates/index.tmpl.html index a95eea2..6533a0e 100644 --- a/internal/templates/templates/index.tmpl.html +++ b/internal/templates/templates/index.tmpl.html @@ -264,7 +264,9 @@

According to this server,

{{ if .Region }}

Server hosted in the {{.Region}} region, running - NTPal. + + NTPal {{ if .Version }} {{.Version}} {{ end }} .

{{ end }}