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:
@@ -652,9 +652,6 @@ def _detect_user() -> Optional[str]:
|
|||||||
|
|
||||||
|
|
||||||
def _parse_arguments() -> argparse.Namespace:
|
def _parse_arguments() -> argparse.Namespace:
|
||||||
user = _detect_user()
|
|
||||||
homedir = os.path.expanduser(f'~{user}')
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(prog='simple_backup',
|
parser = argparse.ArgumentParser(prog='simple_backup',
|
||||||
description='Simple backup script written in Python that uses rsync to copy files',
|
description='Simple backup script written in Python that uses rsync to copy files',
|
||||||
epilog='See simple_backup(1) manpage for full documentation',
|
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',
|
parser.add_argument('-v', '--verbose', action='store_true',
|
||||||
help='More verbose output')
|
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')
|
help='Specify location of configuration file')
|
||||||
parser.add_argument('-i', '--inputs', nargs='+',
|
parser.add_argument('-i', '--inputs', nargs='+',
|
||||||
help='Paths/files to backup')
|
help='Paths/files to backup')
|
||||||
@@ -896,8 +893,16 @@ def simple_backup() -> int:
|
|||||||
except NameError:
|
except NameError:
|
||||||
pass
|
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:
|
try:
|
||||||
config_args = _read_config(args.config, user)
|
config_args = _read_config(config_file, user)
|
||||||
except (configparser.NoSectionError, configparser.NoOptionError):
|
except (configparser.NoSectionError, configparser.NoOptionError):
|
||||||
logger.critical('Bad configuration file')
|
logger.critical('Bad configuration file')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user