github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/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  	cmd := osexec.Command(args[0], args[1:]...) //nolint:gosec
    13  	out, err := cmd.CombinedOutput()
    14  	switch err := err.(type) {
    15  	case nil:
    16  		return nil
    17  	case *osexec.ExitError:
    18  		return fmt.Errorf("failed to run %q:\n%v", args, string(out))
    19  	default:
    20  		return err
    21  	}
    22  }
    23  
    24  // execVerbose executes a shell command while displaying its output.
    25  func execVerbose(args ...string) error {
    26  	cmd := osexec.Command(args[0], args[1:]...) //nolint:gosec
    27  	cmd.Stdout = os.Stdout
    28  	cmd.Stderr = os.Stderr
    29  	return cmd.Run()
    30  }
    31  
    32  // execCompose runs a Docker Compose command for a testnet.
    33  func execCompose(dir string, args ...string) error {
    34  	return exec(append(
    35  		[]string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
    36  		args...)...)
    37  }
    38  
    39  // execComposeVerbose runs a Docker Compose command for a testnet and displays its output.
    40  func execComposeVerbose(dir string, args ...string) error {
    41  	return execVerbose(append(
    42  		[]string{"docker-compose", "-f", filepath.Join(dir, "docker-compose.yml")},
    43  		args...)...)
    44  }
    45  
    46  // execDocker runs a Docker command.
    47  func execDocker(args ...string) error {
    48  	return exec(append([]string{"docker"}, args...)...)
    49  }