capture the output for Run()

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-06-04 20:20:15 -07:00
parent 7e68be7419
commit 04029988b1
5 changed files with 46 additions and 8 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
*.swp
example1/example1 example1/example1
example2/example2

View File

@ -12,7 +12,6 @@ import "os"
import "git.wit.com/wit/shell" import "git.wit.com/wit/shell"
func main() { func main() {
shell.Run("ls /tmp") shell.Run("ls /tmp")
shell.Run("ping -c 3 localhost") shell.Run("ping -c 3 localhost")

3
example2/Makefile Normal file
View File

@ -0,0 +1,3 @@
all:
go build
./example2

11
example2/main.go Normal file
View File

@ -0,0 +1,11 @@
package main
import "log"
import "fmt"
import "git.wit.com/wit/shell"
func main() {
tmp, output, err := shell.Run("cat /etc/issue")
log.Println("cat /etc/issue returned", tmp, "error =", err)
fmt.Print(output)
}

View File

@ -1,11 +1,14 @@
package shell package shell
// import "log" import "fmt"
import "strings" import "strings"
import "time" import "time"
import "os" import "os"
import "os/exec" import "os/exec"
import "bufio" import "bufio"
import "bytes"
import "io"
import "github.com/davecgh/go-spew/spew" import "github.com/davecgh/go-spew/spew"
import "github.com/svent/go-nbreader" import "github.com/svent/go-nbreader"
@ -18,6 +21,8 @@ var shellStderr *os.File
var spewOn bool = false var spewOn bool = false
var msecDelay int = 20 // number of milliseconds to delay between reads with no data var msecDelay int = 20 // number of milliseconds to delay between reads with no data
var buf bytes.Buffer
func Script(cmds string) int { func Script(cmds string) int {
// split on new lines (while we are at it, handle stupid windows text files // split on new lines (while we are at it, handle stupid windows text files
lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n") lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n")
@ -53,14 +58,26 @@ func SetStderr(newerr *os.File) {
shellStderr = newerr shellStderr = newerr
} }
func Run(cmdline string) int { /*
func Capture(cmdline string) (int, string) {
val, _, _ := Run(cmdline)
if (val != 0) {
log.Println("shell.Capture() ERROR")
}
return val, buf.String()
}
*/
func Run(cmdline string) (int, string, error) {
log.Println("START " + cmdline) log.Println("START " + cmdline)
cmd := strings.TrimSpace(cmdline) // this is like 'chomp' in perl cmd := strings.TrimSpace(cmdline) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd) cmdArgs := strings.Fields(cmd)
if (len(cmdArgs) == 0) { if (len(cmdArgs) == 0) {
log.Println("END ", cmd) log.Println("END ", cmd)
return 0 // nothing to do return 0, "", fmt.Errorf("") // nothing to do
} }
if (cmdArgs[0] == "cd") { if (cmdArgs[0] == "cd") {
if (len(cmdArgs) > 1) { if (len(cmdArgs) > 1) {
@ -68,7 +85,7 @@ func Run(cmdline string) int {
os.Chdir(cmdArgs[1]) os.Chdir(cmdArgs[1])
} }
log.Println("END ", cmd) log.Println("END ", cmd)
return 0 // nothing to do return 0, "", fmt.Errorf("") // nothing to do
} }
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
@ -122,6 +139,7 @@ func Run(cmdline string) int {
empty = true empty = true
} else { } else {
log.Println("STDOUT: count = ", count) log.Println("STDOUT: count = ", count)
io.WriteString(&buf, string(oneByte))
f.Write(oneByte[0:count]) f.Write(oneByte[0:count])
f.Flush() f.Flush()
} }
@ -143,10 +161,14 @@ func Run(cmdline string) int {
stuff := err.(*exec.ExitError) stuff := err.(*exec.ExitError)
log.Println("ERROR ", stuff) log.Println("ERROR ", stuff)
log.Println("END ", cmdline) log.Println("END ", cmdline)
return -1 return -1, "", err
} }
log.Println("END ", cmdline) // log.Println("shell.Run() END buf =", buf)
return 0 // log.Println("shell.Run() END string(buf) =", string(buf))
// log.Println("shell.Run() END buf.String() =", buf.String())
// log.Println("shell.Run() END string(buf.Bytes()) =", string(buf.Bytes()))
log.Println("shell.Run() END ", cmdline)
return 0, buf.String(), fmt.Errorf("") // nothing to do
} }
func Daemon(cmdline string, timeout time.Duration) int { func Daemon(cmdline string, timeout time.Duration) int {