diff --git a/docs/README.md b/docs/README.md index 57d820b..1046397 100644 --- a/docs/README.md +++ b/docs/README.md @@ -169,6 +169,17 @@ Example: [calendars] default-calendars = ["Personal", "Work"] ignore-calendars = ["Boring stuff", "Holidays"] + +[output] +week-start = "monday" +``` + +You can also use the $GCALCLI_CONFIG environment variable to customize which +config file/directory to use, which is useful if you need to dynamically switch +between different sets of configuration. For example: + +```shell +GCALCLI_CONFIG=~/.config/gcalcli/config.tuesdays.toml gcalcli add ``` #### Using cli args from a file (and gcalclirc flag file) diff --git a/gcalcli/argparsers.py b/gcalcli/argparsers.py index baa5b95..c67d3ad 100644 --- a/gcalcli/argparsers.py +++ b/gcalcli/argparsers.py @@ -34,7 +34,7 @@ 'is no longer supported.', }, '--config-folder': { - 'default': utils.shorten_path(env.default_config_dir()), + 'default': utils.shorten_path(env.config_dir()), 'type': pathlib.Path, 'help': 'Optional directory used to load config files. Deprecated: ' 'prefer $GCALCLI_CONFIG.', @@ -300,7 +300,7 @@ class RawDescArgDefaultsHelpFormatter( the command-line arguments listed below. $GCALCLI_CONFIG={config_dir} - Path to user config directory. + Path to user config directory or file. Note: this path is also used to determine fallback paths to check for cache/oauth files to be migrated into their proper data dir paths. @@ -322,8 +322,9 @@ class RawDescArgDefaultsHelpFormatter( @parser_allow_deprecated(name='program') def get_argument_parser(): - config_dir = utils.shorten_path(env.default_config_dir()) # Shorten path to ~/PATH if possible for easier readability. + config_dir = utils.shorten_path(env.config_dir()) + config_path = utils.shorten_path(env.explicit_config_path() or config_dir) rc_paths = [config_dir.joinpath('gcalclirc')] legacy_rc_path = pathlib.Path.home().joinpath('.gcalclirc') if legacy_rc_path.exists(): @@ -331,8 +332,8 @@ def get_argument_parser(): parser = argparse.ArgumentParser( description=DESCRIPTION.format( - config_dir=config_dir, - config_file=config_dir.joinpath('config.toml'), + config_dir=config_path, + config_file=utils.shorten_path(env.config_file()), rc_paths=', '.join(str(p) for p in rc_paths), ), formatter_class=RawDescArgDefaultsHelpFormatter, diff --git a/gcalcli/cli.py b/gcalcli/cli.py index 09fe3ee..1c2ffb4 100755 --- a/gcalcli/cli.py +++ b/gcalcli/cli.py @@ -114,8 +114,7 @@ def main(): parser.print_usage() sys.exit(1) - config_dir = env.default_config_dir() - config_filepath = config_dir.joinpath('config.toml') + config_filepath = env.config_file() if config_filepath.exists(): with config_filepath.open('rb') as config_file: opts_from_config = config.Config.from_toml(config_file) diff --git a/gcalcli/env.py b/gcalcli/env.py index f3f0d31..d4f93f4 100644 --- a/gcalcli/env.py +++ b/gcalcli/env.py @@ -1,5 +1,7 @@ import os import pathlib +from typing import Optional + import platformdirs from . import __program__ @@ -9,8 +11,24 @@ def default_data_dir() -> pathlib.Path: return platformdirs.user_data_path(__program__) -def default_config_dir() -> pathlib.Path: - return pathlib.Path( - os.environ.get('GCALCLI_CONFIG') - or platformdirs.user_config_dir(__program__) - ) +def explicit_config_path() -> Optional[pathlib.Path]: + config_path = os.environ.get('GCALCLI_CONFIG') + return pathlib.Path(config_path) if config_path else None + + +def config_dir() -> pathlib.Path: + from_env = explicit_config_path() + if from_env: + return from_env.parent if from_env.is_file() else from_env + return pathlib.Path(platformdirs.user_config_dir(__program__)) + + +def config_file() -> pathlib.Path: + config_path = explicit_config_path() + if config_path and config_path.is_file(): + # Special case: $GCALCLI_CONFIG points directly to file, not necessarily + # named "config.toml". + return config_path + if not config_path: + config_path = pathlib.Path(platformdirs.user_config_dir(__program__)) + return config_path.joinpath('config.toml') diff --git a/tests/cli/__snapshot__/test-02-test_prints_correct_help.snap b/tests/cli/__snapshot__/test-02-test_prints_correct_help.snap index 60bac1e..e91a527 100644 --- a/tests/cli/__snapshot__/test-02-test_prints_correct_help.snap +++ b/tests/cli/__snapshot__/test-02-test_prints_correct_help.snap @@ -16,7 +16,7 @@ configuration: the command-line arguments listed below. $GCALCLI_CONFIG=/some/gcalcli/config - Path to user config directory. + Path to user config directory or file. Note: this path is also used to determine fallback paths to check for cache/oauth files to be migrated into their proper data dir paths.