Make inputs and backup_dir optional in the config file
They can come from -i and -o; missing ones were rejected as a bad configuration file (exit code 6).
This commit is contained in:
@@ -946,19 +946,22 @@ def _read_config(config_file: str, user: str | None = None) -> ConfigArgs:
|
|||||||
config = configparser.ConfigParser(interpolation=None)
|
config = configparser.ConfigParser(interpolation=None)
|
||||||
config.read(config_file)
|
config.read(config_file)
|
||||||
|
|
||||||
section = 'backup'
|
|
||||||
|
|
||||||
# Allow compatibility with previous version of config file
|
# Allow compatibility with previous version of config file
|
||||||
try:
|
if config.has_section('backup'):
|
||||||
inputs = config.get(section, 'inputs')
|
section = 'backup'
|
||||||
except configparser.NoSectionError:
|
elif config.has_section('default'):
|
||||||
section = 'default'
|
section = 'default'
|
||||||
inputs = config.get(section, 'inputs')
|
else:
|
||||||
|
raise configparser.NoSectionError('backup')
|
||||||
|
|
||||||
config_args['inputs'] = list(set(_expand_inputs([i.strip() for i in inputs.split(',')], user)))
|
# inputs and backup_dir are optional, since -i and -o can give them. Missing ones are reported by check_params()
|
||||||
|
inputs = config.get(section, 'inputs', fallback=None)
|
||||||
|
|
||||||
|
if inputs is not None:
|
||||||
|
config_args['inputs'] = list(set(_expand_inputs([i.strip() for i in inputs.split(',')], user)))
|
||||||
|
|
||||||
# '~' is expanded later, since for remote backups it refers to the home on the server
|
# '~' is expanded later, since for remote backups it refers to the home on the server
|
||||||
config_args['output'] = config.get(section, 'backup_dir')
|
config_args['output'] = config.get(section, 'backup_dir', fallback=None)
|
||||||
|
|
||||||
exclude = config.get(section, 'exclude', fallback='')
|
exclude = config.get(section, 'exclude', fallback='')
|
||||||
config_args['exclude'] = [e.strip() for e in exclude.split(',') if e.strip() != '']
|
config_args['exclude'] = [e.strip() for e in exclude.split(',') if e.strip() != '']
|
||||||
|
|||||||
@@ -170,6 +170,15 @@ ssh_keyfile=/keys/id
|
|||||||
assert b.keep == 0
|
assert b.keep == 0
|
||||||
assert (b.ssh_host, b.ssh_user, b.ssh_keyfile) == ('cli-host', 'cli-user', '/cli/key')
|
assert (b.ssh_host, b.ssh_user, b.ssh_keyfile) == ('cli-host', 'cli-user', '/cli/key')
|
||||||
|
|
||||||
|
def test_inputs_and_output_only_from_cli(self, run_cli: RunCli, tmp_path: Path) -> None:
|
||||||
|
config_file = tmp_path / 'simple_backup.conf'
|
||||||
|
config_file.write_text('[backup]\nkeep=3\n')
|
||||||
|
|
||||||
|
assert run_cli('-c', str(config_file), '-i', '/cli/in', '-o', '/cli/out') == 0
|
||||||
|
|
||||||
|
b = last_backup()
|
||||||
|
assert (b.inputs, b.output, b.keep) == (['/cli/in'], '/cli/out', 3)
|
||||||
|
|
||||||
def test_ssh_host_and_user_from_cli_and_config(self, run_cli: RunCli, config: Callable[[str], str]) -> None:
|
def test_ssh_host_and_user_from_cli_and_config(self, run_cli: RunCli, config: Callable[[str], str]) -> None:
|
||||||
config_file = config('backup_dir=/backups\n[server]\nssh_user=user')
|
config_file = config('backup_dir=/backups\n[server]\nssh_user=user')
|
||||||
|
|
||||||
|
|||||||
@@ -224,9 +224,15 @@ backup_dir=/backups
|
|||||||
sb._read_config(config_file, USER)
|
sb._read_config(config_file, USER)
|
||||||
|
|
||||||
|
|
||||||
|
def test_inputs_and_backup_dir_optional(tmp_path: Path) -> None:
|
||||||
|
config_args = sb._read_config(write_config(tmp_path, '[backup]\nkeep=2\n'), USER)
|
||||||
|
|
||||||
|
assert config_args['inputs'] is None
|
||||||
|
assert config_args['output'] is None
|
||||||
|
assert config_args['keep'] == 2
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('text', [
|
@pytest.mark.parametrize('text', [
|
||||||
'[backup]\nbackup_dir=/backups\n',
|
|
||||||
'[backup]\ninputs=/tmp\n',
|
|
||||||
'[other]\ninputs=/tmp\n',
|
'[other]\ninputs=/tmp\n',
|
||||||
'inputs=/tmp\n',
|
'inputs=/tmp\n',
|
||||||
])
|
])
|
||||||
|
|||||||
Reference in New Issue
Block a user