Skip to content

Commit eaa3440

Browse files
authored
Allow plugins to re-use arguments from main tools (#1262)
1 parent d40876f commit eaa3440

18 files changed

Lines changed: 215 additions & 208 deletions

File tree

dissect/target/helpers/docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _get_real_func_obj(func: Callable) -> tuple[type[Plugin], Callable]:
118118
func = func.fget
119119

120120
if inspect.ismethod(func):
121-
for klass in inspect.getmro(func.__self__.__class__):
121+
for klass in func.__self__.__class__.__mro__:
122122
if func.__name__ in klass.__dict__:
123123
break
124124
else:

dissect/target/plugins/general/loaders.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
import json
3+
import json as jsonlib
44

55
from dissect.target.helpers.docs import INDENT_STEP, get_docstring
66
from dissect.target.loader import LOADERS_BY_SCHEME
@@ -14,12 +14,8 @@ def check_compatible(self) -> None:
1414
pass
1515

1616
@export(output="none")
17-
# NOTE: We would prefer to re-use arguments across plugins from argparse in query.py, but that is not possible yet.
18-
# For now we use --as-json, but in the future this should be changed to inherit --json from target-query.
19-
# https://github.com/fox-it/dissect.target/pull/841
20-
# https://github.com/fox-it/dissect.target/issues/889
21-
@arg("--as-json", dest="as_json", action="store_true", help="output in JSON format")
22-
def loaders(self, as_json: bool = False) -> None:
17+
@arg("-j", "--json", action="store_true", help="output in JSON format")
18+
def loaders(self, json: bool = False) -> None:
2319
"""List the available loaders."""
2420

2521
loaders_info = {}
@@ -32,8 +28,8 @@ def loaders(self, as_json: bool = False) -> None:
3228

3329
loaders = sorted(loaders_info.items())
3430

35-
if as_json:
36-
print(json.dumps([{"name": name, "description": desc} for name, desc in loaders]), end="")
31+
if json:
32+
print(jsonlib.dumps([{"name": name, "description": desc} for name, desc in loaders]), end="")
3733

3834
else:
3935
print("Available loaders:")

dissect/target/plugins/general/plugins.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
import json
3+
import json as jsonlib
44
import textwrap
55

66
from dissect.target import plugin
@@ -92,7 +92,7 @@ def generate_functions_json(functions: list[plugin.FunctionDescriptor] | None =
9292
if failures := plugin.failed():
9393
failed = [{"module": f.module, "stacktrace": "".join(f.stacktrace)} for f in failures]
9494

95-
return json.dumps({"loaded": loaded, "failed": failed})
95+
return jsonlib.dumps({"loaded": loaded, "failed": failed})
9696

9797

9898
def _get_os_functions() -> list[plugin.FunctionDescriptor]:
@@ -162,14 +162,10 @@ def check_compatible(self) -> None:
162162

163163
@export(output="none", cache=False)
164164
@arg("--docs", dest="print_docs", action="store_true", help="output docstrings")
165-
# NOTE: We would prefer to re-use arguments across plugins from argparse in query.py, but that is not possible yet.
166-
# For now we use --as-json, but in the future this should be changed to inherit --json from target-query.
167-
# https://github.com/fox-it/dissect.target/pull/841
168-
# https://github.com/fox-it/dissect.target/issues/889
169-
@arg("--as-json", dest="as_json", action="store_true", help="output in JSON format")
170-
def plugins(self, print_docs: bool = False, as_json: bool = False) -> None:
165+
@arg("-j", "--json", action="store_true", help="output in JSON format")
166+
def plugins(self, print_docs: bool = False, json: bool = False) -> None:
171167
"""Print all available plugins."""
172-
if as_json:
168+
if json:
173169
print(generate_functions_json(), end="")
174170
else:
175171
print(generate_functions_overview(include_docs=print_docs))

dissect/target/plugins/os/unix/esxi/_os.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import gzip
4-
import json
4+
import json as jsonlib
55
import lzma
66
import struct
77
import subprocess
@@ -189,12 +189,12 @@ def vm_inventory(self) -> Iterator[VirtualMachineRecord]:
189189

190190
@export(output="none")
191191
@arg("path", help="config path")
192-
@arg("--as-json", action="store_true", help="format as json")
193-
def esxconf(self, path: str, as_json: bool) -> None:
192+
@arg("-j", "--json", action="store_true", help="output in JSON format")
193+
def esxconf(self, path: str, json: bool) -> None:
194194
obj = self._cfg(path)
195195

196-
if as_json:
197-
print(json.dumps(obj, indent=4, sort_keys=True))
196+
if json:
197+
print(jsonlib.dumps(obj, indent=4, sort_keys=True))
198198
else:
199199
print(obj)
200200

@@ -578,11 +578,11 @@ def parse_config_store(fh: BinaryIO) -> dict[str, Any]:
578578
identifier["creation_time"] = row.CreationTime
579579
identifier["version"] = row.Version
580580
identifier["success"] = row.Success
581-
identifier["auto_conf_value"] = json.loads(row.AutoConfValue) if row.AutoConfValue else None
582-
identifier["user_value"] = json.loads(row.UserValue) if row.UserValue else None
583-
identifier["vital_value"] = json.loads(row.VitalValue) if row.VitalValue else None
584-
identifier["cached_value"] = json.loads(row.CachedValue) if row.CachedValue else None
585-
identifier["desired_value"] = json.loads(row.DesiredValue) if row.DesiredValue else None
581+
identifier["auto_conf_value"] = jsonlib.loads(row.AutoConfValue) if row.AutoConfValue else None
582+
identifier["user_value"] = jsonlib.loads(row.UserValue) if row.UserValue else None
583+
identifier["vital_value"] = jsonlib.loads(row.VitalValue) if row.VitalValue else None
584+
identifier["cached_value"] = jsonlib.loads(row.CachedValue) if row.CachedValue else None
585+
identifier["desired_value"] = jsonlib.loads(row.DesiredValue) if row.DesiredValue else None
586586
identifier["revision"] = row.Revision
587587

588588
return store

dissect/target/tools/dd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def main() -> int:
3535
parser.add_argument("-b", "--bytes", type=int, default=-1, help="amount of bytes to read")
3636
configure_generic_arguments(parser)
3737

38-
args, rest = parser.parse_known_args()
39-
process_generic_arguments(args, rest)
38+
args, _ = parser.parse_known_args()
39+
process_generic_arguments(args)
4040

4141
try:
4242
t = Target.open(args.target)

dissect/target/tools/diff.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from dissect.target.tools.utils import (
3838
catch_sigpipe,
3939
configure_generic_arguments,
40-
generate_argparse_for_bound_method,
40+
generate_argparse_for_method,
4141
process_generic_arguments,
4242
)
4343

@@ -119,7 +119,7 @@ def get_plugin_output_records(plugin_name: str, plugin_arg_parts: list[str], tar
119119
raise ValueError("Comparing plugin output is only supported for plugins outputting records.")
120120

121121
if callable(attr):
122-
argparser = generate_argparse_for_bound_method(attr)
122+
argparser = generate_argparse_for_method(attr)
123123
try:
124124
args = argparser.parse_args(plugin_arg_parts)
125125
except SystemExit:
@@ -965,8 +965,8 @@ def main() -> int:
965965

966966
configure_generic_arguments(parser)
967967

968-
args, rest = parser.parse_known_args()
969-
process_generic_arguments(args, rest)
968+
args, _ = parser.parse_known_args()
969+
process_generic_arguments(args)
970970

971971
if len(args.targets) < 2:
972972
parser.error("at least two targets are required for target-diff")

dissect/target/tools/dump/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def parse_arguments() -> tuple[argparse.Namespace, list[str]]:
300300
configure_generic_arguments(parser)
301301

302302
args, rest = parser.parse_known_args()
303-
process_generic_arguments(args, rest)
303+
process_generic_arguments(args)
304304

305305
if not args.function and ("-h" in rest or "--help" in rest):
306306
parser.print_help()

dissect/target/tools/fs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ def main() -> int:
143143
parser_cp.set_defaults(handler=cp)
144144
configure_generic_arguments(parser)
145145

146-
args, rest = parser.parse_known_args()
147-
process_generic_arguments(args, rest)
146+
args, _ = parser.parse_known_args()
147+
process_generic_arguments(args)
148148

149149
if args.subcommand is None:
150150
parser.error("No subcommand specified")

dissect/target/tools/info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def main() -> int:
5858
parser.add_argument("-J", "--jsonlines", action="store_true", help="output records as one-line json")
5959
configure_generic_arguments(parser)
6060

61-
args, rest = parser.parse_known_args()
61+
args, _ = parser.parse_known_args()
6262

6363
if not args.targets and not args.from_file:
6464
parser.error("too few arguments")
@@ -72,7 +72,7 @@ def main() -> int:
7272
targets = targets[:-1]
7373
args.targets = targets
7474

75-
process_generic_arguments(args, rest)
75+
process_generic_arguments(args)
7676

7777
try:
7878
for i, target in enumerate(Target.open_all(args.targets, include_children=args.children)):

dissect/target/tools/mount.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def main() -> int:
5858
parser.add_argument("-o", "--options", help="additional FUSE options")
5959
configure_generic_arguments(parser)
6060

61-
args, rest = parser.parse_known_args()
62-
process_generic_arguments(args, rest)
61+
args, _ = parser.parse_known_args()
62+
process_generic_arguments(args)
6363

6464
if not HAS_FUSE:
6565
log.error("fusepy is not installed: pip install fusepy")

0 commit comments

Comments
 (0)