Skip to content

Commit

Permalink
Add run subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
basicthinker committed Aug 28, 2023
1 parent 9ccca08 commit 20e0e7d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
2 changes: 2 additions & 0 deletions devchat/_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from .log import log
from .prompt import prompt
from .run import run
from .topic import topic

__all__ = [
'log',
'prompt',
'run',
'topic'
]
2 changes: 2 additions & 0 deletions devchat/_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from devchat.utils import get_logger
from devchat._cli import log
from devchat._cli import prompt
from devchat._cli import run
from devchat._cli import topic

logger = get_logger(__name__)
Expand All @@ -21,4 +22,5 @@ def main():

main.add_command(prompt)
main.add_command(log)
main.add_command(run)
main.add_command(topic)
58 changes: 58 additions & 0 deletions devchat/_cli/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import json
import os
import sys
import rich_click as click
from devchat._cli.utils import init_dir, handle_errors
from devchat.engine import Namespace, CommandParser, RecursivePrompter
from devchat.utils import get_logger

logger = get_logger(__name__)


@click.command(
help="The 'command' argument is the name of the command to run or get information about.")
@click.argument('command', required=False, default='')
@click.option('--list', 'list_flag', is_flag=True, default=False,
help='List all commands in JSON format.')
@click.option('--recursive', '-r', 'recursive_flag', is_flag=True, default=True,
help='List all commands recursively.')
def run(command: str, list_flag: bool, recursive_flag: bool):
"""
Operate workflow engine of DevChat.
"""
_, _, user_chat_dir = init_dir()
with handle_errors():
workflows_dir = os.path.join(user_chat_dir, 'workflows')
if not os.path.exists(workflows_dir):
os.makedirs(workflows_dir)
if not os.path.isdir(workflows_dir):
click.echo(f"Error: Failed to find workflows directory: {workflows_dir}", err=True)
sys.exit(1)

namespace = Namespace(workflows_dir)
commander = CommandParser(namespace)

if list_flag:
names = namespace.list_names(command, recursive_flag)
commands = []
for name in names:
cmd = commander.parse(name)
if not cmd:
logger.warning("Existing command directory failed to parse: %s", name)
continue
commands.append({
'name': name,
'description': cmd.description,
})
click.echo(json.dumps(commands, indent=2))
return

if command:
cmd = commander.parse(command)
if not cmd:
click.echo(f"Error: Failed to find command: {command}", err=True)
sys.exit(1)
if not cmd.steps:
prompter = RecursivePrompter(namespace)
click.echo(prompter.run(command))
return
9 changes: 0 additions & 9 deletions devchat/_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ def init_dir() -> Tuple[dict, str, str]:
except Exception:
config_data = default_config_data

try:
workflows_dir = os.path.join(user_chat_dir, 'workflows')
if not os.path.exists(workflows_dir):
os.makedirs(workflows_dir)
except Exception:
click.echo(f"Error: Failed to create {workflows_dir}", err=True)
if not os.path.isdir(workflows_dir):
sys.exit(1)

try:
setup_logger(os.path.join(repo_chat_dir, 'error.log'))
add_gitignore(repo_chat_dir, '*')
Expand Down
12 changes: 12 additions & 0 deletions devchat/engine/command_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ class CommandParser:
def __init__(self, namespace: Namespace):
self.namespace = namespace

def parse(self, name: str) -> Command:
"""
Parse a command configuration file to JSON.
:param name: The command name in the namespace.
:return: The JSON representation of the command.
"""
file_path = self.namespace.get_file(name, 'command.yml')
if not file_path:
return None
return parse_command(file_path)

def parse_json(self, name: str) -> str:
"""
Parse a command configuration file to JSON.
Expand Down

0 comments on commit 20e0e7d

Please sign in to comment.