Skip to content

Commit 98666b9

Browse files
committed
make --list-children usable without required args. Fixes to target-info.
1 parent c06052f commit 98666b9

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

dissect/target/tools/info.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,18 @@ def main() -> int:
8080
try:
8181
for i, target in enumerate(open_targets(args)):
8282
try:
83+
target_info = get_target_info(target, args)
8384
if args.jsonlines:
84-
print(json.dumps(get_target_info(target), default=str))
85+
print(json.dumps(target_info, default=str))
8586
elif args.json:
86-
print(json.dumps(get_target_info(target), indent=4, default=str))
87+
print(json.dumps(target_info, indent=4, default=str))
8788
elif args.record:
8889
rs = record_output(args.strings)
89-
rs.write(InfoRecord(**get_target_info(target), _target=target))
90+
rs.write(InfoRecord(**target_info, _target=target))
9091
else:
9192
if i > 0:
9293
print("-" * 70)
93-
print_target_info(target)
94+
print_target_info(target, target_info)
9495
except Exception as e: # noqa: PERF203
9596
target.log.error("Exception in retrieving information for target: `%s`, use `-vv` for details", target) # noqa: TRY400
9697
target.log.debug("", exc_info=e)
@@ -102,11 +103,11 @@ def main() -> int:
102103
return 0
103104

104105

105-
def get_target_info(target: Target) -> dict[str, str | list[str]]:
106+
def get_target_info(target: Target, args: argparse.Namespace) -> dict[str, str | list[str]]:
106107
return {
107108
"disks": get_disks_info(target),
108109
"volumes": get_volumes_info(target),
109-
"children": get_children_info(target),
110+
"children": get_children_info(target, args.recursive),
110111
"hostname": target.hostname,
111112
"domain": get_optional_func(target, "domain"),
112113
"ips": target.ips,
@@ -126,10 +127,10 @@ def get_optional_func(target: Target, func: str) -> str | None:
126127
return None
127128

128129

129-
def print_target_info(target: Target) -> None:
130+
def print_target_info(target: Target, target_info: dict[str, str | list[str]]) -> None:
130131
print(target)
131132

132-
for name, value in get_target_info(target).items():
133+
for name, value in target_info.items():
133134
if name in ["disks", "volumes", "children"]:
134135
if not any(value):
135136
continue
@@ -161,8 +162,14 @@ def get_volumes_info(target: Target) -> list[dict[str, str | int]]:
161162

162163
def get_children_info(target: Target, recursive: bool = False) -> list[dict[str, str]]:
163164
if recursive:
164-
return [{"child_index": i, "type": c.type, "path": str(c.path)} for i, c in target.list_children_recursive()]
165-
return [{"child_index": i, "type": c.type, "path": str(c.path)} for i, c in enumerate(target.list_children())]
165+
return [
166+
{"child_index": i, "name": c.name, "type": c.type, "path": str(c.path)}
167+
for i, c in target.list_children_recursive()
168+
]
169+
return [
170+
{"child_index": i, "name": c.name, "type": c.type, "path": str(c.path)}
171+
for i, c in enumerate(target.list_children())
172+
]
166173

167174

168175
if __name__ == "__main__":

dissect/target/tools/utils.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@
4242
USAGE_FORMAT_TMPL = "{prog} -f {name}{usage}"
4343

4444

45+
class list_children_action(argparse.Action):
46+
def __init__(
47+
self, option_strings: list, dest: str = argparse.SUPPRESS, default: str = argparse.SUPPRESS, help: None = None
48+
):
49+
super().__init__(option_strings=option_strings, dest=dest, default=default, nargs=0, help=help)
50+
51+
def __call__(
52+
self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, values: list, option_string: None = None
53+
):
54+
for action in parser._get_positional_actions():
55+
action.required = False
56+
57+
namespace.list_children = True
58+
59+
4560
def configure_generic_arguments(parser: argparse.ArgumentParser) -> None:
4661
parser.add_argument("-K", "--keychain-file", type=Path, help="keychain file in CSV format")
4762
parser.add_argument("-Kv", "--keychain-value", help="passphrase, recovery key or key file path value")
@@ -52,14 +67,13 @@ def configure_generic_arguments(parser: argparse.ArgumentParser) -> None:
5267
parser.add_argument("--children", action="store_true", help="include children")
5368
parser.add_argument(
5469
"--list-children",
55-
action=argparse.BooleanOptionalAction,
70+
action=list_children_action,
5671
help="list all children by index and path output to be used in --child - does not process anything",
5772
)
5873
parser.add_argument(
59-
"--list-children-recursive",
60-
action=argparse.BooleanOptionalAction,
61-
default=False,
62-
help="list all children (recursively) by index and path - does not process anything",
74+
"--recursive",
75+
action="store_true",
76+
help="Makes --list-children behave recursively - does not process anything",
6377
)
6478
parser.add_argument("-v", "--verbose", action="count", default=0, help="increase output verbosity")
6579
parser.add_argument("--version", action="store_true", help="print version")
@@ -97,10 +111,11 @@ def process_generic_arguments(args: argparse.Namespace, rest: list[str]) -> None
97111
sys.exit(1)
98112

99113
# List found children on targets and exit
100-
if args.list_children or args.list_children_recursive:
114+
if hasattr(args, "list_children"):
115+
# List found children on targets and exit
101116
# Using open_targets here breaks with target-fs due to target vs targets
102117
list_target = Target.open_all(targets, args.children)
103-
print_children(list_target, recursive=args.list_children_recursive)
118+
print_children(list_target, recursive=args.recursive)
104119
sys.exit(0)
105120

106121
if args.keychain_file:

0 commit comments

Comments
 (0)