Skip to content

Commit 672e886

Browse files
authored
fix(cli): clean --all deletes only relevant dirs (#7704)
Signed-off-by: knqyf263 <[email protected]>
1 parent 27117f8 commit 672e886

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

pkg/commands/clean/run.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package clean
22

33
import (
44
"context"
5-
"os"
65

76
"golang.org/x/xerrors"
87

@@ -25,7 +24,11 @@ func Run(ctx context.Context, opts flag.Options) error {
2524
}
2625

2726
if opts.CleanAll {
28-
return cleanAll(ctx, opts)
27+
opts.CleanScanCache = true
28+
opts.CleanVulnerabilityDB = true
29+
opts.CleanJavaDB = true
30+
opts.CleanChecksBundle = true
31+
opts.CleanVEXRepositories = true
2932
}
3033

3134
if opts.CleanScanCache {
@@ -60,14 +63,6 @@ func Run(ctx context.Context, opts flag.Options) error {
6063
return nil
6164
}
6265

63-
func cleanAll(ctx context.Context, opts flag.Options) error {
64-
log.InfoContext(ctx, "Removing all caches...")
65-
if err := os.RemoveAll(opts.CacheDir); err != nil {
66-
return xerrors.Errorf("failed to remove the directory (%s) : %w", opts.CacheDir, err)
67-
}
68-
return nil
69-
}
70-
7166
func cleanScanCache(ctx context.Context, opts flag.Options) error {
7267
log.InfoContext(ctx, "Removing scan cache...")
7368
c, cleanup, err := cache.New(opts.CacheOpts())

pkg/commands/clean/run_test.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ func TestRun(t *testing.T) {
2828
},
2929
wantErr: false,
3030
checkFunc: func(t *testing.T, dir string) {
31-
assert.NoDirExists(t, dir)
31+
assert.NoDirExists(t, filepath.Join(dir, "fanal"))
32+
assert.NoDirExists(t, filepath.Join(dir, "db"))
33+
assert.NoDirExists(t, filepath.Join(dir, "java-db"))
34+
assert.NoDirExists(t, filepath.Join(dir, "policy"))
35+
assert.NoDirExists(t, filepath.Join(dir, "vex"))
36+
assert.DirExists(t, dir)
3237
},
3338
},
3439
{
@@ -42,6 +47,7 @@ func TestRun(t *testing.T) {
4247
assert.DirExists(t, filepath.Join(dir, "db"))
4348
assert.DirExists(t, filepath.Join(dir, "java-db"))
4449
assert.DirExists(t, filepath.Join(dir, "policy"))
50+
assert.DirExists(t, filepath.Join(dir, "vex"))
4551
},
4652
},
4753
{
@@ -55,6 +61,7 @@ func TestRun(t *testing.T) {
5561
assert.DirExists(t, filepath.Join(dir, "fanal"))
5662
assert.DirExists(t, filepath.Join(dir, "java-db"))
5763
assert.DirExists(t, filepath.Join(dir, "policy"))
64+
assert.DirExists(t, filepath.Join(dir, "vex"))
5865
},
5966
},
6067
{
@@ -68,6 +75,7 @@ func TestRun(t *testing.T) {
6875
assert.DirExists(t, filepath.Join(dir, "fanal"))
6976
assert.DirExists(t, filepath.Join(dir, "db"))
7077
assert.DirExists(t, filepath.Join(dir, "policy"))
78+
assert.DirExists(t, filepath.Join(dir, "vex"))
7179
},
7280
},
7381
{
@@ -81,6 +89,21 @@ func TestRun(t *testing.T) {
8189
assert.DirExists(t, filepath.Join(dir, "fanal"))
8290
assert.DirExists(t, filepath.Join(dir, "db"))
8391
assert.DirExists(t, filepath.Join(dir, "java-db"))
92+
assert.DirExists(t, filepath.Join(dir, "vex"))
93+
},
94+
},
95+
{
96+
name: "clean vex repositories",
97+
cleanOpts: flag.CleanOptions{
98+
CleanVEXRepositories: true,
99+
},
100+
wantErr: false,
101+
checkFunc: func(t *testing.T, dir string) {
102+
assert.DirExists(t, filepath.Join(dir, "policy"))
103+
assert.DirExists(t, filepath.Join(dir, "fanal"))
104+
assert.DirExists(t, filepath.Join(dir, "db"))
105+
assert.DirExists(t, filepath.Join(dir, "java-db"))
106+
assert.NoDirExists(t, filepath.Join(dir, "vex"))
84107
},
85108
},
86109
{
@@ -127,6 +150,7 @@ func createTestFiles(t *testing.T, dir string) {
127150
"db",
128151
"java-db",
129152
"policy",
153+
"vex",
130154
}
131155
for _, subdir := range subdirs {
132156
err := os.MkdirAll(filepath.Join(dir, subdir), 0755)

0 commit comments

Comments
 (0)