From 73d0c26a9a276e08fd24f0ab3d4e73870397a009 Mon Sep 17 00:00:00 2001 From: Jason Luong Date: Thu, 23 Jan 2025 12:05:27 +0000 Subject: [PATCH] chore: add error catalog errors to the end of logs on CLI failure --- .../{logheader.go => logheaderfooter.go} | 49 ++++++++++++++++--- cliv2/cmd/cliv2/main.go | 16 +++--- 2 files changed, 49 insertions(+), 16 deletions(-) rename cliv2/cmd/cliv2/{logheader.go => logheaderfooter.go} (81%) diff --git a/cliv2/cmd/cliv2/logheader.go b/cliv2/cmd/cliv2/logheaderfooter.go similarity index 81% rename from cliv2/cmd/cliv2/logheader.go rename to cliv2/cmd/cliv2/logheaderfooter.go index ba6f45ff83..84bcd84981 100644 --- a/cliv2/cmd/cliv2/logheader.go +++ b/cliv2/cmd/cliv2/logheaderfooter.go @@ -6,20 +6,22 @@ import _ "github.com/snyk/go-application-framework/pkg/networking/fips_enable" import ( "crypto/sha256" "encoding/hex" + "errors" "fmt" "net/http" "regexp" + "strconv" "strings" - "github.com/snyk/go-application-framework/pkg/local_workflows/config_utils" - + "github.com/snyk/error-catalog-golang-public/snyk_errors" "github.com/snyk/go-application-framework/pkg/auth" "github.com/snyk/go-application-framework/pkg/configuration" + "github.com/snyk/go-application-framework/pkg/local_workflows/config_utils" + + "github.com/snyk/cli/cliv2/internal/cliv2" localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows" "github.com/snyk/go-application-framework/pkg/networking" "github.com/snyk/go-application-framework/pkg/networking/fips" - - "github.com/snyk/cli/cliv2/internal/cliv2" ) func logHeaderAuthorizationInfo( @@ -82,6 +84,14 @@ func getFipsStatus(config configuration.Configuration) string { return fipsEnabled } +func tablePrint(name string, value string) { + title := name + if len(name) > 0 { + title = title + ":" + } + globalLogger.Printf("%-22s %s", title, value) +} + func writeLogHeader(config configuration.Configuration, networkAccess networking.NetworkAccess) { authorization, _, userAgent := logHeaderAuthorizationInfo(config, networkAccess) @@ -101,10 +111,6 @@ func writeLogHeader(config configuration.Configuration, networkAccess networking previewFeaturesEnabled = "enabled" } - tablePrint := func(name string, value string) { - globalLogger.Printf("%-22s %s", name+":", value) - } - fipsEnabled := getFipsStatus(config) tablePrint("Version", cliv2.GetFullVersion()+" "+buildType) @@ -136,3 +142,30 @@ func writeLogHeader(config configuration.Configuration, networkAccess networking tablePrint(" Configuration", "all good") } } + +func writeLogFooter(exitCode int, errs []error) { + tablePrint("Exit Code", strconv.Itoa(exitCode)) + + // output error details + if exitCode > 1 && len(errs) > 0 { + for i, err := range errs { + var snykError snyk_errors.Error + if errors.As(err, &snykError) { + tablePrint(fmt.Sprintf("Error (%d)", i+1), snykError.ErrorCode) + tablePrint("", snykError.Title) + tablePrint("", snykError.Classification) + tablePrint("", snykError.Description) + tablePrint("", snykError.Type) + + if len(snykError.Detail) > 0 { + tablePrint("", snykError.Detail) + } + if len(snykError.Meta) > 0 { + for k, v := range snykError.Meta { + tablePrint("", fmt.Sprintf("%s: %s", k, v.(string))) + } + } + } + } + } +} diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index 0a02fb75c3..7393b658f3 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -79,8 +79,8 @@ const ( ) func main() { - errorCode := MainWithErrorCode() - globalLogger.Printf("Exiting with %d", errorCode) + errorCode, errs := MainWithErrorCode() + writeLogFooter(errorCode, errs) os.Exit(errorCode) } @@ -478,9 +478,12 @@ func displayError(err error, userInterface ui.UserInterface, config configuratio } } -func MainWithErrorCode() int { +func MainWithErrorCode() (int, []error) { initDebugBuild() + errorList := []error{} + errorListMutex := sync.Mutex{} + startTime := time.Now() var err error rInfo := runtimeinfo.New(runtimeinfo.WithName("snyk-cli"), runtimeinfo.WithVersion(cliv2.GetFullVersion())) @@ -528,7 +531,7 @@ func MainWithErrorCode() int { err = globalEngine.Init() if err != nil { globalLogger.Print("Failed to init Workflow Engine!", err) - return constants.SNYK_EXIT_CODE_ERROR + return constants.SNYK_EXIT_CODE_ERROR, errorList } // add output flags as persistent flags @@ -539,9 +542,6 @@ func MainWithErrorCode() int { // add workflows as commands createCommandsForWorkflows(rootCommand, globalEngine) - errorList := []error{} - errorListMutex := sync.Mutex{} - // init NetworkAccess ua := networking.UserAgent(networking.UaWithConfig(globalConfiguration), networking.UaWithRuntimeInfo(rInfo), networking.UaWithOS(internalOS)) networkAccess := globalEngine.GetNetworkAccess() @@ -633,7 +633,7 @@ func MainWithErrorCode() int { globalLogger.Printf("Failed to cleanup %v", err) } - return exitCode + return exitCode, errorList } func legacyCLITerminated(err error, errorList []error) error {