diff --git a/CHANGES/145.feature b/CHANGES/145.feature new file mode 100644 index 000000000..d52f47aa6 --- /dev/null +++ b/CHANGES/145.feature @@ -0,0 +1 @@ +Added the ability to include multiple server profiles into the pulp cli config. diff --git a/pulpcore/cli/common/__init__.py b/pulpcore/cli/common/__init__.py index 80bc52719..56c4ffa96 100644 --- a/pulpcore/cli/common/__init__.py +++ b/pulpcore/cli/common/__init__.py @@ -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 = [ @@ -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), diff --git a/tests/scripts/pulpcore/test_config.sh b/tests/scripts/test_config.sh similarity index 78% rename from tests/scripts/pulpcore/test_config.sh rename to tests/scripts/test_config.sh index e0941f718..8a3af33d5 100755 --- a/tests/scripts/pulpcore/test_config.sh +++ b/tests/scripts/test_config.sh @@ -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 @@ -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 diff --git a/tests/scripts/pulpcore/test_debug_api.sh b/tests/scripts/test_debug_api.sh similarity index 72% rename from tests/scripts/pulpcore/test_debug_api.sh rename to tests/scripts/test_debug_api.sh index 6216b1e37..35360a193 100755 --- a/tests/scripts/pulpcore/test_debug_api.sh +++ b/tests/scripts/test_debug_api.sh @@ -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 diff --git a/tests/scripts/pulpcore/test_output.sh b/tests/scripts/test_output.sh similarity index 88% rename from tests/scripts/pulpcore/test_output.sh rename to tests/scripts/test_output.sh index 659e7c03b..04adcd8a7 100755 --- a/tests/scripts/pulpcore/test_output.sh +++ b/tests/scripts/test_output.sh @@ -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 diff --git a/tests/test_scripts.py b/tests/test_scripts.py index 58ea6337e..eda6eb699 100644 --- a/tests/test_scripts.py +++ b/tests/test_scripts.py @@ -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")