Skip to content

Commit

Permalink
feat(scan): add a --exclude option
Browse files Browse the repository at this point in the history
  • Loading branch information
agateau-gg authored and Jguer committed Oct 12, 2021
1 parent 9ffb488 commit 0fe22e7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
15 changes: 12 additions & 3 deletions ggshield/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@
help="Exclude results from a detector.",
multiple=True,
)
@click.option(
"--exclude",
default=None,
type=click.Path(),
help="Do not scan the specified path.",
multiple=True,
)
@click.pass_context
def scan(
ctx: click.Context,
Expand All @@ -83,14 +90,16 @@ def scan(
json_output: bool,
output: Optional[str],
banlist_detector: Optional[List[str]] = None,
exclude: Optional[List[str]] = None,
) -> int:
"""Command to scan various contents."""
ctx.obj["client"] = retrieve_client(ctx)
return_code = 0

ctx.obj["filter_set"] = path_filter_set(
Path(os.getcwd()), ctx.obj["config"].paths_ignore
)
paths_ignore = ctx.obj["config"].paths_ignore
if exclude is not None:
paths_ignore.update(exclude)
ctx.obj["filter_set"] = path_filter_set(Path(os.getcwd()), paths_ignore)
config: Config = ctx.obj["config"]

if show_secrets is not None:
Expand Down
2 changes: 1 addition & 1 deletion ggshield/dev_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def path_cmd(
try:
files = get_files_from_paths(
paths=paths,
paths_ignore=config.paths_ignore,
paths_ignore=ctx.obj["filter_set"],
recursive=recursive,
yes=yes,
verbose=config.verbose,
Expand Down
3 changes: 1 addition & 2 deletions ggshield/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from ggshield.git_shell import git_ls, is_git_dir

from .config import MAX_FILE_SIZE
from .filter import path_filter_set
from .scan import File, Files


Expand Down Expand Up @@ -72,7 +71,7 @@ def get_filepaths(
else:
_targets = {str(target) for target in top_dir.rglob(r"*")}

_targets.difference_update(path_filter_set(top_dir, paths_ignore))
_targets.difference_update(paths_ignore)

targets.update(_targets)
return targets
Expand Down
23 changes: 23 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ def test_directory_verbose_abort(self, cli_fs_runner):
assert result.exit_code == 0
assert not result.exception

def test_directory_verbose_ignored_abort(self, cli_fs_runner):
self.create_files()
result = cli_fs_runner.invoke(
cli,
[
"-v",
"scan",
"--exclude",
"file1",
"--exclude",
"dir/file2",
"path",
"./",
"-r",
],
input="n\n",
)
assert result.exit_code == 0
assert not result.exception
assert "file1\n" not in result.output
assert "dir/file2\n" not in result.output
assert "dir/subdir/file3\n" in result.output

@my_vcr.use_cassette()
def test_directory_verbose_yes(self, cli_fs_runner):
self.create_files()
Expand Down

0 comments on commit 0fe22e7

Please sign in to comment.