Skip to content

Commit

Permalink
Adds documentation for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Nov 13, 2023
1 parent 4abb781 commit b0969bf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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.

Check failure on line 87 in pkg/cli/commands.go

View workflow job for this annotation

GitHub Actions / tests

`succesful` is a misspelling of `successful` (misspell)
func (c *CommandEdit) Execute(exCtx *ExecutionContext) (Executer, error) {
color.New(color.FgGreen).Fprint(exCtx.cli.output, "->\n")

Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand Down Expand Up @@ -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, ":")

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b0969bf

Please sign in to comment.