chore: initial commit
Co-authored-by: Lifei Zhou <lifei@squareup.com> Co-authored-by: Mic Neale <micn@tbd.email> Co-authored-by: Lily Delalande <ldelalande@squareup.com> Co-authored-by: Bradley Axen <baxen@squareup.com> Co-authored-by: Andy Lane <alane@squareup.com> Co-authored-by: Elena Zherdeva <ezherdeva@squareup.com> Co-authored-by: Zaki Ali <zaki@squareup.com> Co-authored-by: Salman Mohammed <smohammed@squareup.com>
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
from functools import cache
|
||||
from typing import Dict
|
||||
|
||||
from goose.command.base import Command
|
||||
from goose.utils import load_plugins
|
||||
|
||||
|
||||
@cache
|
||||
def get_command(name: str) -> type[Command]:
|
||||
return load_plugins(group="goose.command")[name]
|
||||
|
||||
|
||||
@cache
|
||||
def get_commands() -> Dict[str, type[Command]]:
|
||||
return load_plugins(group="goose.command")
|
||||
@@ -0,0 +1,16 @@
|
||||
from abc import ABC
|
||||
from typing import List, Optional
|
||||
|
||||
from prompt_toolkit.completion import Completion
|
||||
|
||||
|
||||
class Command(ABC):
|
||||
"""A command that can be executed by the CLI."""
|
||||
|
||||
def get_completions(self, query: str) -> List[Completion]:
|
||||
"""Get completions for the command."""
|
||||
return []
|
||||
|
||||
def execute(self, query: str) -> Optional[str]:
|
||||
"""Execute's the command and replaces it with the output."""
|
||||
return ""
|
||||
@@ -0,0 +1,61 @@
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
from prompt_toolkit.completion import Completion
|
||||
|
||||
from goose.command.base import Command
|
||||
|
||||
|
||||
class FileCommand(Command):
|
||||
def get_completions(self, query: str) -> List[Completion]:
|
||||
if query.startswith("/"):
|
||||
directory = os.path.dirname(query)
|
||||
search_term = os.path.basename(query)
|
||||
else:
|
||||
directory = os.path.join(os.getcwd(), os.path.dirname(query))
|
||||
search_term = os.path.basename(query)
|
||||
|
||||
# if query is a file, don't show completions
|
||||
if os.path.isfile(directory):
|
||||
return []
|
||||
|
||||
# Get the list of files in the directory
|
||||
options = []
|
||||
try:
|
||||
for file_name in os.listdir(directory):
|
||||
if file_name.startswith(search_term):
|
||||
full_path = os.path.join(directory, file_name)
|
||||
if os.path.isdir(full_path):
|
||||
options.append(
|
||||
dict(
|
||||
display_text=" " + file_name,
|
||||
insert_text=file_name,
|
||||
is_dir=True,
|
||||
)
|
||||
)
|
||||
else:
|
||||
options.append(
|
||||
dict(
|
||||
display_text=" " + file_name,
|
||||
insert_text=file_name + " ",
|
||||
is_dir=False,
|
||||
)
|
||||
)
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
|
||||
completions = []
|
||||
options.sort(key=lambda x: (not x["is_dir"], x["insert_text"]), reverse=False)
|
||||
for option in options:
|
||||
completions.append(
|
||||
Completion(
|
||||
option["insert_text"],
|
||||
start_position=-len(search_term),
|
||||
display=option["display_text"],
|
||||
)
|
||||
)
|
||||
return completions
|
||||
|
||||
def execute(self, query: str) -> str | None:
|
||||
# GOOSE-TODO: return the query
|
||||
pass
|
||||
Reference in New Issue
Block a user