go-cmd output logging option

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-13 10:33:54 -06:00
parent d887da32f2
commit d986f8f331
2 changed files with 27 additions and 8 deletions

31
cmd.go
View File

@ -31,8 +31,8 @@ import (
// shortcut, sends a blank value for pwd
// which means the exec Dir is not set
// echos output (otherwise use RunQuiet)
func Run(args []string) cmd.Status {
return PathRun("", args)
func Run(argv []string) cmd.Status {
return PathRun("", argv)
}
// exec the cmd at a filepath. this does not change the working directory
@ -42,10 +42,20 @@ func Run(args []string) cmd.Status {
// this is basically the exact example from the go-cmd/cmd devs
// where the have rocked out a proper smart read on both filehandles
// https://dave.cheney.net/2013/04/30/curious-channels
func PathRun(path string, args []string) cmd.Status {
func PathRun(path string, argv []string) cmd.Status {
return PathRunLog(path, argv, NOW)
}
// the actual wrapper around go-cmd/cmd
// adds a log Flag so that echo to stdout can be enabled/disabled
func PathRunLog(path string, args []string, logf *log.LogFlag) cmd.Status {
var save []string // combined stdout & stderr
var arg0 string
var argx []string
if logf == nil {
logf = NOW
}
log.Log(logf, "shell.PathRunLog():", args)
// Check if the slice has at least one element (the command name)
if len(args) == 0 {
var s cmd.Status
@ -87,14 +97,16 @@ func PathRun(path string, args []string) cmd.Status {
continue
}
save = append(save, line)
fmt.Println(line)
log.Log(logf, line)
// fmt.Println(line)
case line, open := <-envCmd.Stderr:
if !open {
envCmd.Stderr = nil
continue
}
save = append(save, line)
fmt.Println(line)
log.Log(logf, line)
// fmt.Println(line)
}
}
}()
@ -110,8 +122,14 @@ func PathRun(path string, args []string) cmd.Status {
return s
}
// absolutely doesn't echo anything
// only echos if you enable the shell.INFO log flag
func PathRunQuiet(pwd string, args []string) cmd.Status {
return PathRunLog(pwd, args, INFO)
}
// absolutely doesn't echo anything
// bad for now. leaves goroutines running all over the place
func PathRunQuietBad(pwd string, args []string) cmd.Status {
// Check if the slice has at least one element (the command name)
if len(args) == 0 {
var s cmd.Status
@ -137,6 +155,7 @@ func PathRunQuiet(pwd string, args []string) cmd.Status {
n := len(status.Stdout)
if n != 0 {
fmt.Println("todo:removethisecho", status.Stdout[n-1])
fmt.Println("status", status.Exit)
}
}
}()

View File

@ -123,7 +123,7 @@ func XtermCmdWait(path string, cmd []string) {
// keeps git diff from exiting on small diffs
os.Setenv("LESS", "-+F -+X -R")
PathRun(path, argsXterm)
PathRunLog(path, argsXterm, INFO)
}
// spawns an xterm with something you can run at a command line
@ -136,5 +136,5 @@ func XtermCmdBash(path string, cmd []string) {
bash += "'; bash\""
tmp = append(argsXterm, "bash", bash)
log.Info("XtermCmd() path =", path, "cmd =", tmp)
go PathRun(path, tmp)
go PathRunLog(path, tmp, INFO)
}