Skip to content

Commit

Permalink
refactor: de-duplicate core22 and core24 command groups
Browse files Browse the repository at this point in the history
Signed-off-by: Callahan Kovacs <callahan.kovacs@canonical.com>
  • Loading branch information
mr-cal committed May 31, 2024
1 parent ec18739 commit 90d6b68
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 125 deletions.
5 changes: 5 additions & 0 deletions docs/reference/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ provided, the command applies to all parts.

.. include:: commands/lifecycle-commands.rst

Plugins
----------

.. include:: commands/plugins-commands.rst

Extensions
----------

Expand Down
100 changes: 3 additions & 97 deletions snapcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import snapcraft
import snapcraft_legacy
from snapcraft import cli, commands, errors, models, services
from snapcraft import cli, errors, models, services
from snapcraft.extensions import apply_extensions
from snapcraft.models.project import SnapcraftBuildPlanner, apply_root_packages
from snapcraft.parts import set_global_environment
Expand Down Expand Up @@ -271,102 +271,8 @@ def create_app() -> Snapcraft:
extra_loggers={"snapcraft.remote"},
)

app.add_command_group(
"Lifecycle",
[
craft_app_commands.lifecycle.CleanCommand,
craft_app_commands.lifecycle.PullCommand,
craft_app_commands.lifecycle.BuildCommand,
craft_app_commands.lifecycle.StageCommand,
craft_app_commands.lifecycle.PrimeCommand,
commands.PackCommand,
commands.SnapCommand, # Hidden (legacy compatibility)
commands.RemoteBuildCommand,
commands.TryCommand,
],
)
app.add_command_group(
"Plugins",
[
commands.ListPluginsCommand,
commands.PluginsCommand,
],
)
app.add_command_group(
"Extensions",
[
commands.ExpandExtensionsCommand,
commands.ExtensionsCommand,
commands.ListExtensionsCommand,
],
)
app.add_command_group(
"Store Account",
[
commands.StoreExportLoginCommand,
commands.StoreLoginCommand,
commands.StoreLogoutCommand,
commands.StoreWhoAmICommand,
],
)
app.add_command_group(
"Store Snap Names",
[
commands.StoreRegisterCommand,
commands.StoreNamesCommand,
commands.StoreLegacyListRegisteredCommand,
commands.StoreLegacyListCommand,
commands.StoreLegacyMetricsCommand,
commands.StoreLegacyUploadMetadataCommand,
],
)
app.add_command_group(
"Store Snap Release Management",
[
commands.StoreReleaseCommand,
commands.StoreCloseCommand,
commands.StoreStatusCommand,
commands.StoreUploadCommand,
commands.StoreLegacyPushCommand,
commands.StoreLegacyPromoteCommand,
commands.StoreListRevisionsCommand,
commands.StoreRevisionsCommand,
],
)
app.add_command_group(
"Store Snap Tracks",
[
commands.StoreListTracksCommand,
commands.StoreTracksCommand,
commands.StoreLegacySetDefaultTrackCommand,
],
)
app.add_command_group(
"Store Key Management",
[
commands.StoreLegacyCreateKeyCommand,
commands.StoreLegacyRegisterKeyCommand,
commands.StoreLegacySignBuildCommand,
commands.StoreLegacyListKeysCommand,
],
)
app.add_command_group(
"Store Validation Sets",
[
commands.StoreEditValidationSetsCommand,
commands.StoreLegacyListValidationSetsCommand,
commands.StoreLegacyValidateCommand,
commands.StoreLegacyGatedCommand,
],
)
app.add_command_group(
"Other",
list(craft_app_commands.get_other_command_group().commands)
+ [
commands.LintCommand,
commands.InitCommand,
],
)
for group in [cli.CORE24_LIFECYCLE_COMMAND_GROUP, *cli.COMMAND_GROUPS]:
app.add_command_group(group.name, group.commands)

return app

Expand Down
77 changes: 54 additions & 23 deletions snapcraft/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import contextlib
import os
import sys
from dataclasses import dataclass
from typing import Any, Dict

import craft_application.commands
Expand All @@ -35,36 +36,61 @@
from . import commands
from .legacy_cli import run_legacy


@dataclass
class CommandGroup:
"""Dataclass to hold a command group."""

name: str
commands: list


CORE22_LIFECYCLE_COMMAND_GROUP = CommandGroup(
"Lifecycle",
[
commands.core22.CleanCommand,
commands.core22.PullCommand,
commands.core22.BuildCommand,
commands.core22.StageCommand,
commands.core22.PrimeCommand,
commands.core22.PackCommand,
commands.core22.SnapCommand, # hidden (legacy compatibility)
commands.core22.TryCommand,
],
)

