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

fix(cli): clean --all deletes only relevant dirs #7704

Merged
merged 1 commit into from
Oct 10, 2024
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
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