From 2e900e88b0b2c4c746a2b0a6b13930a9dfb7b3ae Mon Sep 17 00:00:00 2001 From: Adrien PONSIN Date: Tue, 8 Apr 2025 09:34:22 +0200 Subject: [PATCH] fix version construction --- cmd/middleman/main.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/cmd/middleman/main.go b/cmd/middleman/main.go index c3c4acd..8c20506 100644 --- a/cmd/middleman/main.go +++ b/cmd/middleman/main.go @@ -2,7 +2,8 @@ package main import ( "context" - "crypto/sha256" + "crypto/sha1" + "fmt" "gitea.illuad.fr/adrien/middleman/command" "github.com/rs/zerolog/log" "github.com/urfave/cli/v3" @@ -19,27 +20,17 @@ type noCommit struct { func (nc noCommit) String() string { if nc.message == nil { - nc.message = []byte("not tied to a commit") // 6344da387a21711b36a91c2fd55f48a026b2be8b5345cbbf32a31f5c089f4d75 + nc.message = []byte("not tied to a commit") // dbf242029aeedcfe71af2e5843474d8f8e2e9d63 } - sum := sha256.Sum256(nc.message) - return string(sum[:]) + sum := sha1.Sum(nc.message) + return fmt.Sprintf("%x", sum) } const appName = "middleman" -var ( - version = "dev" - commit = noCommit{}.String() -) +var version, commit string func main() { - info, ok := debug.ReadBuildInfo() - if ok { - if info.Main.Version != "" { - version = info.Main.Version - commit = info.Main.Sum - } - } // Note: os.Kill/syscall.SIGKILL (SIGKILL) signal cannot be caught or ignored. ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer cancel() @@ -48,7 +39,7 @@ func main() { app := cli.Command{ Name: appName, Usage: "Securely mount the Docker socket: apply fine-grained access control to Docker socket HTTP requests", - Version: version + "-" + commit, + Version: buildVersion(), DefaultCommand: command.ServeCmd, Commands: []*cli.Command{ command.Serve(group), @@ -66,3 +57,16 @@ func main() { log.Err(err).Send() } } + +func buildVersion() string { + info, ok := debug.ReadBuildInfo() + if ok { + if info.Main.Version != "" { + return info.Main.Version + "-" + info.Main.Sum + } + } + if version == "" && commit == "" { + return "dev" + "-" + noCommit{}.String() + } + return version + "-" + commit +}