github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/pkg/exec/exec.go (about)

     1  package exec
     2  
     3  import (
     4  	"github.com/drud/ddev/pkg/globalconfig"
     5  	"os"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	"github.com/drud/ddev/pkg/output"
    10  	log "github.com/sirupsen/logrus"
    11  )
    12  
    13  // RunCommand runs a command on the host system.
    14  // returns the stdout of the command and an err
    15  func RunCommand(command string, args []string) (string, error) {
    16  	out, err := exec.Command(
    17  		command, args...,
    18  	).CombinedOutput()
    19  
    20  	output.UserOut.WithFields(log.Fields{
    21  		"Result": string(out),
    22  	}).Debug("Command ")
    23  
    24  	return string(out), err
    25  }
    26  
    27  // RunCommandPipe runs a command on the host system
    28  // Returns combined output as string, and error
    29  func RunCommandPipe(command string, args []string) (string, error) {
    30  	output.UserOut.WithFields(log.Fields{
    31  		"Command": command + " " + strings.Join(args[:], " "),
    32  	}).Info("Running ")
    33  
    34  	cmd := exec.Command(command, args...)
    35  	stdoutStderr, err := cmd.CombinedOutput()
    36  	return string(stdoutStderr), err
    37  }
    38  
    39  // RunInteractiveCommand runs a command on the host system interactively, with stdin/stdout/stderr connected
    40  // Returns error
    41  func RunInteractiveCommand(command string, args []string) error {
    42  	cmd := exec.Command(command, args...)
    43  	cmd.Stdin = os.Stdin
    44  	cmd.Stdout = os.Stdout
    45  	cmd.Stderr = os.Stderr
    46  	err := cmd.Start()
    47  	if err != nil {
    48  		return err
    49  	}
    50  	err = cmd.Wait()
    51  	return err
    52  }
    53  
    54  // RunHostCommand executes a command on the host and returns the
    55  // combined stdout/stderr results and error
    56  func RunHostCommand(command string, args ...string) (string, error) {
    57  	if globalconfig.DdevVerbose {
    58  		output.UserOut.Printf("RunHostCommand: " + command + " " + strings.Join(args, " "))
    59  	}
    60  	c := exec.Command(command, args...)
    61  	c.Stdin = os.Stdin
    62  	o, err := c.CombinedOutput()
    63  	if globalconfig.DdevVerbose {
    64  		output.UserOut.Printf("RunHostCommand returned. output=%v err=%v", string(o), err)
    65  	}
    66  
    67  	return string(o), err
    68  }
    69  
    70  // RunHostCommandSeparateStreams executes a command on the host and returns the
    71  // stdout and error
    72  func RunHostCommandSeparateStreams(command string, args ...string) (string, error) {
    73  	if globalconfig.DdevVerbose {
    74  		output.UserOut.Printf("RunHostCommandSeparateStreams: " + command + " " + strings.Join(args, " "))
    75  	}
    76  	c := exec.Command(command, args...)
    77  	c.Stdin = os.Stdin
    78  	o, err := c.Output()
    79  	if globalconfig.DdevVerbose {
    80  		output.UserOut.Printf("RunHostCommandSeparateStreams returned. stdout=%v, err=%v", string(o), err)
    81  	}
    82  
    83  	return string(o), err
    84  }