summaryrefslogtreecommitdiff
path: root/cmd.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-13 10:33:54 -0600
committerJeff Carr <[email protected]>2024-11-13 10:33:54 -0600
commitd986f8f33181d892c2dd220c145863fc3fe6ce14 (patch)
tree7719d8b72db25c35ebc3a40b8b8f6d064aaf4d59 /cmd.go
parentd887da32f20feee8bfbebf7b2db63f1a42fd7176 (diff)
go-cmd output logging option
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'cmd.go')
-rw-r--r--cmd.go31
1 files changed, 25 insertions, 6 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)
}
}
}()