0
0
mirror of https://github.com/thegeeklab/wp-plugin-go.git synced 2024-11-24 03:00:40 +00:00

feat: overwrite Cmd.Run to set defaults (#74)

This commit is contained in:
Robert Kaussow 2024-05-05 16:29:17 +02:00 committed by GitHub
parent 25469ebab3
commit 830d37fa00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,8 +1,52 @@
package types package types
import "golang.org/x/sys/execabs" import (
"fmt"
"io"
"os"
"strings"
"golang.org/x/sys/execabs"
)
type Cmd struct { type Cmd struct {
*execabs.Cmd *execabs.Cmd
Private bool 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
} }