From 474ed682bc3b85af050aaef69c79f0f09057772a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Galv=C3=A3o=20Xavier?= Date: Tue, 21 Dec 2021 23:18:49 -0800 Subject: [PATCH] add version to logs (#715) --- cmd/check_for_application_update.go | 1 + cmd/cmd.go | 38 +++++++++++++++++++++++++++-- cmd/packages.go | 2 -- cmd/power_user.go | 2 -- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/cmd/check_for_application_update.go b/cmd/check_for_application_update.go index 409d6beb0..c4f1bdab3 100644 --- a/cmd/check_for_application_update.go +++ b/cmd/check_for_application_update.go @@ -11,6 +11,7 @@ import ( func checkForApplicationUpdate() { if appConfig.CheckForAppUpdate { + log.Debugf("checking if new vesion of %s is available", internal.ApplicationName) isAvailable, newVersion, err := version.IsUpdateAvailable() if err != nil { // this should never stop the application diff --git a/cmd/cmd.go b/cmd/cmd.go index acd446c26..6d71d6e87 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,17 +1,19 @@ package cmd import ( + "encoding/json" "fmt" "os" - - "github.com/spf13/cobra" + "sort" "github.com/anchore/stereoscope" "github.com/anchore/syft/internal/config" "github.com/anchore/syft/internal/log" "github.com/anchore/syft/internal/logger" + "github.com/anchore/syft/internal/version" "github.com/anchore/syft/syft" "github.com/gookit/color" + "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/wagoodman/go-partybus" ) @@ -28,6 +30,8 @@ func init() { initAppConfig, initLogging, logAppConfig, + checkForApplicationUpdate, + logAppVersion, initEventBus, ) } @@ -110,3 +114,33 @@ func initEventBus() { stereoscope.SetBus(eventBus) syft.SetBus(eventBus) } + +func logAppVersion() { + versionInfo := version.FromBuild() + log.Infof("syft version: %s", versionInfo.Version) + + var fields map[string]interface{} + bytes, err := json.Marshal(versionInfo) + if err != nil { + return + } + err = json.Unmarshal(bytes, &fields) + if err != nil { + return + } + + keys := make([]string, 0, len(fields)) + for k := range fields { + keys = append(keys, k) + } + sort.Strings(keys) + + for idx, field := range keys { + value := fields[field] + branch := "├──" + if idx == len(fields)-1 { + branch = "└──" + } + log.Debugf(" %s %s: %s", branch, field, value) + } +} diff --git a/cmd/packages.go b/cmd/packages.go index 5835bec21..182daaef3 100644 --- a/cmd/packages.go +++ b/cmd/packages.go @@ -260,8 +260,6 @@ func packagesExecWorker(userInput string) <-chan error { return } - checkForApplicationUpdate() - src, cleanup, err := source.New(userInput, appConfig.Registry.ToOptions(), appConfig.Exclusions) if err != nil { errs <- fmt.Errorf("failed to determine image source: %w", err) diff --git a/cmd/power_user.go b/cmd/power_user.go index 1ef141a4b..572290361 100644 --- a/cmd/power_user.go +++ b/cmd/power_user.go @@ -111,8 +111,6 @@ func powerUserExecWorker(userInput string) <-chan error { return } - checkForApplicationUpdate() - src, cleanup, err := source.New(userInput, appConfig.Registry.ToOptions(), appConfig.Exclusions) if err != nil { errs <- err