Skip to content

Commit

Permalink
Inject and print more version information (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored Oct 28, 2022
1 parent 11c5615 commit d04bf8b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
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 + ")"
}()
)

0 comments on commit d04bf8b

Please sign in to comment.