mirror of
https://github.com/taigrr/wails.git
synced 2026-04-15 11:21:15 -07:00
Squashed 'v2/' content from commit 72ef153
git-subtree-dir: v2 git-subtree-split: 72ef15359e36e42b18d9407f74c762f83eb9a099
This commit is contained in:
90
internal/shell/shell.go
Normal file
90
internal/shell/shell.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type Command struct {
|
||||
command string
|
||||
args []string
|
||||
env []string
|
||||
dir string
|
||||
stdo, stde bytes.Buffer
|
||||
}
|
||||
|
||||
func NewCommand(command string) *Command {
|
||||
return &Command{
|
||||
command: command,
|
||||
env: os.Environ(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Command) Dir(dir string) {
|
||||
c.dir = dir
|
||||
}
|
||||
|
||||
func (c *Command) Env(name string, value string) {
|
||||
c.env = append(c.env, name+"="+value)
|
||||
}
|
||||
|
||||
func (c *Command) Run() error {
|
||||
cmd := exec.Command(c.command, c.args...)
|
||||
if c.dir != "" {
|
||||
cmd.Dir = c.dir
|
||||
}
|
||||
cmd.Stdout = &c.stdo
|
||||
cmd.Stderr = &c.stde
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (c *Command) Stdout() string {
|
||||
return c.stdo.String()
|
||||
}
|
||||
func (c *Command) Stderr() string {
|
||||
return c.stde.String()
|
||||
}
|
||||
|
||||
func (c *Command) AddArgs(args []string) {
|
||||
for _, arg := range args {
|
||||
c.args = append(c.args, arg)
|
||||
}
|
||||
}
|
||||
|
||||
// CreateCommand returns a *Cmd struct that when run, will run the given command + args in the given directory
|
||||
func CreateCommand(directory string, command string, args ...string) *exec.Cmd {
|
||||
cmd := exec.Command(command, args...)
|
||||
cmd.Dir = directory
|
||||
return cmd
|
||||
}
|
||||
|
||||
// RunCommand will run the given command + args in the given directory
|
||||
// Will return stdout, stderr and error
|
||||
func RunCommand(directory string, command string, args ...string) (string, string, error) {
|
||||
cmd := CreateCommand(directory, command, args...)
|
||||
var stdo, stde bytes.Buffer
|
||||
cmd.Stdout = &stdo
|
||||
cmd.Stderr = &stde
|
||||
err := cmd.Run()
|
||||
return stdo.String(), stde.String(), err
|
||||
}
|
||||
|
||||
// RunCommandVerbose will run the given command + args in the given directory
|
||||
// Will return an error if one occurs
|
||||
func RunCommandVerbose(directory string, command string, args ...string) error {
|
||||
cmd := CreateCommand(directory, command, args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
return err
|
||||
}
|
||||
|
||||
// CommandExists returns true if the given command can be found on the shell
|
||||
func CommandExists(name string) bool {
|
||||
_, err := exec.LookPath(name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user