github.com/badrootd/nibiru-cometbft@v0.37.5-0.20240307173500-2a75559eee9b/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  	cmd := osexec.Command(args[0], args[1:]...) //nolint:gosec
    18  	out, err := cmd.CombinedOutput()
    19  	switch err := err.(type) {
    20  	case nil:
    21  		return out, nil
    22  	case *osexec.ExitError:
    23  		return nil, fmt.Errorf("failed to run %q:\n%v", args, string(out))
    24  	default:
    25  		return nil, err
    26  	}
    27  }
    28  
    29  // execVerbose executes a shell command while displaying its output.
    30  func execVerbose(args ...string) error {
    31  	cmd := osexec.Command(args[0], args[1:]...) //nolint:gosec
    32  	cmd.Stdout = os.Stdout
    33  	cmd.Stderr = os.Stderr
    34  	return cmd.Run()
    35  }
    36  
    37  // execCompose runs a Docker Compose command for a testnet.
    38  func execCompose(dir string, args ...string) error {
    39  	return exec(append(
    40  		[]string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
    41  		args...)...)
    42  }
    43  
    44  func execComposeOutput(dir string, args ...string) ([]byte, error) {
    45  	return execOutput(append(
    46  		[]string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
    47  		args...)...)
    48  }
    49  
    50  // execComposeVerbose runs a Docker Compose command for a testnet and displays its output.
    51  func execComposeVerbose(dir string, args ...string) error {
    52  	return execVerbose(append(
    53  		[]string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
    54  		args...)...)
    55  }
    56  
    57  // execDocker runs a Docker command.
    58  func execDocker(args ...string) error {
    59  	return exec(append([]string{"docker"}, args...)...)
    60  }