Skip to content

Commit

Permalink
[WIP] Log errors (#72)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AkshayBhansali18 authored Aug 31, 2021
1 parent 516f001 commit 5c108e1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion gomanifest/generator/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pkg/analyses/golang/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/analyses/maven/matcher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package maven

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion pkg/analyses/npm/matcher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package npm

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyses/summary/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5c108e1

Please sign in to comment.