diff --git a/README.md b/README.md index ca954f41..451029eb 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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) diff --git a/cmd/config.go b/internal/cmd/config.go similarity index 100% rename from cmd/config.go rename to internal/cmd/config.go diff --git a/cmd/iq.go b/internal/cmd/iq.go similarity index 100% rename from cmd/iq.go rename to internal/cmd/iq.go diff --git a/cmd/iq_test.go b/internal/cmd/iq_test.go similarity index 100% rename from cmd/iq_test.go rename to internal/cmd/iq_test.go diff --git a/cmd/root.go b/internal/cmd/root.go similarity index 92% rename from cmd/root.go rename to internal/cmd/root.go index 42c08af0..b617cd62 100644 --- a/cmd/root.go +++ b/internal/cmd/root.go @@ -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" @@ -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) { @@ -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") @@ -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 @@ -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 } diff --git a/cmd/root_test.go b/internal/cmd/root_test.go similarity index 96% rename from cmd/root_test.go rename to internal/cmd/root_test.go index 81ae38af..77a084b2 100644 --- a/cmd/root_test.go +++ b/internal/cmd/root_test.go @@ -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() { @@ -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 @@ -94,8 +104,9 @@ 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) { @@ -103,7 +114,7 @@ func TestProcessConfigPath(t *testing.T) { 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{} diff --git a/cmd/sleuth.go b/internal/cmd/sleuth.go similarity index 100% rename from cmd/sleuth.go rename to internal/cmd/sleuth.go diff --git a/cmd/sleuth_test.go b/internal/cmd/sleuth_test.go similarity index 99% rename from cmd/sleuth_test.go rename to internal/cmd/sleuth_test.go index a3e4f444..b02bb38c 100644 --- a/cmd/sleuth_test.go +++ b/internal/cmd/sleuth_test.go @@ -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") diff --git a/main.go b/main.go index 6aa1b5e8..fe9b068c 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ package main import ( - "github.com/sonatype-nexus-community/nancy/cmd" + "github.com/sonatype-nexus-community/nancy/internal/cmd" ) func main() {