In "borgmatic --help", don't expand $HOME in listing of default "--config" paths.

This commit is contained in:
Dan Helfman 2020-01-02 10:37:31 -08:00
parent 1995c80e60
commit d0c533555e
5 changed files with 21 additions and 7 deletions

3
NEWS
View file

@ -1,3 +1,6 @@
1.4.22.dev0
* In "borgmatic --help", don't expand $HOME in listing of default "--config" paths.
1.4.21
* #268: Override particular configuration options from the command-line via "--override" flag. See
the documentation for more information:

View file

@ -106,7 +106,8 @@ def parse_arguments(*unparsed_arguments):
Given command-line arguments with which this script was invoked, parse the arguments and return
them as a dict mapping from subparser name (or "global") to an argparse.Namespace instance.
'''
config_paths = collect.get_default_config_paths()
config_paths = collect.get_default_config_paths(expand_home=True)
unexpanded_config_paths = collect.get_default_config_paths(expand_home=False)
global_parser = ArgumentParser(add_help=False)
global_group = global_parser.add_argument_group('global arguments')
@ -118,7 +119,7 @@ def parse_arguments(*unparsed_arguments):
dest='config_paths',
default=config_paths,
help='Configuration filenames or directories, defaults to: {}'.format(
' '.join(config_paths)
' '.join(unexpanded_config_paths)
),
)
global_group.add_argument(

View file

@ -1,15 +1,17 @@
import os
def get_default_config_paths():
def get_default_config_paths(expand_home=True):
'''
Based on the value of the XDG_CONFIG_HOME and HOME environment variables, return a list of
default configuration paths. This includes both system-wide configuration and configuration in
the current user's home directory.
Don't expand the home directory ($HOME) if the expand home flag is False.
'''
user_config_directory = os.getenv('XDG_CONFIG_HOME') or os.path.expandvars(
os.path.join('$HOME', '.config')
)
user_config_directory = os.getenv('XDG_CONFIG_HOME') or os.path.join('$HOME', '.config')
if expand_home:
user_config_directory = os.path.expandvars(user_config_directory)
return [
'/etc/borgmatic/config.yaml',

View file

@ -1,6 +1,6 @@
from setuptools import find_packages, setup
VERSION = '1.4.21'
VERSION = '1.4.22.dev0'
setup(

View file

@ -21,6 +21,14 @@ def test_get_default_config_paths_prefers_xdg_config_home_for_user_config_path()
assert '/home/user/.etc/borgmatic/config.yaml' in config_paths
def test_get_default_config_paths_does_not_expand_home_when_false():
flexmock(module.os, environ={'HOME': '/home/user'})
config_paths = module.get_default_config_paths(expand_home=False)
assert '$HOME/.config/borgmatic/config.yaml' in config_paths
def test_collect_config_filenames_collects_given_files():
config_paths = ('config.yaml', 'other.yaml')
flexmock(module.os.path).should_receive('isdir').and_return(False)