Skip to content

Commit

Permalink
Enhance abstraction by moving targetrc constant to subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
twiggler committed Oct 7, 2024
1 parent 15b09a6 commit 9e82179
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions dissect/target/tools/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import sys
from contextlib import contextmanager
from datetime import datetime, timedelta, timezone
from typing import Any, BinaryIO, Callable, Iterator, TextIO
from typing import Any, BinaryIO, Callable, Iterator, Optional, TextIO

from dissect.cstruct import hexdump
from flow.record import RecordOutput
Expand Down Expand Up @@ -96,7 +96,7 @@ class ExtendedCmd(cmd.Cmd):

CMD_PREFIX = "cmd_"
_runtime_aliases = {}
DEFAULT_RUNCOMMANDSFILE = "~/.targetrc"
DEFAULT_RUNCOMMANDSFILE = None

def __init__(self, cyber: bool = False):
cmd.Cmd.__init__(self)
Expand Down Expand Up @@ -135,14 +135,15 @@ def _load_targetrc(self, path: pathlib.Path) -> None:
except Exception as e:
log.debug("Error processing .targetrc file: %s", e)

Check warning on line 136 in dissect/target/tools/shell.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/tools/shell.py#L135-L136

Added lines #L135 - L136 were not covered by tests

def _get_targetrc_path(self) -> pathlib.Path:
"""Get the path to the run commands file."""

return pathlib.Path(self.DEFAULT_RUNCOMMANDSFILE).expanduser()
def _get_targetrc_path(self) -> Optional[pathlib.Path]:
"""Get the path to the run commands file. Can return None if DEFAULT_RUNCOMMANDSFILE is not set."""
return pathlib.Path(self.DEFAULT_RUNCOMMANDSFILE).expanduser() if self.DEFAULT_RUNCOMMANDSFILE else None

Check warning on line 140 in dissect/target/tools/shell.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/tools/shell.py#L140

Added line #L140 was not covered by tests

def preloop(self) -> None:
super().preloop()
self._load_targetrc(self._get_targetrc_path())
targetrc_path = self._get_targetrc_path()
if targetrc_path is not None:
self._load_targetrc(targetrc_path)

@staticmethod
def check_compatible(target: Target) -> bool:
Expand Down Expand Up @@ -332,6 +333,7 @@ class TargetCmd(ExtendedCmd):
DEFAULT_HISTFILESIZE = 10_000
DEFAULT_HISTDIR = None
DEFAULT_HISTDIRFMT = ".dissect_history_{uid}_{target}"
DEFAULT_RUNCOMMANDSFILE = "~/.targetrc"
CONFIG_KEY_RUNCOMMANDSFILE = "TARGETRCFILE"

def __init__(self, target: Target):
Expand Down

0 comments on commit 9e82179

Please sign in to comment.