github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/test/e2e/runner/exec.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	osexec "os/exec"
     7  	"path/filepath"
     8  )
     9  
    10  // execute executes a shell command.
    11  func exec(args ...string) error {
    12  	_, err := execOutput(args...)
    13  	return err
    14  }
    15  
    16  func execOutput(args ...string) ([]byte, error) {
    17  	//nolint:gosec // G204: Subprocess launched with a potential tainted input or cmd arguments
    18  	cmd := osexec.Command(args[0], args[1:]...)
    19  	out, err := cmd.CombinedOutput()
    20  	switch err := err.(type) {
    21  	case nil:
    22  		return out, nil
    23  	case *osexec.ExitError:
    24  		return nil, fmt.Errorf("failed to run %q:\n%v", args, string(out))
    25  	default:
    26  		return nil, err
    27  	}
    28  }
    29  
    30  // execVerbose executes a shell command while displaying its output.
    31  func execVerbose(args ...string) error {
    32  	//nolint:gosec // G204: Subprocess launched with a potential tainted input or cmd arguments
    33  	cmd := osexec.Command(args[0], args[1:]...)
    34  	cmd.Stdout = os.Stdout
    35  	cmd.Stderr = os.Stderr
    36  	return cmd.Run()
    37  }
    38  
    39  // execCompose runs a Docker Compose command for a testnet.
    40  func execCompose(dir string, args ...string) error {
    41  	return exec(append(
    42  		[]string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
    43  		args...)...)
    44  }
    45  
    46  func execComposeOutput(dir string, args ...string) ([]byte, error) {
    47  	return execOutput(append(
    48  		[]string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
    49  		args...)...)
    50  }
    51  
    52  // execComposeVerbose runs a Docker Compose command for a testnet and displays its output.
    53  func execComposeVerbose(dir string, args ...string) error {
    54  	return execVerbose(append(
    55  		[]string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
    56  		args...)...)
    57  }
    58  
    59  // execDocker runs a Docker command.
    60  func execDocker(args ...string) error {
    61  	return exec(append([]string{"docker"}, args...)...)
    62  }