github.com/df-mc/dragonfly@v0.9.13/server/cmd/output.go (about)

     1  package cmd
     2  
     3  import "fmt"
     4  
     5  // Output holds the output of a command execution. It holds success messages and error messages, which the
     6  // source of a command execution gets sent.
     7  type Output struct {
     8  	errors   []error
     9  	messages []string
    10  }
    11  
    12  // Errorf formats an error message and adds it to the command output.
    13  func (o *Output) Errorf(format string, a ...any) {
    14  	o.errors = append(o.errors, fmt.Errorf(format, a...))
    15  }
    16  
    17  // Error formats an error message and adds it to the command output.
    18  func (o *Output) Error(a ...any) {
    19  	o.errors = append(o.errors, fmt.Errorf(fmt.Sprint(a...)))
    20  }
    21  
    22  // Printf formats a (success) message and adds it to the command output.
    23  func (o *Output) Printf(format string, a ...any) {
    24  	o.messages = append(o.messages, fmt.Sprintf(format, a...))
    25  }
    26  
    27  // Print formats a (success) message and adds it to the command output.
    28  func (o *Output) Print(a ...any) {
    29  	o.messages = append(o.messages, fmt.Sprint(a...))
    30  }
    31  
    32  // Errors returns a list of all errors added to the command output. Usually only one error message is set:
    33  // After one error message, execution of a command typically terminates.
    34  func (o *Output) Errors() []error {
    35  	return o.errors
    36  }
    37  
    38  // ErrorCount returns the count of errors that the command output has.
    39  func (o *Output) ErrorCount() int {
    40  	return len(o.errors)
    41  }
    42  
    43  // Messages returns a list of all messages added to the command output. The amount of messages present depends
    44  // on the command called.
    45  func (o *Output) Messages() []string {
    46  	return o.messages
    47  }
    48  
    49  // MessageCount returns the count of (success) messages that the command output has.
    50  func (o *Output) MessageCount() int {
    51  	return len(o.messages)
    52  }