Skip to content

Commit

Permalink
Sleuth - merge repairs (#171)
Browse files Browse the repository at this point in the history
* move cmd to internal package. fixes #166 (#168)

* make `--clean-cache` available as root command, since it is not specific to ossi or iq. (#169)
  • Loading branch information
bhamail authored Aug 19, 2020
1 parent 0fdbaab commit 24e7caa
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 31 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ Flags:
Global Flags:
-v, -- count Set log level, multiple v's is more verbose
-c, --clean-cache Deletes local cache directory
--loud indicate output should include non-vulnerable packages
-p, --path string Specify a path to a dep Gopkg.lock file for scanning
-q, --quiet indicate output should contain only packages with vulnerabilities (default true)
Expand Down Expand Up @@ -113,7 +112,6 @@ Flags:
Global Flags:
-v, -- count Set log level, multiple v's is more verbose
-c, --clean-cache Deletes local cache directory
--loud indicate output should include non-vulnerable packages
-p, --path string Specify a path to a dep Gopkg.lock file for scanning
-q, --quiet indicate output should contain only packages with vulnerabilities (default true)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
63 changes: 40 additions & 23 deletions cmd/root.go → internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/sonatype-nexus-community/nancy/buildversion"
"github.com/sonatype-nexus-community/nancy/internal/audit"
"github.com/sonatype-nexus-community/nancy/internal/customerrors"
"github.com/sonatype-nexus-community/nancy/internal/logger"
"github.com/sonatype-nexus-community/nancy/packages"
"github.com/sonatype-nexus-community/nancy/parse"
"github.com/sonatype-nexus-community/nancy/types"
Expand Down Expand Up @@ -110,9 +111,34 @@ var rootCmd = &cobra.Command{
Long: `nancy is a tool to check for vulnerabilities in your Golang dependencies,
powered by the 'Sonatype OSS Index', and as well, works with Nexus IQ Server, allowing you
a smooth experience as a Golang developer, using the best tools in the market!`,
Run: func(cmd *cobra.Command, args []string) {
RunE: doRoot,
}

//goland:noinspection GoUnusedParameter
func doRoot(cmd *cobra.Command, args []string) (err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("pkg: %v", r)
}
err = customerrors.ErrorShowLogPath{Err: err}
}
}()

logLady = logger.GetLogger("", configOssi.LogLevel)
logLady.Info("Nancy parsing config for root command")

if configOssi.CleanCache {
ossIndex := ossiCreator.create()
if err = doCleanCache(ossIndex); err != nil {
panic(err)
}
} else {
_ = cmd.Usage()
},
}
return
}

func Execute() (err error) {
Expand Down Expand Up @@ -141,7 +167,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&configOssi.Version, "version", "V", false, "Get the version")
rootCmd.PersistentFlags().BoolVarP(&configOssi.Quiet, "quiet", "q", true, "indicate output should contain only packages with vulnerabilities")
rootCmd.PersistentFlags().BoolVar(&configOssi.Loud, "loud", false, "indicate output should include non-vulnerable packages")
rootCmd.PersistentFlags().BoolVarP(&configOssi.CleanCache, "clean-cache", "c", false, "Deletes local cache directory")
rootCmd.Flags().BoolVarP(&configOssi.CleanCache, "clean-cache", "c", false, "Deletes local cache directory")
rootCmd.PersistentFlags().StringVarP(&configOssi.Username, flagNameOssiUsername, "u", "", "Specify OSS Index username for request")
rootCmd.PersistentFlags().StringVarP(&configOssi.Token, flagNameOssiToken, "t", "", "Specify OSS Index API token for request")
rootCmd.PersistentFlags().StringVarP(&configOssi.Path, "path", "p", "", "Specify a path to a dep "+GopkgLockFilename+" file for scanning")
Expand Down Expand Up @@ -226,28 +252,8 @@ func processConfig() (err error) {
configOssi.Formatter = audit.AuditLogTextFormatter{Quiet: isQuiet, NoColor: configOssi.NoColor}
}

