Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject and print more version information #115

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/gcr-cleaner-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/gcr-cleaner-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
31 changes: 30 additions & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package version

import (
"runtime"
"runtime/debug"
)

var (
// Name is the name of the binary.
Name = "gcr-cleaner"
Expand All @@ -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 + ")"
}()
)