0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-22 00:20:38 +00:00
wp-plugin-go/types/command.go

53 lines
620 B
Go

package types
import (
"fmt"
"io"
"os"
"strings"
"golang.org/x/sys/execabs"
)
type Cmd struct {
*execabs.Cmd
Private bool
Trace *bool
}
func (c *Cmd) Run() error {
if c.Trace == nil {
c.SetTrace(true)
}
if c.Env == nil {
c.Env = os.Environ()
}
if c.Stdout == nil {
c.Stdout = os.Stdout
}
if c.Stderr == nil {
c.Stderr = os.Stderr
}
if c.Private {
c.Stdout = io.Discard
}
if *c.Trace {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(c.Args, " "))
}
if err := c.Start(); err != nil {
return err
}
return c.Wait()
}
func (c *Cmd) SetTrace(trace bool) {
c.Trace = &trace
}