ssm-browse: added cd command

Also came up with an approach for dealing with commands that will probably work with contexts
This commit is contained in:
Leon Mika 2022-03-29 10:29:25 +11:00
parent 0b745a6dfa
commit f6f06eb22d
13 changed files with 142 additions and 37 deletions

View file

@ -9,15 +9,20 @@ import (
)
type CommandController struct {
commands map[string]Command
commandList *CommandContext
}
func NewCommandController(commands map[string]Command) *CommandController {
func NewCommandController() *CommandController {
return &CommandController{
commands: commands,
commandList: nil,
}
}
func (c *CommandController) AddCommands(ctx *CommandContext) {
ctx.parent = c.commandList
c.commandList = ctx
}
func (c *CommandController) Prompt() tea.Cmd {
return func() tea.Msg {
return events.PromptForInputMsg{
@ -36,10 +41,19 @@ func (c *CommandController) Execute(commandInput string) tea.Cmd {
}
tokens := shellwords.Split(input)
command, ok := c.commands[tokens[0]]
if !ok {
command := c.lookupCommand(tokens[0])
if command == nil {
return events.SetStatus("no such command: " + tokens[0])
}
return command(tokens)
return command(tokens[1:])
}
func (c *CommandController) lookupCommand(name string) Command {
for ctx := c.commandList; ctx != nil; ctx = ctx.parent {
if cmd, ok := ctx.Commands[name]; ok {
return cmd
}
}
return nil
}

View file

@ -9,3 +9,9 @@ func NoArgCommand(cmd tea.Cmd) Command {
return cmd
}
}
type CommandContext struct {
Commands map[string]Command
parent *CommandContext
}