Skip to content

Commit

Permalink
Add config profile option
Browse files Browse the repository at this point in the history
This allows to select different profiles from the config file to load a
different set of default values. The default profile is the one with the
section name "cli". Other profiles can be provided by adding sections
with names like "cli-<profile>". The profile "cli-sandbox" e.g. can be
selected with "pulp --profile sandbox <...>".

[noissue]
  • Loading branch information
mdellweg committed Feb 20, 2021
1 parent 0aabdf4 commit 3f7cc1f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES/145.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added the ability to include multiple server profiles into the pulp cli config.
34 changes: 30 additions & 4 deletions pulpcore/cli/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,37 @@
# Main entry point


PROFILE_KEY = f"{__name__}.profile"


def _config_profile_callback(ctx: click.Context, param: Any, value: Optional[str]) -> Optional[str]:
if value is not None:
ctx.meta[PROFILE_KEY] = value
return value


def _config_callback(ctx: click.Context, param: Any, value: Optional[str]) -> None:
if ctx.default_map:
return

if value:
ctx.default_map = toml.load(value)["cli"]
config = toml.load(value)
else:
default_config_path = os.path.join(click.utils.get_app_dir("pulp"), "settings.toml")

try:
ctx.default_map = toml.load(default_config_path)["cli"]
config = toml.load(default_config_path)
except FileNotFoundError:
pass
# No config, but also none requested
return

profile: str = "cli"
if PROFILE_KEY in ctx.meta:
profile = "cli-" + ctx.meta[PROFILE_KEY]
try:
ctx.default_map = config[profile]
except KeyError:
raise click.ClickException(_("Profile named '{profile}' not found.").format(profile=profile))


CONFIG_OPTIONS = [
Expand Down Expand Up @@ -58,7 +76,15 @@ def config_options(command: Callable[..., Any]) -> Callable[..., Any]:


@click.group()
@click.version_option(prog_name="pulp3 command line interface")
@click.version_option(prog_name=_("pulp3 command line interface"))
@click.option(
"--profile",
"-p",
help=_("Config profile to use"),
callback=_config_profile_callback,
expose_value=False,
is_eager=True,
)
@click.option(
"--config",
type=click.Path(resolve_path=True),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/sh

# shellcheck source=tests/scripts/config.source
. "$(dirname "$(dirname "$(realpath "$0")")")"/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"
test_settings="test.toml"

export XDG_CONFIG_HOME=/nowhere
Expand All @@ -29,6 +30,14 @@ expect_fail pulp --username test --password test --client "/some/path" status
expect_fail pulp --key "/some/path" file remote list


# CONFIG PROFILE

cp "$bad_settings" "$profile_settings"
sed -e 's/\[cli\]/[cli-profile1]/' "$good_settings" >> "$profile_settings"

expect_fail pulp --config "$profile_settings" file repository list
expect_succ pulp --config "$profile_settings" --profile profile1 file repository list

# CONFIG COMMAND

expect_fail pulp config edit --location $test_settings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

# shellcheck source=tests/scripts/config.source
. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source
. "$(dirname "$(realpath "$0")")"/config.source

expect_succ pulp -v status

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# shellcheck source=tests/scripts/config.source
. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source
. "$(dirname "$(realpath "$0")")"/config.source

# skip this test if pygments is not installed
pip show pygments > /dev/null || exit 3
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TEST_SCRIPTS = [
pytest.param(
path.resolve(),
id=path.stem,
id=("" if path.parent.name == "scripts" else path.parent.name + ".") + path.stem,
marks=[] if path.parent.name == "scripts" else getattr(pytest.mark, path.parent.name),
)
for path in Path("tests/scripts").glob("**/test_*.sh")
Expand Down

0 comments on commit 3f7cc1f

Please sign in to comment.