Skip to content
Open
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
22 changes: 16 additions & 6 deletions dissect/target/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,12 @@ def open_raw(cls, path: str | Path, *, apply: bool = True) -> Self:

@classmethod
def open_all(
cls, paths: str | Path | list[str | Path], include_children: bool = False, *, apply: bool = True
cls,
paths: str | Path | list[str | Path],
include_children: bool = False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can move this and recursive past the * (keyword argument only), or if that will break anything.

recursive: bool = False,
*,
apply: bool = True,
) -> Iterator[Self]:
"""Yield all targets from one or more paths or directories.

Expand All @@ -332,13 +337,16 @@ def open_all(
If the path is a ``os.PathLike`` object, it will be used as-is.
If the path is a string and looks like a URI, it will be parsed as such.
If the path is a string and does not look like a URI, it will be treated as a local path.
include_children: Whether to recursively open child targets.
include_children: Whether to open child targets.
recursive: Whether to open child targets recursively.

Raises:
TargetError: Raised when not a single ``Target`` can be loaded.
"""

def _open_all(spec: str | Path, include_children: bool = False, *, apply: bool = True) -> Iterator[Target]:
def _open_all(
spec: str | Path, include_children: bool = False, recursive: bool = False, *, apply: bool = True
) -> Iterator[Target]:
# If the path is a URI-like string, separate the path component
adjusted_path, parsed_path = parse_path_uri(spec)
# We always need a path to work with, so convert the spec into one if it's not one already
Expand Down Expand Up @@ -404,7 +412,7 @@ def _open_all(spec: str | Path, include_children: bool = False, *, apply: bool =

if include_children:
try:
yield from target.open_children(apply=apply)
yield from target.open_children(recursive=recursive, apply=apply)
except Exception as e:
get_target_logger(load_spec).error("Failed to load child target from %s", target, exc_info=e)

Expand All @@ -417,7 +425,7 @@ def _open_all(spec: str | Path, include_children: bool = False, *, apply: bool =
for spec in paths:
loaded = False

for target in _open_all(spec, include_children=include_children, apply=apply):
for target in _open_all(spec, include_children=include_children, recursive=recursive, apply=apply):
loaded = True
at_least_one_loaded = True
yield target
Expand All @@ -438,7 +446,9 @@ def _open_all(spec: str | Path, include_children: bool = False, *, apply: bool =

if path.is_dir():
for entry in path.iterdir():
for target in _open_all(entry, include_children=include_children, apply=apply):
for target in _open_all(
entry, include_children=include_children, recursive=recursive, apply=apply
):
at_least_one_loaded = True
yield target

Expand Down
11 changes: 7 additions & 4 deletions dissect/target/tools/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ def configure_generic_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument("-Kv", "--keychain-value", help="passphrase, recovery key or key file path value")
parser.add_argument("-L", "--loader", help="select a specific loader (i.e. vmx, raw)")
parser.add_argument("--child", help="load child of target by path of index (see --list-children)")
parser.add_argument("--children", action="store_true", help="include children")
parser.add_argument("--children", action="store_true", help="include children (depth=1 or use --recursive)")
parser.add_argument(
"--list-children", action=_OverrideRequiredAction, help="list all children indices and paths, then exit"
"--list-children",
action=_OverrideRequiredAction,
help="list children indices and paths (optionally use \
--recursive), then exit",
)
parser.add_argument("--recursive", action="store_true", help="make --list-children behave recursively")
parser.add_argument("--recursive", action="store_true", help="make --(list-)children behave recursively")
parser.add_argument("-v", "--verbose", action="count", default=0, help="increase output verbosity")
parser.add_argument("--version", action="store_true", help="print version")
parser.add_argument("-q", "--quiet", action="store_true", help="do not output logging information")
Expand Down Expand Up @@ -241,7 +244,7 @@ def open_targets(args: argparse.Namespace, *, apply: bool = True) -> Iterator[Ta
targets: Iterable[Target] = (
[Target.open_direct(args.targets)]
if direct
else Target.open_all(args.targets, include_children=children, apply=apply)
else Target.open_all(args.targets, include_children=children, recursive=args.recursive, apply=apply)
)

for target in targets:
Expand Down