|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from typing import List |
| 3 | + |
| 4 | +from codegate import __version__ |
| 5 | +from codegate.workspaces.crud import WorkspaceCrud |
| 6 | + |
| 7 | + |
| 8 | +class CodegateCommand(ABC): |
| 9 | + @abstractmethod |
| 10 | + async def run(self, args: List[str]) -> str: |
| 11 | + pass |
| 12 | + |
| 13 | + @property |
| 14 | + @abstractmethod |
| 15 | + def help(self) -> str: |
| 16 | + pass |
| 17 | + |
| 18 | + async def exec(self, args: List[str]) -> str: |
| 19 | + if args and args[0] == "-h": |
| 20 | + return self.help |
| 21 | + return await self.run(args) |
| 22 | + |
| 23 | + |
| 24 | +class Version(CodegateCommand): |
| 25 | + async def run(self, args: List[str]) -> str: |
| 26 | + return f"CodeGate version: {__version__}" |
| 27 | + |
| 28 | + @property |
| 29 | + def help(self) -> str: |
| 30 | + return ( |
| 31 | + "### CodeGate Version\n\n" |
| 32 | + "Prints the version of CodeGate.\n\n" |
| 33 | + "**Usage**: `codegate version`\n\n" |
| 34 | + "*args*: None" |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +class Workspace(CodegateCommand): |
| 39 | + |
| 40 | + def __init__(self): |
| 41 | + self.workspace_crud = WorkspaceCrud() |
| 42 | + self.commands = { |
| 43 | + "list": self._list_workspaces, |
| 44 | + "add": self._add_workspace, |
| 45 | + "activate": self._activate_workspace, |
| 46 | + } |
| 47 | + |
| 48 | + async def _list_workspaces(self, *args: List[str]) -> str: |
| 49 | + """ |
| 50 | + List all workspaces |
| 51 | + """ |
| 52 | + workspaces = await self.workspace_crud.get_workspaces() |
| 53 | + respond_str = "" |
| 54 | + for workspace in workspaces: |
| 55 | + respond_str += f"- {workspace.name}" |
| 56 | + if workspace.active_workspace_id: |
| 57 | + respond_str += " **(active)**" |
| 58 | + respond_str += "\n" |
| 59 | + return respond_str |
| 60 | + |
| 61 | + async def _add_workspace(self, args: List[str]) -> str: |
| 62 | + """ |
| 63 | + Add a workspace |
| 64 | + """ |
| 65 | + if args is None or len(args) == 0: |
| 66 | + return "Please provide a name. Use `codegate-workspace add your_workspace_name`" |
| 67 | + |
| 68 | + new_workspace_name = args[0] |
| 69 | + if not new_workspace_name: |
| 70 | + return "Please provide a name. Use `codegate-workspace add your_workspace_name`" |
| 71 | + |
| 72 | + workspace_created = await self.workspace_crud.add_workspace(new_workspace_name) |
| 73 | + if not workspace_created: |
| 74 | + return ( |
| 75 | + "Something went wrong. Workspace could not be added.\n" |
| 76 | + "1. Check if the name is alphanumeric and only contains dashes, and underscores.\n" |
| 77 | + "2. Check if the workspace already exists." |
| 78 | + ) |
| 79 | + return f"Workspace **{new_workspace_name}** has been added" |
| 80 | + |
| 81 | + async def _activate_workspace(self, args: List[str]) -> str: |
| 82 | + """ |
| 83 | + Activate a workspace |
| 84 | + """ |
| 85 | + if args is None or len(args) == 0: |
| 86 | + return "Please provide a name. Use `codegate-workspace activate workspace_name`" |
| 87 | + |
| 88 | + workspace_name = args[0] |
| 89 | + if not workspace_name: |
| 90 | + return "Please provide a name. Use `codegate-workspace activate workspace_name`" |
| 91 | + |
| 92 | + was_activated = await self.workspace_crud.activate_workspace(workspace_name) |
| 93 | + if not was_activated: |
| 94 | + return ( |
| 95 | + f"Workspace **{workspace_name}** does not exist or was already active. " |
| 96 | + f"Use `codegate-workspace add {workspace_name}` to add it" |
| 97 | + ) |
| 98 | + return f"Workspace **{workspace_name}** has been activated" |
| 99 | + |
| 100 | + async def run(self, args: List[str]) -> str: |
| 101 | + if not args: |
| 102 | + return "Please provide a command. Use `codegate workspace -h` to see available commands" |
| 103 | + command = args[0] |
| 104 | + command_to_execute = self.commands.get(command) |
| 105 | + if command_to_execute is not None: |
| 106 | + return await command_to_execute(args[1:]) |
| 107 | + else: |
| 108 | + return "Command not found. Use `codegate workspace -h` to see available commands" |
| 109 | + |
| 110 | + @property |
| 111 | + def help(self) -> str: |
| 112 | + return ( |
| 113 | + "### CodeGate Workspace\n\n" |
| 114 | + "Manage workspaces.\n\n" |
| 115 | + "**Usage**: `codegate workspace <command> [args]`\n\n" |
| 116 | + "Available commands:\n\n" |
| 117 | + "- `list`: List all workspaces\n\n" |
| 118 | + " - *args*: None\n\n" |
| 119 | + "- `add`: Add a workspace\n\n" |
| 120 | + " - *args*:\n\n" |
| 121 | + " - `workspace_name`\n\n" |
| 122 | + "- `activate`: Activate a workspace\n\n" |
| 123 | + " - *args*:\n\n" |
| 124 | + " - `workspace_name`" |
| 125 | + ) |
0 commit comments