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

proposal for cli-commands api #15630

Merged
merged 8 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions conan/api/conan_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys

from conan.api.subapi.cache import CacheAPI
from conan.api.subapi.cli import CliAPI
from conan.api.subapi.local import LocalAPI
from conan.api.subapi.lockfile import LockfileAPI
from conans import __version__ as client_version
Expand Down Expand Up @@ -39,6 +40,7 @@ def __init__(self, cache_folder=None):
migrator = ClientMigrator(self.cache_folder, Version(client_version))
migrator.migrate()

self.cli = CliAPI(self)
self.remotes = RemotesAPI(self)
# Search recipes by wildcard and packages filtering by configuracion
self.search = SearchAPI(self)
Expand Down
19 changes: 19 additions & 0 deletions conan/api/subapi/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from conans.errors import ConanException


class CliAPI:

def __init__(self, conan_api):
self.conan_api = conan_api
self.cli = None

def __getattr__(self, item):
commands = self.cli._commands
try:
command = commands[item]
except KeyError:
raise ConanException(f"Command {item} does not exist")

def wrapper(*args):
return command.run_cli(self.conan_api, *args)
return wrapper
2 changes: 2 additions & 0 deletions conan/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

_CONAN_INTERNAL_CUSTOM_COMMANDS_PATH = "_CONAN_INTERNAL_CUSTOM_COMMANDS_PATH"


class Cli:
"""A single command of the conan application, with all the first level commands. Manages the
parsing of parameters and delegates functionality to the conan python api. It can also show the
Expand All @@ -33,6 +34,7 @@ def __init__(self, conan_api):
assert isinstance(conan_api, ConanAPI), \
"Expected 'Conan' type, got '{}'".format(type(conan_api))
self._conan_api = conan_api
self._conan_api.cli.cli = self
self._groups = defaultdict(list)
self._commands = {}

Expand Down
8 changes: 8 additions & 0 deletions conan/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ def add_subcommand(self, subcommand):
subcommand.set_name(self.name)
self._subcommands[subcommand.name] = subcommand

def run_cli(self, conan_api, *args):
parser = ConanArgumentParser(conan_api, description=self._doc,
prog="conan {}".format(self._name),
formatter_class=SmartFormatter)
self._init_log_levels(parser)
self._init_formatters(parser)
return self._method(conan_api, parser, *args)

def run(self, conan_api, *args):
parser = ConanArgumentParser(conan_api, description=self._doc,
prog="conan {}".format(self._name),
Expand Down
23 changes: 23 additions & 0 deletions conans/test/integration/command_v2/custom_commands_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import textwrap

Expand Down Expand Up @@ -286,3 +287,25 @@ def hello(conan_api, parser, *args, **kwargs):
# Without the variable it only loads the default custom commands location
client.run("hello")
assert "Hello world!" in client.out

def test_command_reuse_interface(self):
mycommand = textwrap.dedent("""
import json
from conan.cli.command import conan_command
from conan.api.output import cli_out_write

@conan_command(group="custom commands")
def mycommand(conan_api, parser, *args, **kwargs):
\"""
mycommand help
\"""
result = conan_api.cli.list("*", "-c")
cli_out_write(json.dumps(result["results"], indent=2))
""")

c = TestClient()
command_file_path = os.path.join(c.cache_folder, 'extensions',
'commands', 'cmd_mycommand.py')
c.save({f"{command_file_path}": mycommand})
c.run("mycommand", redirect_stdout="file.json")
assert json.loads(c.load("file.json")) == {"Local Cache": {}}