Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: pass --verbose and -v to snapcraft_legacy #4024

Merged
merged 3 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 54 additions & 42 deletions snapcraft_legacy/cli/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,50 +72,50 @@ def __repr__(self):
_ALL_PROVIDERS = _SUPPORTED_PROVIDERS + _HIDDEN_PROVIDERS
_PROVIDER_OPTIONS: List[Dict[str, Any]] = [
dict(
param_decls="--target-arch",
param_decls=["--target-arch"],
metavar="<arch>",
help="Target architecture to cross compile to",
supported_providers=["host", "lxd", "multipass"],
),
dict(
param_decls="--debug",
param_decls=["--debug"],
is_flag=True,
help="Shells into the environment if the build fails.",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--shell",
param_decls=["--shell"],
is_flag=True,
help="Shells into the environment in lieu of the step to run.",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--offline",
param_decls=["--offline"],
is_flag=True,
help="Operate in offline mode.",
envvar="SNAPCRAFT_OFFLINE",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--shell-after",
param_decls=["--shell-after"],
is_flag=True,
help="Shells into the environment after the step has run.",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--destructive-mode",
param_decls=["--destructive-mode"],
is_flag=True,
help="Forces snapcraft to try and use the current host to build (implies `--provider=host`).",
supported_providers=["host", "managed-host"],
),
dict(
param_decls="--use-lxd",
param_decls=["--use-lxd"],
is_flag=True,
help="Forces snapcraft to use LXD to build (implies `--provider=lxd`).",
supported_providers=["lxd"],
),
dict(
param_decls="--provider",
param_decls=["--provider"],
envvar="SNAPCRAFT_BUILD_ENVIRONMENT",
show_envvar=False,
help="Build provider to use.",
Expand All @@ -124,21 +124,21 @@ def __repr__(self):
supported_providers=_ALL_PROVIDERS,
),
dict(
param_decls="--http-proxy",
param_decls=["--http-proxy"],
metavar="<http-proxy>",
help="HTTP proxy for host build environments.",
envvar="http_proxy",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--https-proxy",
param_decls=["--https-proxy"],
metavar="<https-proxy>",
help="HTTPS proxy for host build environments.",
envvar="https_proxy",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--add-ca-certificates",
param_decls=["--add-ca-certificates"],
metavar="<certificate-path>",
help="File or directory containing CA certificates to install into build environments.",
envvar="SNAPCRAFT_ADD_CA_CERTIFICATES",
Expand All @@ -148,22 +148,22 @@ def __repr__(self):
),
),
dict(
param_decls="--bind-ssh",
param_decls=["--bind-ssh"],
is_flag=True,
help="Bind ~/.ssh directory to locally-run build environments.",
envvar="SNAPCRAFT_BIND_SSH",
supported_providers=["lxd", "multipass"],
),
dict(
param_decls="--enable-developer-debug",
param_decls=["--enable-developer-debug"],
is_flag=True,
help="Enable developer debug logging.",
envvar="SNAPCRAFT_ENABLE_DEVELOPER_DEBUG",
supported_providers=["host", "lxd", "managed-host", "multipass"],
hidden=True,
),
dict(
param_decls="--enable-manifest",
param_decls=["--enable-manifest"],
is_flag=True,
type=BoolParamType(),
help="Generate snap manifest.",
Expand All @@ -172,41 +172,47 @@ def __repr__(self):
hidden=True,
),
dict(
param_decls="--manifest-image-information",
param_decls=["--manifest-image-information"],
metavar="<json string>",
help="Set snap manifest image-info",
envvar="SNAPCRAFT_IMAGE_INFO",
supported_providers=["host", "lxd", "managed-host", "multipass"],
hidden=True,
),
dict(
param_decls="--enable-experimental-extensions",
param_decls=["--enable-experimental-extensions"],
is_flag=True,
help="Enable extensions that are experimental and not considered stable.",
envvar="SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--enable-experimental-target-arch",
param_decls=["--enable-experimental-target-arch"],
is_flag=True,
help="Enable experimental `--target-arch` support for core20.",
envvar="SNAPCRAFT_ENABLE_EXPERIMENTAL_TARGET_ARCH",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--ua-token",
param_decls=["--ua-token"],
metavar="<ua-token>",
help="Configure build environment with ESM using specified UA token.",
envvar="SNAPCRAFT_UA_TOKEN",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls="--enable-experimental-ua-services",
param_decls=["--enable-experimental-ua-services"],
is_flag=True,
help="Allow selection of UA services to enable.",
envvar="SNAPCRAFT_ENABLE_EXPERIMENTAL_UA_SERVICES",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
dict(
param_decls=["--verbose", "-v"],
is_flag=True,
help="Show debug information and be more verbose.",
supported_providers=["host", "lxd", "managed-host", "multipass"],
),
]


