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

New config.toml: rename default-calendar->default-calendars, add note config.toml file into --help #746

Merged
merged 2 commits into from
Sep 12, 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
2 changes: 1 addition & 1 deletion data/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "object",
"description": "Settings about the set of calendars gcalcli operates on",
"properties": {
"default-calendar": {
"default-calendars": {
"type": "array",
"items": { "type": "string" },
"description": "Calendar(s) to use as default for certain commands when no explicit target calendar is otherwise specified"
Expand Down
31 changes: 26 additions & 5 deletions gcalcli/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,18 @@ class RawDescArgDefaultsHelpFormatter(
%(prog)s supports a few other configuration mechanisms in addition to
the command-line arguments listed below.

$GCALCLI_CONFIG={config_dir!r}
$GCALCLI_CONFIG={config_dir}
Path to user config directory.
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.

{config_file}
A toml config file where some general-purpose settings can be
configured.
Schema:
https://raw.githubusercontent.com/insanum/gcalcli/HEAD/data/config-schema.json

gcalclirc @ {rc_paths}
A flag file listing additional command-line args to always pass,
one per line.
Expand All @@ -308,17 +314,32 @@ class RawDescArgDefaultsHelpFormatter(
"""


def shorten_path(path: pathlib.Path) -> pathlib.Path:
"""Try to shorten path using special characters like ~.

Returns original path unmodified if it can't be shortened.
"""
tilde_home = pathlib.Path('~')
expanduser_len = len(tilde_home.expanduser().parts)
if path.parts[:expanduser_len] == tilde_home.expanduser().parts:
return tilde_home.joinpath(*path.parts[expanduser_len:])
return path


@parser_allow_deprecated(name='program')
def get_argument_parser():
config_dir = env.default_config_dir()
rc_paths = [pathlib.Path(config_dir).joinpath('gcalclirc')]
config_dir = shorten_path(env.default_config_dir())
# Shorten path to ~/PATH if possible for easier readability.
rc_paths = [config_dir.joinpath('gcalclirc')]
legacy_rc_path = pathlib.Path.home().joinpath('.gcalclirc')
if legacy_rc_path.exists():
rc_paths.append(legacy_rc_path)
rc_paths.append(shorten_path(legacy_rc_path))

parser = argparse.ArgumentParser(
description=DESCRIPTION.format(
config_dir=config_dir, rc_paths=', '.join(str(p) for p in rc_paths)
config_dir=config_dir,
config_file=config_dir.joinpath('config.toml'),
rc_paths=', '.join(str(p) for p in rc_paths),
),
formatter_class=RawDescArgDefaultsHelpFormatter,
fromfile_prefix_chars='@',
Expand Down
2 changes: 1 addition & 1 deletion gcalcli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def main():
with config_filepath.open('rb') as config_file:
config = tomllib.load(config_file)
opts_from_config.defaultCalendar = config.get('calendars', {}).get(
'default-calendar', []
'default-calendars', []
)

if parsed_args.config_folder:
Expand Down
Loading