diff --git a/pkg/cli/commands.go b/pkg/cli/commands.go index 3beea4a..a109284 100644 --- a/pkg/cli/commands.go +++ b/pkg/cli/commands.go @@ -26,6 +26,10 @@ type Executer interface { Execute(*ExecutionContext) (Executer, error) } +// CommandFactory returns an Executer and an error. It takes a string and a Macro pointer as input. +// The string is split into parts and the first part is used to determine which command to execute. +// Depending on the command, different arguments are passed to the corresponding constructor. +// If the command is not recognized, an error is returned. func CommandFactory(raw string, macro *Macro) (Executer, error) { if raw == "" { return nil, fmt.Errorf("empty command") @@ -80,6 +84,7 @@ func NewCommandEdit(content string) *CommandEdit { return &CommandEdit{content} } +// Execute executes the edit command and returns a Send command id editing was succesful or an error in other case. func (c *CommandEdit) Execute(exCtx *ExecutionContext) (Executer, error) { color.New(color.FgGreen).Fprint(exCtx.cli.output, "->\n") @@ -103,6 +108,8 @@ func NewCommandSend(request string) *CommandSend { return &CommandSend{request} } +// Execute sends the request using the WebSocket connection and returns a CommandPrintMsg to print the response message. +// It implements the Execute method of the Executer interface. func (c *CommandSend) Execute(exCtx *ExecutionContext) (Executer, error) { msg, err := exCtx.cli.wsConn.Send(c.request) if err != nil { @@ -120,6 +127,9 @@ func NewCommandPrintMsg(msg ws.Message) *CommandPrintMsg { return &CommandPrintMsg{msg} } +// Execute executes the CommandPrintMsg command and returns nil and error. +// It formats the message and prints it to the output file. +// If an output file is provided, it writes the formatted message to the file. func (c *CommandPrintMsg) Execute(exCtx *ExecutionContext) (Executer, error) { msg := c.msg output, err := exCtx.cli.formater.FormatMessage(msg) @@ -157,6 +167,8 @@ func NewCommandExit() *CommandExit { return &CommandExit{} } +// Execute method implements the Execute method of the Executer interface. +// It returns an error indicating that the program was interrupted. func (c *CommandExit) Execute(_ *ExecutionContext) (Executer, error) { return nil, fmt.Errorf("interrupted") } @@ -169,6 +181,10 @@ func NewCommandWaitForResp(timeout time.Duration) *CommandWaitForResp { return &CommandWaitForResp{timeout} } +// Execute executes the CommandWaitForResp command and waits for a response from the WebSocket connection. +// If a timeout is set, it will return an error if no response is received within the specified time. +// If a response is received, it will return a new CommandPrintMsg command with the received message. +// If the WebSocket connection is closed, it will return an error. func (c *CommandWaitForResp) Execute(exCtx *ExecutionContext) (Executer, error) { if c.timeout.Seconds() == 0 { msg, ok := <-exCtx.cli.wsConn.Messages @@ -197,6 +213,8 @@ func NewCommandCmdEdit() *CommandCmdEdit { return &CommandCmdEdit{} } +// Execute executes the CommandCmdEdit and returns an Executer and an error. +// It prompts the user to edit a command and returns the corresponding Command object. func (c *CommandCmdEdit) Execute(exCtx *ExecutionContext) (Executer, error) { fmt.Fprint(exCtx.cli.output, ":") @@ -227,6 +245,8 @@ func NewCommandSequence(subCommands []Executer) *CommandSequence { return &CommandSequence{subCommands} } +// Execute executes the command sequence by iterating over all sub-commands and executing them recursively. +// It takes an ExecutionContext as input and returns an Executer and an error. func (c *CommandSequence) Execute(exCtx *ExecutionContext) (Executer, error) { for _, cmd := range c.subCommands { for cmd != nil {