From 90d6b68374ed7b3d5de26a0d0181beb9d5b77498 Mon Sep 17 00:00:00 2001 From: Callahan Kovacs Date: Fri, 31 May 2024 10:18:23 -0500 Subject: [PATCH] refactor: de-duplicate core22 and core24 command groups Signed-off-by: Callahan Kovacs --- docs/reference/commands.rst | 5 ++ snapcraft/application.py | 100 ++---------------------------------- snapcraft/cli.py | 77 ++++++++++++++++++--------- tools/docs/gen_cli_docs.py | 13 +++-- 4 files changed, 70 insertions(+), 125 deletions(-) diff --git a/docs/reference/commands.rst b/docs/reference/commands.rst index ce3887f24c..2faa5e9106 100644 --- a/docs/reference/commands.rst +++ b/docs/reference/commands.rst @@ -13,6 +13,11 @@ provided, the command applies to all parts. .. include:: commands/lifecycle-commands.rst +Plugins +---------- + +.. include:: commands/plugins-commands.rst + Extensions ---------- diff --git a/snapcraft/application.py b/snapcraft/application.py index 9db09e6075..85d3ef66d7 100644 --- a/snapcraft/application.py +++ b/snapcraft/application.py @@ -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 @@ -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 diff --git a/snapcraft/cli.py b/snapcraft/cli.py index 6640537696..5037f691a5 100644 --- a/snapcraft/cli.py +++ b/snapcraft/cli.py @@ -20,6 +20,7 @@ import contextlib import os import sys +from dataclasses import dataclass from typing import Any, Dict import craft_application.commands @@ -35,28 +36,53 @@ 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, @@ -64,7 +90,7 @@ commands.ExpandExtensionsCommand, ], ), - craft_cli.CommandGroup( + CommandGroup( "Store Account", [ commands.StoreLoginCommand, @@ -73,7 +99,7 @@ commands.StoreWhoAmICommand, ], ), - craft_cli.CommandGroup( + CommandGroup( "Store Snap Names", [ commands.StoreRegisterCommand, @@ -84,7 +110,7 @@ commands.StoreLegacyUploadMetadataCommand, ], ), - craft_cli.CommandGroup( + CommandGroup( "Store Snap Release Management", [ commands.StoreReleaseCommand, @@ -97,7 +123,7 @@ commands.StoreRevisionsCommand, # hidden (alias to list-revisions) ], ), - craft_cli.CommandGroup( + CommandGroup( "Store Snap Tracks", [ commands.StoreListTracksCommand, @@ -105,7 +131,7 @@ commands.StoreLegacySetDefaultTrackCommand, ], ), - craft_cli.CommandGroup( + CommandGroup( "Store Key Management", [ commands.StoreLegacyCreateKeyCommand, @@ -114,7 +140,7 @@ commands.StoreLegacyListKeysCommand, ], ), - craft_cli.CommandGroup( + CommandGroup( "Store Validation Sets", [ commands.StoreEditValidationSetsCommand, @@ -123,7 +149,7 @@ commands.StoreLegacyGatedCommand, ], ), - craft_cli.CommandGroup( + CommandGroup( "Other", [ *craft_application.commands.get_other_command_group().commands, @@ -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, diff --git a/tools/docs/gen_cli_docs.py b/tools/docs/gen_cli_docs.py index 4e813621b3..84a7e9c5a0 100755 --- a/tools/docs/gen_cli_docs.py +++ b/tools/docs/gen_cli_docs.py @@ -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): @@ -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 @@ -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")