Skip to content

Commit

Permalink
fix(cli): clean --all deletes only relevant dirs (#7704)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 authored Oct 10, 2024
1 parent 27117f8 commit 672e886
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
15 changes: 5 additions & 10 deletions pkg/commands/clean/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package clean

import (
"context"
"os"

"golang.org/x/xerrors"

Expand All @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
26 changes: 25 additions & 1 deletion pkg/commands/clean/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
},
{
Expand All @@ -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"))
},
},
{
Expand All @@ -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"))
},
},
{
Expand All @@ -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"))
},
},
{
Expand All @@ -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"))
},
},
{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 672e886

Please sign in to comment.