Resolve default config path after the user is known

The default config path was computed before -u was parsed, so
running as root without sudo/doas used '~None/...' even when the
user was given with -u. Expand '~' only after the user is detected.
This commit is contained in:
Fuxino
2026-10-08 17:45:02 +02:00
parent 7e7d11c520
commit dadc4b0265
+10 -5
View File
@@ -652,9 +652,6 @@ def _detect_user() -> Optional[str]:
def _parse_arguments() -> argparse.Namespace:
user = _detect_user()
homedir = os.path.expanduser(f'~{user}')
parser = argparse.ArgumentParser(prog='simple_backup',
description='Simple backup script written in Python that uses rsync to copy files',
epilog='See simple_backup(1) manpage for full documentation',
@@ -662,7 +659,7 @@ def _parse_arguments() -> argparse.Namespace:
parser.add_argument('-v', '--verbose', action='store_true',
help='More verbose output')
parser.add_argument('-c', '--config', default=f'{homedir}/.config/simple_backup/simple_backup.conf',
parser.add_argument('-c', '--config', default='~/.config/simple_backup/simple_backup.conf',
help='Specify location of configuration file')
parser.add_argument('-i', '--inputs', nargs='+',
help='Paths/files to backup')
@@ -896,8 +893,16 @@ def simple_backup() -> int:
except NameError:
pass
config_file = args.config
# Expand '~' to the home of the user running the backup, not root's
if config_file.startswith('~/') and user is not None:
config_file = f'~{user}{config_file[1:]}'
config_file = os.path.expanduser(config_file)
try:
config_args = _read_config(args.config, user)
config_args = _read_config(config_file, user)
except (configparser.NoSectionError, configparser.NoOptionError):
logger.critical('Bad configuration file')