CORE24_LIFECYCLE_COMMAND_GROUP = CommandGroup(
"Lifecycle",
[
craft_application.commands.lifecycle.CleanCommand,
craft_application.commands.lifecycle.PullCommand,
craft_application.commands.lifecycle.BuildCommand,
craft_application.commands.lifecycle.StageCommand,
craft_application.commands.lifecycle.PrimeCommand,
commands.PackCommand,
commands.SnapCommand, # Hidden (legacy compatibility)
commands.RemoteBuildCommand,
commands.TryCommand,
],
)

COMMAND_GROUPS = [
craft_cli.CommandGroup(
"Lifecycle",
[
commands.core22.CleanCommand,
commands.core22.PullCommand,
commands.core22.BuildCommand,
commands.core22.StageCommand,
commands.core22.PrimeCommand,
commands.core22.PackCommand,
commands.core22.SnapCommand, # hidden (legacy compatibility)
commands.core22.TryCommand,
],
),
craft_cli.CommandGroup(
CommandGroup(
"Plugins",
[
commands.PluginsCommand,
commands.ListPluginsCommand,
],
),
craft_cli.CommandGroup(
CommandGroup(
"Extensions",
[
commands.ListExtensionsCommand,
commands.ExtensionsCommand, # hidden (alias to list-extensions)
commands.ExpandExtensionsCommand,
],
),
craft_cli.CommandGroup(
CommandGroup(
"Store Account",
[
commands.StoreLoginCommand,
Expand All @@ -73,7 +99,7 @@
commands.StoreWhoAmICommand,
],
),
craft_cli.CommandGroup(
CommandGroup(
"Store Snap Names",
[
commands.StoreRegisterCommand,
Expand All @@ -84,7 +110,7 @@
commands.StoreLegacyUploadMetadataCommand,
],
),
craft_cli.CommandGroup(
CommandGroup(
"Store Snap Release Management",
[
commands.StoreReleaseCommand,
Expand All @@ -97,15 +123,15 @@
commands.StoreRevisionsCommand, # hidden (alias to list-revisions)
],
),
craft_cli.CommandGroup(
CommandGroup(
"Store Snap Tracks",
[
commands.StoreListTracksCommand,
commands.StoreTracksCommand, # hidden (alias to list-tracks)
commands.StoreLegacySetDefaultTrackCommand,
],
),
craft_cli.CommandGroup(
CommandGroup(
"Store Key Management",
[
commands.StoreLegacyCreateKeyCommand,
Expand All @@ -114,7 +140,7 @@
commands.StoreLegacyListKeysCommand,
],
),
craft_cli.CommandGroup(
CommandGroup(
"Store Validation Sets",
[
commands.StoreEditValidationSetsCommand,
Expand All @@ -123,7 +149,7 @@
commands.StoreLegacyGatedCommand,
],
),
craft_cli.CommandGroup(
CommandGroup(
"Other",
[
*craft_application.commands.get_other_command_group().commands,
Expand Down Expand Up @@ -179,9 +205,14 @@ def get_verbosity() -> EmitterMode:

def get_dispatcher() -> craft_cli.Dispatcher:
"""Return an instance of Dispatcher."""
craft_cli_command_groups = [
craft_cli.CommandGroup(group.name, group.commands)
for group in COMMAND_GROUPS + [CORE22_LIFECYCLE_COMMAND_GROUP]
]

return craft_cli.Dispatcher(
"snapcraft",
COMMAND_GROUPS,
craft_cli_command_groups,
summary="Package, distribute, and update snaps for Linux and IoT",
extra_global_args=GLOBAL_ARGS,
default_command=commands.core22.PackCommand,
Expand Down
13 changes: 8 additions & 5 deletions tools/docs/gen_cli_docs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#!/usr/bin/env python3

import argparse
import os
import pathlib
import sys

from craft_cli.dispatcher import Dispatcher, _CustomArgumentParser
from craft_cli.dispatcher import _CustomArgumentParser

this_dir = pathlib.Path(os.path.split(__file__)[0])
sys.path.insert(0, str((this_dir / ".." / "..").absolute()))

from snapcraft import cli
from snapcraft import application


def command_page_header(cmd, options_str, required_str):
Expand Down Expand Up @@ -82,7 +81,11 @@ def main(docs_dir):
commands_ref_dir.mkdir()

# Create a dispatcher like Snapcraft does to get access to the same options.
dispatcher = cli.get_dispatcher()
app = application.create_app()
command_groups = app.command_groups

# Create a dispatcher like Snapcraft does to get access to the same options.
dispatcher = app._create_dispatcher()

help_builder = dispatcher._help_builder

Expand All @@ -93,7 +96,7 @@ def main(docs_dir):

toc = []

for group in cli.COMMAND_GROUPS:
for group in command_groups:
group_name = remove_spaces(group.name.lower()) + "-commands" + os.extsep + "rst"
group_path = commands_ref_dir / group_name
g = group_path.open("w")
Expand Down

0 comments on commit 90d6b68

Please sign in to comment.