switch configOssi.LogLevel {
case 1:
logLady.Level = logrus.InfoLevel
case 2:
logLady.Level = logrus.DebugLevel
case 3:
logLady.Level = logrus.TraceLevel
}

ossIndex := ossiCreator.create()

if configOssi.CleanCache {
logLady.Info("Attempting to clean cache")
if err = ossIndex.NoCacheNoProblems(); err != nil {
logLady.WithField("error", err).Error("Error cleaning cache")
fmt.Printf("ERROR: cleaning cache: %v\n", err)
return
}
logLady.Info("Cache cleaned")
return
}

printHeader(!getIsQuiet() && reflect.TypeOf(configOssi.Formatter).String() == "audit.AuditLogTextFormatter")

// todo: should errors from this call be ignored
Expand All @@ -272,6 +278,17 @@ func processConfig() (err error) {
return
}

func doCleanCache(ossIndex ossindex.IServer) (err error) {
logLady.Info("Attempting to clean cache")
if err = ossIndex.NoCacheNoProblems(); err != nil {
logLady.WithField("error", err).Error("Error cleaning cache")
fmt.Printf("ERROR: cleaning cache: %v\n", err)
return
}
logLady.Info("Cache cleaned")
return
}

func getIsQuiet() bool {
return !configOssi.Loud
}
Expand Down
19 changes: 15 additions & 4 deletions cmd/root_test.go → internal/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ func TestRootCommandUnknownCommand(t *testing.T) {
assert.Contains(t, err.Error(), "unknown command \"one\" for \"nancy\"")
}

func TestRootCommandCleanCache(t *testing.T) {
origConfig := configOssi
defer func() {
configOssi = origConfig
}()
output, err := executeCommand(rootCmd, "-c")
assert.Equal(t, output, "")
assert.Nil(t, err)
}

func TestProcessConfigInvalidStdIn(t *testing.T) {
origConfig := configOssi
defer func() {
Expand All @@ -77,7 +87,7 @@ func TestProcessConfigInvalidStdIn(t *testing.T) {
assert.Equal(t, stdInInvalid, err)
}

func TestProcessConfigCleanCacheError(t *testing.T) {
func TestDoRootCleanCacheError(t *testing.T) {
origConfig := configOssi
defer func() {
configOssi = origConfig
Expand All @@ -94,16 +104,17 @@ func TestProcessConfigCleanCacheError(t *testing.T) {
}()
ossiCreator = &ossiFactoryMock{mockOssiServer: mockOssiServer{auditPackagesErr: expectedError}}

err := processConfig()
assert.Equal(t, expectedError, err)
err := doRoot(nil, nil)
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), expectedError.Error()), err.Error())
}

func TestProcessConfigPath(t *testing.T) {
origConfig := configOssi
defer func() {
configOssi = origConfig
}()
configOssi = types.Configuration{Path: "../packages/testdata/" + GopkgLockFilename}
configOssi = types.Configuration{Path: "../../packages/testdata/" + GopkgLockFilename}

logLady, _ = test.NewNullLogger()
configOssi.Formatter = &logrus.TextFormatter{}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/sleuth_test.go → internal/cmd/sleuth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestConfigOssi_exclude_vulnerabilities(t *testing.T) {
[]string{sleuthCmd.Use, "--exclude-vulnerability=CVE123,CVE988"}...)
}

const testdataDir = "../internal/configuration/testdata"
const testdataDir = "../../internal/configuration/testdata"

func TestConfigOssi_exclude_vulnerabilities_with_sane_file(t *testing.T) {
file, _ := os.Open(testdataDir + "/normalIgnore")
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package main

import (
"github.com/sonatype-nexus-community/nancy/cmd"
"github.com/sonatype-nexus-community/nancy/internal/cmd"
)

func main() {
Expand Down

0 comments on commit 24e7caa

Please sign in to comment.