github.com/secoba/wails/v2@v2.6.4/internal/shell/shell.go (about)

     1  package shell
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"os/exec"
     7  )
     8  
     9  type Command struct {
    10  	command    string
    11  	args       []string
    12  	env        []string
    13  	dir        string
    14  	stdo, stde bytes.Buffer
    15  }
    16  
    17  func NewCommand(command string) *Command {
    18  	return &Command{
    19  		command: command,
    20  		env:     os.Environ(),
    21  	}
    22  }
    23  
    24  func (c *Command) Dir(dir string) {
    25  	c.dir = dir
    26  }
    27  
    28  func (c *Command) Env(name string, value string) {
    29  	c.env = append(c.env, name+"="+value)
    30  }
    31  
    32  func (c *Command) Run() error {
    33  	cmd := exec.Command(c.command, c.args...)
    34  	if c.dir != "" {
    35  		cmd.Dir = c.dir
    36  	}
    37  	cmd.Stdout = &c.stdo
    38  	cmd.Stderr = &c.stde
    39  	return cmd.Run()
    40  }
    41  
    42  func (c *Command) Stdout() string {
    43  	return c.stdo.String()
    44  }
    45  
    46  func (c *Command) Stderr() string {
    47  	return c.stde.String()
    48  }
    49  
    50  func (c *Command) AddArgs(args []string) {
    51  	c.args = append(c.args, args...)
    52  }
    53  
    54  // CreateCommand returns a *Cmd struct that when run, will run the given command + args in the given directory
    55  func CreateCommand(directory string, command string, args ...string) *exec.Cmd {
    56  	cmd := exec.Command(command, args...)
    57  	cmd.Dir = directory
    58  	return cmd
    59  }
    60  
    61  // RunCommand will run the given command + args in the given directory
    62  // Will return stdout, stderr and error
    63  func RunCommand(directory string, command string, args ...string) (string, string, error) {
    64  	return RunCommandWithEnv(nil, directory, command, args...)
    65  }
    66  
    67  // RunCommandWithEnv will run the given command + args in the given directory and using the specified env.
    68  //
    69  // Env specifies the environment of the process. Each entry is of the form "key=value".
    70  // If Env is nil, the new process uses the current process's environment.
    71  //
    72  // Will return stdout, stderr and error
    73  func RunCommandWithEnv(env []string, directory string, command string, args ...string) (string, string, error) {
    74  	cmd := CreateCommand(directory, command, args...)
    75  	cmd.Env = env
    76  
    77  	var stdo, stde bytes.Buffer
    78  	cmd.Stdout = &stdo
    79  	cmd.Stderr = &stde
    80  	err := cmd.Run()
    81  	return stdo.String(), stde.String(), err
    82  }
    83  
    84  // RunCommandVerbose will run the given command + args in the given directory
    85  // Will return an error if one occurs
    86  func RunCommandVerbose(directory string, command string, args ...string) error {
    87  	cmd := CreateCommand(directory, command, args...)
    88  	cmd.Stdout = os.Stdout
    89  	cmd.Stderr = os.Stderr
    90  	err := cmd.Run()
    91  	return err
    92  }
    93  
    94  // CommandExists returns true if the given command can be found on the shell
    95  func CommandExists(name string) bool {
    96  	_, err := exec.LookPath(name)
    97  	return err == nil
    98  }