Skip to content

Commit

Permalink
Renamed "scan" command to "malware-scan"
Browse files Browse the repository at this point in the history
  • Loading branch information
akenion committed Oct 24, 2023
1 parent 212eca4 commit 4219593
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 17 deletions.
15 changes: 11 additions & 4 deletions wordfence/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..logging import log
from ..scanning.scanner import ExceptionContainer
from .banner.banner import show_welcome_banner_if_enabled
from .config import load_config
from .config import load_config, RenamedSubcommandException
from .subcommands import load_subcommand_definitions
from .context import CliContext
from .configurer import Configurer
Expand All @@ -21,9 +21,16 @@ class WordfenceCli:
def __init__(self):
self.initialize_early_logging()
self.subcommand_definitions = load_subcommand_definitions()
self.config, self.subcommand_definition = load_config(
self.subcommand_definitions
)
try:
self.config, self.subcommand_definition = load_config(
self.subcommand_definitions,
)
except RenamedSubcommandException as rename:
print(
f'The "{rename.old}" subcommand has been renamed to '
f'"{rename.new}"'
)
sys.exit(1)
self.initialize_logging(self.config.verbose)
self.cache = self.initialize_cache()
self.subcommand = None
Expand Down
31 changes: 29 additions & 2 deletions wordfence/cli/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
value_extractors: List = []


class RenamedSubcommandException(Exception):

def __init__(self, old: str, new: str):
self.old = old
self.new = new


def create_config_object(
subcommand: str,
definitions: Dict[str, ConfigItemDefinition],
Expand Down Expand Up @@ -58,6 +65,18 @@ def create_config_object(
return target


def _get_renamed_subcommand(
subcommand: str,
definitions: Dict[str, SubcommandDefinition]
) -> str:
for definition in definitions.values():
if subcommand in definition.previous_names:
return definition.name
raise KeyError(
f'Subcommand {subcommand} does not appear to have been renamed'
)


def load_config(
subcommand_definitions: Dict[str, SubcommandDefinition],
subcommand: str = None
Expand All @@ -70,8 +89,16 @@ def load_config(
subcommand = cli_values.subcommand

if subcommand:
assert subcommand in subcommand_definitions
subcommand_definition = subcommand_definitions[subcommand]
try:
subcommand_definition = subcommand_definitions[subcommand]
except KeyError:
raise RenamedSubcommandException(
subcommand,
_get_renamed_subcommand(
subcommand,
subcommand_definitions
)
)
config_map = {
**base_config_map,
**subcommand_definition.get_config_map()
Expand Down
9 changes: 8 additions & 1 deletion wordfence/cli/config/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ def get_cli_values(
add_definitions_to_parser(parser, base_config_map)

subparsers = parser.add_subparsers(title="Wordfence CLI subcommands",
dest="subcommand")
dest="subcommand",
metavar='')
for subcommand_definition in subcommand_definitions.values():
definitions = subcommand_definition.get_config_map()
subparser = subparsers.add_parser(
Expand All @@ -170,6 +171,12 @@ def get_cli_values(
add_definitions_to_parser(subparser, base_config_map)
add_definitions_to_parser(subparser, definitions)

for previous_name in subcommand_definition.previous_names:
subparsers.add_parser(
previous_name,
prog=previous_name
)

cli_values, trailing_arguments = parser.parse_known_args()
if '--' in trailing_arguments:
if trailing_arguments[0] != '--':
Expand Down
3 changes: 3 additions & 0 deletions wordfence/cli/malwarescan/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import malwarescan

__all__ = ['malwarescan']
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@
}

definition = SubcommandDefinition(
name='scan',
name='malware-scan',
description='Scan files for malware',
config_definitions=config_definitions,
config_section='SCAN',
cacheable_types=cacheable_types
config_section='MALWARE_SCAN',
cacheable_types=cacheable_types,
previous_names={'scan'}
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def revert_progress_changes() -> None:
reset_terminal()


class ScanSubcommand(Subcommand):
class MalwareScanSubcommand(Subcommand):

def _filter_signatures(
self,
Expand Down Expand Up @@ -199,4 +199,4 @@ def generate_exception_message(
return super().generate_exception_message(exception)


factory = ScanSubcommand
factory = MalwareScanSubcommand
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions wordfence/cli/scan/__init__.py

This file was deleted.

7 changes: 5 additions & 2 deletions wordfence/cli/subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

VALID_SUBCOMMANDS = {
'configure',
'scan',
'malware-scan',
'vuln-scan',
'help'
}
Expand Down Expand Up @@ -63,7 +63,8 @@ def __init__(
config_definitions: ConfigDefinitions,
config_section: str,
cacheable_types: Set[str],
requires_config: bool = True
requires_config: bool = True,
previous_names: Set[str] = None
):
self.name = name
self.description = description
Expand All @@ -72,6 +73,8 @@ def __init__(
self.config_map = None
self.cacheable_types = cacheable_types
self.requires_config = requires_config
self.previous_names = previous_names if previous_names is not None \
else set()

def get_config_map(self) -> Dict[str, ConfigItemDefinition]:
if self.config_map is None:
Expand Down

0 comments on commit 4219593

Please sign in to comment.