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

     1  package executor
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/kubeshop/testkube/pkg/executor/env"
     8  	"github.com/kubeshop/testkube/pkg/executor/output"
     9  	"github.com/kubeshop/testkube/pkg/process"
    10  	"github.com/kubeshop/testkube/pkg/ui"
    11  )
    12  
    13  // Run runs executor process wrapped in json line output
    14  // wraps stdout lines into JSON chunks we want it to have common interface for agent
    15  // stdin <- testkube.Execution, stdout <- stream of json logs
    16  // LoggedExecuteInDir will put wrapped JSON output to stdout AND get RAW output into out var
    17  // json logs can be processed later on watch of pod logs
    18  func Run(dir string, command string, envMngr env.Interface, arguments ...string) (out []byte, err error) {
    19  	var obfuscatedArgs []byte
    20  	if envMngr != nil {
    21  		obfuscatedArgs = envMngr.ObfuscateSecrets([]byte(strings.Join(arguments, " ")))
    22  	}
    23  	output.PrintLogf("%s Executing in directory %s: \n $ %s %s", ui.IconMicroscope, dir, command, obfuscatedArgs)
    24  	out, err = process.LoggedExecuteInDir(dir, output.NewJSONWrapWriter(os.Stdout, envMngr), command, arguments...)
    25  	if err != nil {
    26  		output.PrintLogf("%s Execution failed: %s", ui.IconCross, err.Error())
    27  		return out, err
    28  	}
    29  	output.PrintLogf("%s Execution succeeded", ui.IconCheckMark)
    30  	return out, nil
    31  }
    32  
    33  // MergeCommandAndArgs prepares command and args for Run method
    34  func MergeCommandAndArgs(command, arguments []string) (string, []string) {
    35  	cmd := ""
    36  	if len(command) > 0 {
    37  		cmd = command[0]
    38  		arguments = append(command[1:], arguments...)
    39  	}
    40  
    41  	return cmd, arguments
    42  }