From 830d37fa004838972ebacec15e621beaef7e13c2 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 5 May 2024 16:29:17 +0200 Subject: [PATCH] feat: overwrite Cmd.Run to set defaults (#74) --- types/command.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/types/command.go b/types/command.go index 2e6ba95..224ca4f 100644 --- a/types/command.go +++ b/types/command.go @@ -1,8 +1,52 @@ package types -import "golang.org/x/sys/execabs" +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 }