From 672e886aed152ae0f09a16941706746f3053ca94 Mon Sep 17 00:00:00 2001 From: Teppei Fukuda Date: Thu, 10 Oct 2024 15:02:06 +0400 Subject: [PATCH] fix(cli): `clean --all` deletes only relevant dirs (#7704) Signed-off-by: knqyf263 --- pkg/commands/clean/run.go | 15 +++++---------- pkg/commands/clean/run_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/pkg/commands/clean/run.go b/pkg/commands/clean/run.go index c60227360d19..8dd83519422f 100644 --- a/pkg/commands/clean/run.go +++ b/pkg/commands/clean/run.go @@ -2,7 +2,6 @@ package clean import ( "context" - "os" "golang.org/x/xerrors" @@ -25,7 +24,11 @@ func Run(ctx context.Context, opts flag.Options) error { } if opts.CleanAll { - return cleanAll(ctx, opts) + opts.CleanScanCache = true + opts.CleanVulnerabilityDB = true + opts.CleanJavaDB = true + opts.CleanChecksBundle = true + opts.CleanVEXRepositories = true } if opts.CleanScanCache { @@ -60,14 +63,6 @@ func Run(ctx context.Context, opts flag.Options) error { return nil } -func cleanAll(ctx context.Context, opts flag.Options) error { - log.InfoContext(ctx, "Removing all caches...") - if err := os.RemoveAll(opts.CacheDir); err != nil { - return xerrors.Errorf("failed to remove the directory (%s) : %w", opts.CacheDir, err) - } - return nil -} - func cleanScanCache(ctx context.Context, opts flag.Options) error { log.InfoContext(ctx, "Removing scan cache...") c, cleanup, err := cache.New(opts.CacheOpts()) diff --git a/pkg/commands/clean/run_test.go b/pkg/commands/clean/run_test.go index 9b301d238219..32e4110aba97 100644 --- a/pkg/commands/clean/run_test.go +++ b/pkg/commands/clean/run_test.go @@ -28,7 +28,12 @@ func TestRun(t *testing.T) { }, wantErr: false, checkFunc: func(t *testing.T, dir string) { - assert.NoDirExists(t, dir) + assert.NoDirExists(t, filepath.Join(dir, "fanal")) + assert.NoDirExists(t, filepath.Join(dir, "db")) + assert.NoDirExists(t, filepath.Join(dir, "java-db")) + assert.NoDirExists(t, filepath.Join(dir, "policy")) + assert.NoDirExists(t, filepath.Join(dir, "vex")) + assert.DirExists(t, dir) }, }, { @@ -42,6 +47,7 @@ func TestRun(t *testing.T) { assert.DirExists(t, filepath.Join(dir, "db")) assert.DirExists(t, filepath.Join(dir, "java-db")) assert.DirExists(t, filepath.Join(dir, "policy")) + assert.DirExists(t, filepath.Join(dir, "vex")) }, }, { @@ -55,6 +61,7 @@ func TestRun(t *testing.T) { assert.DirExists(t, filepath.Join(dir, "fanal")) assert.DirExists(t, filepath.Join(dir, "java-db")) assert.DirExists(t, filepath.Join(dir, "policy")) + assert.DirExists(t, filepath.Join(dir, "vex")) }, }, { @@ -68,6 +75,7 @@ func TestRun(t *testing.T) { assert.DirExists(t, filepath.Join(dir, "fanal")) assert.DirExists(t, filepath.Join(dir, "db")) assert.DirExists(t, filepath.Join(dir, "policy")) + assert.DirExists(t, filepath.Join(dir, "vex")) }, }, { @@ -81,6 +89,21 @@ func TestRun(t *testing.T) { assert.DirExists(t, filepath.Join(dir, "fanal")) assert.DirExists(t, filepath.Join(dir, "db")) assert.DirExists(t, filepath.Join(dir, "java-db")) + assert.DirExists(t, filepath.Join(dir, "vex")) + }, + }, + { + name: "clean vex repositories", + cleanOpts: flag.CleanOptions{ + CleanVEXRepositories: true, + }, + wantErr: false, + checkFunc: func(t *testing.T, dir string) { + assert.DirExists(t, filepath.Join(dir, "policy")) + assert.DirExists(t, filepath.Join(dir, "fanal")) + assert.DirExists(t, filepath.Join(dir, "db")) + assert.DirExists(t, filepath.Join(dir, "java-db")) + assert.NoDirExists(t, filepath.Join(dir, "vex")) }, }, { @@ -127,6 +150,7 @@ func createTestFiles(t *testing.T, dir string) { "db", "java-db", "policy", + "vex", } for _, subdir := range subdirs { err := os.MkdirAll(filepath.Join(dir, subdir), 0755)