github.com/kubeshop/testkube@v1.17.23/pkg/executor/output/output.go (about)

     1  package output
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"time"
     8  
     9  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    10  )
    11  
    12  const (
    13  	TypeLogEvent     = "event"
    14  	TypeLogLine      = "line"
    15  	TypeError        = "error"
    16  	TypeParsingError = "parsing-error"
    17  	TypeResult       = "result"
    18  	TypeUnknown      = "unknown"
    19  )
    20  
    21  // NewOutputEvent returns new Output struct of type event
    22  func NewOutputEvent(message string) Output {
    23  	return Output{
    24  		Type_:   TypeLogEvent,
    25  		Content: message,
    26  		Time:    time.Now(),
    27  	}
    28  }
    29  
    30  // NewOutputLine returns new Output struct of type line
    31  func NewOutputLine(content []byte) Output {
    32  	return Output{
    33  		Type_:   TypeLogLine,
    34  		Content: string(content),
    35  		Time:    time.Now(),
    36  	}
    37  }
    38  
    39  // NewOutputError returns new Output struct of type error
    40  func NewOutputError(err error) Output {
    41  	return Output{
    42  		Type_:   TypeError,
    43  		Content: string(err.Error()),
    44  		Time:    time.Now(),
    45  	}
    46  }
    47  
    48  // NewOutputResult returns new Output struct of type result - should be last line in stream as it'll stop listening
    49  func NewOutputResult(result testkube.ExecutionResult) Output {
    50  	return Output{
    51  		Type_:  TypeResult,
    52  		Result: &result,
    53  		Time:   time.Now(),
    54  	}
    55  }
    56  
    57  // Output generic json based output data structure
    58  type Output testkube.ExecutorOutput
    59  
    60  // String
    61  func (out Output) String() string {
    62  	switch out.Type_ {
    63  	case TypeError, TypeParsingError, TypeLogLine, TypeLogEvent:
    64  		return out.Content
    65  	case TypeResult:
    66  		b, _ := json.Marshal(out.Result)
    67  		return string(b)
    68  	}
    69  
    70  	return ""
    71  }
    72  
    73  // PrintError - prints error as output json
    74  func PrintError(w io.Writer, err error) {
    75  	out, _ := json.Marshal(NewOutputError(err))
    76  	fmt.Fprintf(w, "%s\n", out)
    77  }
    78  
    79  // PrintLog - prints log line as output json
    80  func PrintLog(message string) {
    81  	out, _ := json.Marshal(NewOutputLine([]byte(message)))
    82  	fmt.Printf("%s\n", out)
    83  }
    84  
    85  // PrintLogf - prints log line as output json and supports sprintf formatting
    86  func PrintLogf(format string, args ...any) {
    87  	message := fmt.Sprintf(format, args...)
    88  	out, _ := json.Marshal(NewOutputLine([]byte(message)))
    89  	fmt.Printf("%s\n", out)
    90  }
    91  
    92  // PrintResult - prints result as output json
    93  func PrintResult(result testkube.ExecutionResult) {
    94  	out, _ := json.Marshal(NewOutputResult(result))
    95  	fmt.Printf("%s\n", out)
    96  }
    97  
    98  // PrintEvent - prints event as output json
    99  func PrintEvent(message string, obj ...interface{}) {
   100  	out, _ := json.Marshal(NewOutputEvent(fmt.Sprintf("%s %v", message, obj)))
   101  	fmt.Printf("%s\n", out)
   102  }