github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/executils/executils.go (about)

     1  package executils
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  )
     7  
     8  //go:generate mockgen -destination ../../mock/mock_executils_runner.go -package mock -source=./executils.go
     9  
    10  // Runner is mainly for executing shell command
    11  type Runner interface {
    12  	Run(string, ...string) error
    13  	Start(string, ...string) (*exec.Cmd, error)
    14  	Output(string, ...string) ([]byte, error)
    15  }
    16  
    17  // CmdRunner implements Runner interface
    18  type CmdRunner struct{}
    19  
    20  func (r CmdRunner) Output(command string, args ...string) ([]byte, error) {
    21  	return exec.Command(command, args...).Output()
    22  }
    23  
    24  // Run executes commands
    25  func (r CmdRunner) Run(command string, args ...string) error {
    26  	cmd := exec.Command(command, args...)
    27  	cmd.Stdout = os.Stdout
    28  	cmd.Stderr = os.Stderr
    29  	return cmd.Run()
    30  }
    31  
    32  // Start starts the specified command but does not wait for it to complete.
    33  func (r CmdRunner) Start(command string, args ...string) (*exec.Cmd, error) {
    34  	cmd := exec.Command(command, args...)
    35  	cmd.Stdout = os.Stdout
    36  	cmd.Stderr = os.Stderr
    37  	return cmd, cmd.Start()
    38  }