Skip to content

Commit b9dbd7c

Browse files
committed
cli make and back require the config file
1 parent 31493e4 commit b9dbd7c

13 files changed

Lines changed: 55 additions & 5 deletions

prime_backup/cli/cli_entrypoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __create_command_adapters(cls) -> Dict[str, CliCommandAdapterBase]:
6565
def main(self):
6666
parser = argparse.ArgumentParser(description='Prime Backup v{} CLI tools'.format(cli_utils.get_plugin_version()), formatter_class=argparse.ArgumentDefaultsHelpFormatter)
6767
parser.add_argument('-d', '--db', default=_DEFAULT_STORAGE_ROOT, help='Path to the {db} database file, or path to the directory that contains the {db} database file, e.g. "/my/path/{db}", or "/my/path"'.format(db=db_constants.DB_FILE_NAME))
68-
parser.add_argument('-c', '--config', help='Path to the Prime Backup config.json file. If omitted, attempt to load ../config/prime_backup/config.json relative to the database directory')
68+
parser.add_argument('-c', '--config', help='Path to the Prime Backup config.json file. If omitted, attempt to load ../config/prime_backup/config.json relative to the database directory. Commands that need user backup settings, such as make and back, fail if no config file is found; other commands use default config')
6969
parser.add_argument('--version', action='store_true', help='Show version and exit')
7070
parser.add_argument('--debug', action='store_true', help='Enable debug logging')
7171
subparsers = parser.add_subparsers(title='Command', help='Available commands', dest='command')

prime_backup/cli/cmd/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class CliCommandHandlerBase(ABC):
2525
def __init__(self):
2626
self.logger: logging.Logger = logger.get()
2727

28+
@abstractmethod
29+
def requires_user_config_file(self) -> bool:
30+
raise NotImplementedError()
31+
2832
@property
2933
def config(self) -> Config:
3034
return Config.get()
@@ -47,18 +51,20 @@ def init_environment(self, db_path: Path, *, migrate: bool = False, config_path:
4751
ErrorReturnCodes.invalid_argument.sys_exit()
4852
self.init_db(root_path, create=False, migrate=migrate)
4953

50-
@classmethod
51-
def load_config(cls, root_path: Path, config_path: Optional[Path]) -> Config:
54+
def load_config(self, root_path: Path, config_path: Optional[Path]) -> Config:
5255
if config_path is None:
5356
auto_config_path = root_path.parent / 'config' / 'prime_backup' / 'config.json'
5457
if auto_config_path.is_file():
55-
config = cls.__load_config(auto_config_path)
58+
config = self.__load_config(auto_config_path)
5659
logger.get().info('Config file auto-detected and loaded from {!r}'.format(auto_config_path.as_posix()))
60+
elif self.requires_user_config_file():
61+
logger.get().error('Config file is required for this command, but it was not provided and the auto-detected path {!r} does not exist'.format(auto_config_path.as_posix()))
62+
ErrorReturnCodes.invalid_argument.sys_exit()
5763
else:
5864
config = Config.get_default()
5965
logger.get().info('Config file not provided; auto-detected path {!r} does not exist, using default config'.format(auto_config_path.as_posix()))
6066
else:
61-
config = cls.__load_config(config_path)
67+
config = self.__load_config(config_path)
6268
logger.get().info('Config file loaded from {!r}'.format(config_path.as_posix()))
6369
return config
6470

prime_backup/cli/cmd/cmd_back.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def __init__(self, args: BackCommandArgs):
3535
super().__init__()
3636
self.args = args
3737

38+
@override
39+
def requires_user_config_file(self) -> bool:
40+
return True
41+
3842
def __get_backup(self) -> BackupInfo:
3943
if self.args.backup_id is not None:
4044
backup_id = cli_utils.parse_backup_id(self.args.backup_id)

prime_backup/cli/cmd/cmd_db_overview.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def __init__(self, args: DbOverviewCommandArgs):
2020
super().__init__()
2121
self.args = args
2222

23+
@override
24+
def requires_user_config_file(self) -> bool:
25+
return False
26+
2327
@staticmethod
2428
def __size_str(size: int) -> str:
2529
return '{} ({} bytes)'.format(ByteCount(size).auto_str(), size)

prime_backup/cli/cmd/cmd_export.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def __init__(self, args: ExportCommandArgs):
3434
super().__init__()
3535
self.args = args
3636

37+
@override
38+
def requires_user_config_file(self) -> bool:
39+
return False
40+
3741
def handle(self):
3842
fmt = cli_utils.get_ebf(self.args.output_path, self.args.format)
3943
self.init_environment_from_args(self.args)

prime_backup/cli/cmd/cmd_extract.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def __init__(self, args: ExtractCommandArgs):
2424
super().__init__()
2525
self.args = args
2626

27+
@override
28+
def requires_user_config_file(self) -> bool:
29+
return False
30+
2731
def handle(self):
2832
self.init_environment_from_args(self.args)
2933

prime_backup/cli/cmd/cmd_fuse.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def __init__(self, args: FuseCommandArgs):
2727
super().__init__()
2828
self.args = args
2929

30+
@override
31+
def requires_user_config_file(self) -> bool:
32+
return False
33+
3034
def __adjust_fuse_config(self):
3135
from prime_backup.cli.fuse.config import FuseConfig
3236
fuse_config = FuseConfig.get()

prime_backup/cli/cmd/cmd_import.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def __init__(self, args: ImportCommandArgs):
2727
super().__init__()
2828
self.args = args
2929

30+
@override
31+
def requires_user_config_file(self) -> bool:
32+
return False
33+
3034
def handle(self):
3135
fmt = cli_utils.get_ebf(self.args.input_path, self.args.format)
3236
self.init_environment_from_args(self.args)

prime_backup/cli/cmd/cmd_init.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def __init__(self, args: InitCommandArgs):
2222
super().__init__()
2323
self.args = args
2424

25+
@override
26+
def requires_user_config_file(self) -> bool:
27+
return False
28+
2529
def handle(self):
2630
root_path = self.args.db_path
2731
if root_path.name == db_constants.DB_FILE_NAME and not root_path.is_dir():

prime_backup/cli/cmd/cmd_list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def __init__(self, args: ListCommandArgs):
2121
super().__init__()
2222
self.args = args
2323

24+
@override
25+
def requires_user_config_file(self) -> bool:
26+
return False
27+
2428
def handle(self):
2529
self.init_environment_from_args(self.args)
2630

0 commit comments

Comments
 (0)