From 5c108e194b7aa81f5cc5bc6618989d0aad38d141 Mon Sep 17 00:00:00 2001 From: Akshay Bhansali Date: Tue, 31 Aug 2021 16:44:11 +0530 Subject: [PATCH] [WIP] Log errors (#72) * capture the stderr from the commands executed on running analyse and print that output plus the command that failed to provide useful feedback * Display error on encountering error while generating golist.json * Log Errors * Change to Msg * Elaborate on Go errors * fix typo * dummy commit * trigger-build * log error --- cmd/root.go | 4 +++- gomanifest/generator/deps.go | 2 +- pkg/analyses/golang/matcher.go | 9 +++++++-- pkg/analyses/maven/matcher.go | 8 +++++++- pkg/analyses/npm/matcher.go | 9 ++++++++- pkg/analyses/summary/helpers.go | 3 +++ 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index a8e4fcc..c5f67f1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -59,6 +59,7 @@ func Execute() { } err := segmentClient.Close() if err != nil { + log.Debug().Msgf("ERROR: Failed to Close Segment Client: " + err.Error()) return } os.Exit(exitCode) @@ -73,7 +74,8 @@ func init() { // Initiate segment client if segmentClient, err = segment.NewClient(); err != nil { - log.Fatal().Err(err).Msgf(err.Error()) + + log.Fatal().Err(err).Msg("Failed to Create Segment Client: " + err.Error()) } } diff --git a/gomanifest/generator/deps.go b/gomanifest/generator/deps.go index 436a666..4efc976 100644 --- a/gomanifest/generator/deps.go +++ b/gomanifest/generator/deps.go @@ -74,7 +74,7 @@ func GetDeps(cmd GoList) (map[string]DepPackage, error) { } // Wait for the `go list` command to complete. if err := cmd.Wait(); err != nil { - return nil, fmt.Errorf("%v: `go list` failed, use `go mod tidy` to known more", err) + return nil, fmt.Errorf("%v: `go list` failed, use `go mod tidy` from the directory where the manifest file is present to know more", err) } log.Debug().Msgf("Total packages: \t\t%d", len(depPackagesMap)) return depPackagesMap, nil diff --git a/pkg/analyses/golang/matcher.go b/pkg/analyses/golang/matcher.go index 34bc97c..46e075c 100644 --- a/pkg/analyses/golang/matcher.go +++ b/pkg/analyses/golang/matcher.go @@ -62,8 +62,13 @@ func (m *Matcher) GeneratorDependencyTree(manifestFilePath string) string { } manifestDir := filepath.Dir(manifestFilePath) treePath, _ := filepath.Abs(filepath.Join(os.TempDir(), m.DepsTreeFileName())) - generate(golang, manifestDir, treePath) - log.Debug().Msgf("Success: Generate golist.json") + err = generate(golang, manifestDir, treePath) + if err == nil { + log.Debug().Msgf("Success: Generate golist.json") + } else { + log.Fatal().Err(err).Msg("Failed to Generate golist.json") + } + return treePath } diff --git a/pkg/analyses/maven/matcher.go b/pkg/analyses/maven/matcher.go index 89fa7cc..7a86127 100644 --- a/pkg/analyses/maven/matcher.go +++ b/pkg/analyses/maven/matcher.go @@ -1,6 +1,7 @@ package maven import ( + "bytes" "fmt" "os" "os/exec" @@ -36,13 +37,18 @@ func (m *Matcher) GeneratorDependencyTree(manifestFilePath string) string { outcmd := fmt.Sprintf("-DoutputFile=%s", treePath) cleanRepo := exec.Command(maven, "--quiet", "clean", "-f", manifestFilePath) dependencyTree := exec.Command(maven, "--quiet", "org.apache.maven.plugins:maven-dependency-plugin:3.0.2:tree", "-f", manifestFilePath, outcmd, "-DoutputType=dot", "-DappendOutput=true") + + var stdout bytes.Buffer + dependencyTree.Stdout = &stdout + log.Debug().Msgf("Clean Repo Command: %s", cleanRepo) log.Debug().Msgf("dependencyTree Command: %s", dependencyTree) if err := cleanRepo.Run(); err != nil { log.Fatal().Err(err).Msgf(err.Error()) } if err := dependencyTree.Run(); err != nil { - log.Fatal().Err(err).Msgf(err.Error()) + log.Debug().Err(err).Msg("ERROR - Failed to execute: "+dependencyTree.String()+"\n"+stdout.String()) + log.Fatal().Err(err).Msgf("Missing, or Unable to Resolve Certain Dependencies or Artifacts") } log.Debug().Msgf("Success: buildDepsTree") return treePath diff --git a/pkg/analyses/npm/matcher.go b/pkg/analyses/npm/matcher.go index 5913348..7a72eff 100644 --- a/pkg/analyses/npm/matcher.go +++ b/pkg/analyses/npm/matcher.go @@ -1,6 +1,7 @@ package npm import ( + "bytes" "fmt" "os" "os/exec" @@ -44,10 +45,16 @@ func (m *Matcher) GeneratorDependencyTree(manifestFilePath string) string { npmList.Stdout = outfile log.Debug().Msgf("Dependency Tree Command: %s", npmList) + + var stderr bytes.Buffer + npmList.Stderr = &stderr + if err := npmList.Run(); err != nil { - log.Fatal().Err(err).Msgf(err.Error()) + log.Debug().Msg("ERROR - Failed to Execute "+npmList.String()+"\n"+stderr.String()) + log.Fatal().Err(err).Msgf("Missing Dependencies. Hint: Please install the required dependencies with \"npm install\" from the directory of the manifest file") } npmList.Wait() + log.Debug().Msgf("Success: buildDepsTree at %s", treePath) return treePath } diff --git a/pkg/analyses/summary/helpers.go b/pkg/analyses/summary/helpers.go index 2d48f3d..a8c1cd5 100644 --- a/pkg/analyses/summary/helpers.go +++ b/pkg/analyses/summary/helpers.go @@ -122,6 +122,9 @@ func outputSummaryPlain(result *StackSummary, verboseMsg bool) { blue("Low Vulnerabilities: "), blue(result.LowVulnerabilities), "\n\n", white("Full Report: "), result.ReportLink, "\n\n", ) + if result.TotalScannedDependencies == 0 { + fmt.Fprint(os.Stdout, yellow("Could not generate Dependency tree, Please make sure all packages present in manifest are installed.\n\n")) + } fmt.Fprint(os.Stdout, "(Powered by Snyk)\n\n") if verboseMsg { fmt.Fprint(os.Stdout, "tip: Register with Snyk and add token by running `crda auth`.\n")