From a0e5e73c0c2992878699411f677b480602e55ae7 Mon Sep 17 00:00:00 2001 From: darksworm <9987548+darksworm@users.noreply.github.com> Date: Sun, 6 Sep 2026 12:25:51 +0200 Subject: [PATCH] feat: log version on startup --- .github/workflows/publish-image.yml | 2 ++ Dockerfile | 3 ++- main.go | 12 +++++++++++- main_test.go | 26 ++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish-image.yml b/.github/workflows/publish-image.yml index 608eda9..5de515b 100644 --- a/.github/workflows/publish-image.yml +++ b/.github/workflows/publish-image.yml @@ -71,6 +71,8 @@ jobs: ./docker-bake.hcl cwd://${{ steps.meta.outputs.bake-file }} targets: release + set: | + release.args.VERSION=${{ inputs.tag }} push: true # release-please creates the release as a draft so it only becomes visible diff --git a/Dockerfile b/Dockerfile index c47a822..1e65ad1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,8 @@ RUN go mod download COPY *.go ./ ARG TARGETOS=linux ARG TARGETARCH -RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w" -o /out/doormouse . +ARG VERSION=dev +RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -ldflags="-s -w -X main.version=$VERSION" -o /out/doormouse . FROM alpine:3.22 diff --git a/main.go b/main.go index f7040fe..ecae038 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,10 @@ type Logger interface { const tcpScheme = "tcp://" +// version is replaced by the release build. Keep a useful value for local +// builds and tests that do not supply linker flags. +var version = "dev" + // shutdownGrace is how long in-flight requests get to finish on shutdown. const shutdownGrace = 15 * time.Second @@ -1728,6 +1732,10 @@ func (l *StdLogger) Error(msg string, args ...interface{}) { log.Printf("[ERROR] "+msg, args...) } +func logStartup(logger Logger, version string) { + logger.Info("Starting doormouse version `%s`", version) +} + // warnUnreadableSSHKeys reports SSH keys the process cannot open. The key is // otherwise read only when a machine has gone idle, so a permissions mistake // stays invisible until the first shutdown silently fails, up to an @@ -1760,6 +1768,9 @@ func warnUnreadableSSHKeys(config *ProxyConfig, logger Logger) { // Main function func main() { + logger := &StdLogger{} + logStartup(logger, version) + if len(os.Args) < 2 { log.Fatal("Usage: doormouse ") } @@ -1768,7 +1779,6 @@ func main() { // Load configuration clock := RealClock{} - logger := &StdLogger{} MigrateConfigFile(configFile, logger) diff --git a/main_test.go b/main_test.go index c2dba46..75445f4 100644 --- a/main_test.go +++ b/main_test.go @@ -324,6 +324,32 @@ type noopLogger struct{} func (noopLogger) Info(_ string, _ ...interface{}) {} func (noopLogger) Error(_ string, _ ...interface{}) {} +type startupLogger struct { + info []string + error []string +} + +func (l *startupLogger) Info(msg string, args ...interface{}) { + l.info = append(l.info, fmt.Sprintf(msg, args...)) +} + +func (l *startupLogger) Error(msg string, args ...interface{}) { + l.error = append(l.error, fmt.Sprintf(msg, args...)) +} + +func TestLogStartup_LogsDoormouseVersionAtInfoLevel(t *testing.T) { + logger := &startupLogger{} + + logStartup(logger, "2.1.0") + + if len(logger.info) != 1 || logger.info[0] != "Starting doormouse version `2.1.0`" { + t.Fatalf("info logs = %q, want one startup version line", logger.info) + } + if len(logger.error) != 0 { + t.Fatalf("error logs = %q, want none", logger.error) + } +} + // --------------------------------------------------------------------------- // Test helpers // ---------------------------------------------------------------------------