Expand All @@ -222,7 +228,7 @@ def _add_options(options, func, hidden):
option.pop("supported_providers")

hidden_override = option.pop("hidden", hidden)
click_option = click.option(param_decls, **option, hidden=hidden_override)
click_option = click.option(*param_decls, **option, hidden=hidden_override)
func = click_option(func)
return func

Expand Down Expand Up @@ -264,12 +270,13 @@ def _sanity_check_build_provider_flags(build_provider: str, **kwargs) -> None:
# change defaults, so they are safe to ignore due to filtering
# in get_build_provider_flags().
for option in _PROVIDER_OPTIONS:
key: str = option["param_decls"] # type: ignore
keys: List[str] = option["param_decls"] # type: ignore
supported_providers: List[str] = option["supported_providers"] # type: ignore
if key in sys.argv and build_provider not in supported_providers:
raise click.BadArgumentUsage(
f"{key} cannot be used with build provider {build_provider!r}"
)
for key in keys:
if key in sys.argv and build_provider not in supported_providers:
raise click.BadArgumentUsage(
f"{key} cannot be used with build provider {build_provider!r}"
)

# Check if running as sudo but only if the host is not managed-host where Snapcraft
# runs as root already. This effectively avoids the warning when using the default
Expand Down Expand Up @@ -336,9 +343,11 @@ def get_build_provider(**kwargs) -> str:

def _param_decls_to_kwarg(key: str) -> str:
"""Format a param_decls to keyword argument name."""

# Drop leading "--".
key = key.replace("--", "", 1)
# Drop leading dashes ("-" or "--")
if key.startswith("--"):
key = key.replace("--", "", 1)
elif key.startswith("-"):
key = key.replace("-", "", 1)

# Convert dashes to underscores.
return key.replace("-", "_")
Expand All @@ -354,27 +363,30 @@ def get_build_provider_flags(build_provider: str, **kwargs) -> Dict[str, str]:
raise RuntimeError(f"Invalid build provider: {build_provider}")

for option in _PROVIDER_OPTIONS:
key: str = option["param_decls"] # type: ignore
keys: List[str] = option["param_decls"] # type: ignore
is_flag: bool = option.get("is_flag", False) # type: ignore
envvar: Optional[str] = option.get("envvar") # type: ignore
supported_providers: List[str] = option["supported_providers"] # type: ignore

# Skip --provider option.
if key == "--provider":
continue
for key in keys:
# TODO: skip single character flags (e.g. keep `verbose` but discard `v`)
sergiusens marked this conversation as resolved.
Show resolved Hide resolved

# Skip options that do not apply to configured provider.
if build_provider not in supported_providers:
continue
# Skip --provider option.
if key == "--provider":
continue

# Skip boolean flags that have not been set.
key_formatted = _param_decls_to_kwarg(key)
if is_flag and not kwargs.get(key_formatted):
continue
# Skip options that do not apply to configured provider.
if build_provider not in supported_providers:
continue

# Skip boolean flags that have not been set.
key_formatted = _param_decls_to_kwarg(key)
if is_flag and not kwargs.get(key_formatted):
continue

# Add build provider flag using envvar as key.
if envvar is not None and key_formatted in kwargs:
build_provider_flags[envvar] = kwargs[key_formatted]
# Add build provider flag using envvar as key.
if envvar is not None and key_formatted in kwargs:
build_provider_flags[envvar] = kwargs[key_formatted]

return build_provider_flags

Expand Down
9 changes: 9 additions & 0 deletions snapcraft_legacy/cli/_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,26 @@ def configure_requests_ca() -> None:
@click.pass_context
@add_provider_options(hidden=True)
@click.option("--debug", "-d", is_flag=True)
@click.option("--verbose", "-v", is_flag=True)
def run(ctx, debug, catch_exceptions=False, **kwargs):
"""Snapcraft is a delightful packaging tool."""

is_snapcraft_developer_debug = kwargs["enable_developer_debug"]
verbose = kwargs["verbose"]

if is_snapcraft_developer_debug:
if verbose:
raise click.BadArgumentUsage(
"The 'enable-developer-debug' and 'verbose' options are "
"mutually exclusive."
)
log_level = logging.DEBUG
click.echo(
"Starting snapcraft {} from {}.".format(
snapcraft_legacy.__version__, os.path.dirname(__file__)
)
)
# verbose is the default log level so no need to check if `--verbose` was passed
else:
log_level = logging.INFO

Expand Down
Loading