diff --git a/CHANGES/tba.feature b/CHANGES/tba.feature new file mode 100644 index 000000000..d52f47aa6 --- /dev/null +++ b/CHANGES/tba.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..a786cc143 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 '{}' not found.".format(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),