Skip to content

Commit

Permalink
Add confirmation on invalid config
Browse files Browse the repository at this point in the history
fixes pulp#156
  • Loading branch information
mdellweg committed Apr 8, 2021
1 parent 61119d1 commit 1fcecc1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGES/156.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a confirmation whether to continue with invalid config.
57 changes: 36 additions & 21 deletions pulpcore/cli/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gettext
import os
from typing import Any, Callable, Optional
import sys
from typing import Any, Callable, Dict, Optional

import click
import pkg_resources
Expand All @@ -19,6 +20,13 @@


PROFILE_KEY = f"{__name__}.profile"
FORMAT_CHOICES = ["json", "yaml", "none"]


def _validate_config(config: Dict[str, Any]) -> bool:
if "format" in config and config["format"].lower() not in FORMAT_CHOICES:
raise ValueError(_("'format' is not one of {}").format(FORMAT_CHOICES))
return True


def _config_profile_callback(ctx: click.Context, param: Any, value: Optional[str]) -> Optional[str]:
Expand All @@ -31,26 +39,33 @@ def _config_callback(ctx: click.Context, param: Any, value: Optional[str]) -> No
if ctx.default_map:
return

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

try:
config = toml.load(default_config_path)
except FileNotFoundError:
# 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)
)
if value:
config = toml.load(value)
else:
default_config_path = os.path.join(click.utils.get_app_dir("pulp"), "settings.toml")

try:
config = toml.load(default_config_path)
except FileNotFoundError:
# No config, but also none requested
return
profile: str = "cli"
if PROFILE_KEY in ctx.meta:
profile = "cli-" + ctx.meta[PROFILE_KEY]
try:
_validate_config(config[profile])
ctx.default_map = config[profile]
except KeyError:
raise click.ClickException(
_("Profile named '{profile}' not found.").format(profile=profile)
)
except ValueError as e:
if sys.stdout.isatty():
click.echo(_("Config file failed to parse. ({}).").format(e), err=True)
if click.confirm(_("Continue without config?")):
return
raise click.ClickException(_("Aborted."))


CONFIG_OPTIONS = [
Expand All @@ -65,7 +80,7 @@ def _config_callback(ctx: click.Context, param: Any, value: Optional[str]) -> No
click.option("--verify-ssl/--no-verify-ssl", default=True, help=_("Verify SSL connection")),
click.option(
"--format",
type=click.Choice(["json", "yaml", "none"], case_sensitive=False),
type=click.Choice(FORMAT_CHOICES, case_sensitive=False),
default="json",
help=_("Format of the response"),
),
Expand Down
4 changes: 0 additions & 4 deletions pulpcore/cli/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ def config() -> None:
@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=LOCATION, type=click.Path(resolve_path=True))
@click.option(
"--format", type=click.Choice(["json", "yaml", "none"], case_sensitive=False), default="json"
)
@click.pass_context
def create(
ctx: click.Context,
Expand Down Expand Up @@ -68,7 +65,6 @@ def prompt_config_option(name: str) -> Any:
for setting in SETTINGS:
settings[setting] = locals()[setting] or ""

output = f"[cli]\n{toml.dumps(settings)}"
output = toml.dumps({"cli": settings})

if editor:
Expand Down

0 comments on commit 1fcecc1

Please sign in to comment.