Skip to content

Commit

Permalink
Add a global config file, rename the user config
Browse files Browse the repository at this point in the history
This will read the global (/etc/pulp/cli_config.toml) first and merge
with the user local ones (settings.toml, cli_config.toml) in that order.
Creating and editing config files default to cli_config now.

fixes pulp#290
fixes pulp#293
  • Loading branch information
mdellweg committed Jul 16, 2021
1 parent 901cfb3 commit 9b72266
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.egg-info
__pycache__/
build/
tests/settings.toml
tests/cli.toml
/.ci/settings/
!/.ci/settings/settings.py
site/
1 change: 1 addition & 0 deletions CHANGES/290.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added lookup for a global config file ``/etc/pulp/cli.toml``.
1 change: 1 addition & 0 deletions CHANGES/293.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed default config location to ``<app-dir>/cli.toml``. The old file will still be read.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ lint:
mypy
@echo "🙊 Code 🙈 LGTM 🙉 !"

tests/settings.toml:
tests/cli.toml:
cp $@.example $@
@echo "In order to configure the tests to talk to your test server, you might need to edit $@ ."

test: | tests/settings.toml
test: | tests/cli.toml
pytest -v tests

servedocs:
Expand Down
19 changes: 13 additions & 6 deletions pulpcore/cli/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
except ImportError:
HAS_CLICK_SHELL = False

from pulpcore.cli.common.config import CONFIG_LOCATION, config, config_options, validate_config
from pulpcore.cli.common.config import CONFIG_LOCATIONS, config, config_options, validate_config
from pulpcore.cli.common.context import PulpContext
from pulpcore.cli.common.debug import debug

Expand Down Expand Up @@ -43,11 +43,18 @@ def _config_callback(ctx: click.Context, param: Any, value: Optional[str]) -> No
if value:
config = toml.load(value)
else:
try:
config = toml.load(CONFIG_LOCATION)
except FileNotFoundError:
# No config, but also none requested
return
config = {"cli": {}}
for location in CONFIG_LOCATIONS:
try:
new_config = toml.load(location)
# level 1 merge
for key in new_config:
if key in config:
config[key].update(new_config[key])
else:
config[key] = new_config[key]
except FileNotFoundError:
pass
profile: str = "cli"
if PROFILE_KEY in ctx.meta:
profile = "cli-" + ctx.meta[PROFILE_KEY]
Expand Down
12 changes: 8 additions & 4 deletions pulpcore/cli/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
_ = gettext.gettext
_T = TypeVar("_T")

CONFIG_LOCATION = str(Path(click.utils.get_app_dir("pulp"), "settings.toml"))
CONFIG_LOCATIONS = [
"/etc/pulp/cli.toml",
str(Path(click.utils.get_app_dir("pulp"), "settings.toml")),
str(Path(click.utils.get_app_dir("pulp"), "cli.toml")),
]
FORMAT_CHOICES = ["json", "yaml", "none"]
SETTINGS = [
"base_url",
Expand Down Expand Up @@ -117,7 +121,7 @@ def config() -> None:
@click.option("--interactive", "-i", is_flag=True)
@click.option("--editor", "-e", is_flag=True, help=_("Edit the config file in an editor"))
@click.option("--overwrite", "-o", is_flag=True, help=_("Overwite any existing config file"))
@click.option("--location", default=CONFIG_LOCATION, type=click.Path(resolve_path=True))
@click.option("--location", default=CONFIG_LOCATIONS[-1], type=click.Path(resolve_path=True))
@click.pass_context
def create(
ctx: click.Context,
Expand Down Expand Up @@ -172,7 +176,7 @@ def prompt_config_option(name: str) -> Any:


@config.command(help=_("Open the settings config file in an editor"))
@click.option("--location", default=CONFIG_LOCATION, type=click.Path(resolve_path=True))
@click.option("--location", default=CONFIG_LOCATIONS[-1], type=click.Path(resolve_path=True))
def edit(location: str) -> None:
if not Path(location).exists():
raise click.ClickException(
Expand All @@ -198,7 +202,7 @@ def edit(location: str) -> None:


@config.command(help=_("Validate a pulp-cli config file"))
@click.option("--location", default=CONFIG_LOCATION, type=click.Path(resolve_path=True))
@click.option("--location", default=CONFIG_LOCATIONS[-1], type=click.Path(resolve_path=True))
@click.option("--strict", is_flag=True, help=_("Validate that all settings are present"))
def validate(location: str, strict: bool) -> None:
try:
Expand Down
4 changes: 2 additions & 2 deletions pytest_pulp_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def pulp_cli_settings(tmp_path_factory):
It is most likely not useful to be included standalone.
The `pulp_cli_env` fixture, however depends on it and sets $XDG_CONFIG_HOME up accordingly.
"""
settings = toml.load("tests/settings.toml")
settings = toml.load("tests/cli.toml")
settings_path = tmp_path_factory.mktemp("config", numbered=False)
(settings_path / "pulp").mkdir(parents=True)
with open(settings_path / "pulp" / "settings.toml", "w") as settings_file:
with open(settings_path / "pulp" / "cli.toml", "w") as settings_file:
toml.dump(settings, settings_file)
yield settings_path, settings

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/scripts/test_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# shellcheck source=tests/scripts/config.source
. "$(dirname "$(realpath "$0")")"/config.source

good_settings="${XDG_CONFIG_HOME}/pulp/settings.toml"
bad_settings="bad_settings.toml"
profile_settings="profile_settings.toml"
good_settings="${XDG_CONFIG_HOME}/pulp/cli.toml"
bad_settings="bad_config.toml"
profile_settings="profile_config.toml"
test_settings="test.toml"

export XDG_CONFIG_HOME=/nowhere
Expand Down

0 comments on commit 9b72266

Please sign in to comment.