From c94d723371381ad24484dab61fc84fd893671d28 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 28 Oct 2022 10:20:17 -0400 Subject: [PATCH] Inject and print more version information --- cmd/gcr-cleaner-cli/main.go | 3 +++ cmd/gcr-cleaner-server/main.go | 4 ++++ internal/version/version.go | 31 ++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/gcr-cleaner-cli/main.go b/cmd/gcr-cleaner-cli/main.go index c62c65f..ac7cfa8 100644 --- a/cmd/gcr-cleaner-cli/main.go +++ b/cmd/gcr-cleaner-cli/main.go @@ -104,6 +104,9 @@ func main() { } func realMain(ctx context.Context, logger *gcrcleaner.Logger) error { + logger.Debug("cli is starting", "version", version.HumanVersion) + defer logger.Debug("cli finished") + if args := flag.Args(); len(args) > 0 { return fmt.Errorf("expected zero arguments, got %d: %q", len(args), args) } diff --git a/cmd/gcr-cleaner-server/main.go b/cmd/gcr-cleaner-server/main.go index c1353ef..82b43e7 100644 --- a/cmd/gcr-cleaner-server/main.go +++ b/cmd/gcr-cleaner-server/main.go @@ -26,6 +26,7 @@ import ( "time" "github.com/GoogleCloudPlatform/gcr-cleaner/internal/bearerkeychain" + "github.com/GoogleCloudPlatform/gcr-cleaner/internal/version" "github.com/GoogleCloudPlatform/gcr-cleaner/pkg/gcrcleaner" gcrauthn "github.com/google/go-containerregistry/pkg/authn" gcrgoogle "github.com/google/go-containerregistry/pkg/v1/google" @@ -55,6 +56,9 @@ func main() { } func realMain(ctx context.Context, logger *gcrcleaner.Logger) error { + logger.Debug("server is starting", "version", version.HumanVersion) + defer logger.Debug("server finished") + port := os.Getenv("PORT") if port == "" { port = "8080" diff --git a/internal/version/version.go b/internal/version/version.go index 1e3314f..0a2646d 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,5 +1,10 @@ package version +import ( + "runtime" + "runtime/debug" +) + var ( // Name is the name of the binary. Name = "gcr-cleaner" @@ -10,6 +15,30 @@ var ( // Commit is the git sha. Commit = "HEAD" + // OSArch is the operating system and architecture combination. + OSArch = runtime.GOOS + "/" + runtime.GOARCH + // HumanVersion is the compiled version. - HumanVersion = Name + " v" + Version + " (" + Commit + ")" + HumanVersion = func() string { + version := Version + if version == "" { + version = "source" + } + + commit := Commit + if commit == "" { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + } + } + if commit == "" { + commit = "unknown" + } + + return Name + " " + version + " (" + commit + ", " + OSArch + ")" + }() )