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 <...>".
  • Loading branch information
mdellweg committed Feb 17, 2021
1 parent 0aabdf4 commit 026163e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/tba.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 '{}' not found.".format(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

0 comments on commit 026163e

Please sign in to comment.