Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make --clean-cache available as root command #169

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
63 changes: 40 additions & 23 deletions 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
17 changes: 14 additions & 3 deletions 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,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) {
Expand Down