Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: allow $GCALCLI_CONFIG to accept file paths vs. only dir paths #754

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions gcalcli/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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.
Expand All @@ -322,17 +322,18 @@ 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():
rc_paths.append(utils.shorten_path(legacy_rc_path))

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,
Expand Down
3 changes: 1 addition & 2 deletions gcalcli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 23 additions & 5 deletions gcalcli/env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import pathlib
from typing import Optional

import platformdirs

from . import __program__
Expand All @@ -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')
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading