From d986f8f33181d892c2dd220c145863fc3fe6ce14 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 13 Nov 2024 10:33:54 -0600 Subject: [PATCH] go-cmd output logging option Signed-off-by: Jeff Carr --- cmd.go | 31 +++++++++++++++++++++++++------ xterm.go | 4 ++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cmd.go b/cmd.go index cfaafd1..907a67f 100644 --- a/cmd.go +++ b/cmd.go @@ -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) } } }() diff --git a/xterm.go b/xterm.go index 051e956..b5aa6f2 100644 --- a/xterm.go +++ b/xterm.go @@ -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) }