summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--example1/main.go1
-rw-r--r--example2/Makefile3
-rw-r--r--example2/main.go11
-rw-r--r--shell.go36
5 files changed, 46 insertions, 8 deletions
diff --git a/.gitignore b/.gitignore
index d24c859..efcd838 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,4 @@
+*.swp
+
example1/example1
+example2/example2
diff --git a/example1/main.go b/example1/main.go
index ee48938..39ce6fc 100644
--- a/example1/main.go
+++ b/example1/main.go
@@ -12,7 +12,6 @@ import "os"
import "git.wit.com/wit/shell"
func main() {
-
shell.Run("ls /tmp")
shell.Run("ping -c 3 localhost")
diff --git a/example2/Makefile b/example2/Makefile
new file mode 100644
index 0000000..250e900
--- /dev/null
+++ b/example2/Makefile
@@ -0,0 +1,3 @@
+all:
+ go build
+ ./example2
diff --git a/example2/main.go b/example2/main.go
new file mode 100644
index 0000000..d8f39b4
--- /dev/null
+++ b/example2/main.go
@@ -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)
+}
diff --git a/shell.go b/shell.go
index b1f7d10..fba932b 100644
--- a/shell.go
+++ b/shell.go
@@ -1,11 +1,14 @@
package shell
-// import "log"
+import "fmt"
import "strings"
import "time"
import "os"
import "os/exec"
import "bufio"
+import "bytes"
+import "io"
+
import "github.com/davecgh/go-spew/spew"
import "github.com/svent/go-nbreader"
@@ -18,6 +21,8 @@ var shellStderr *os.File
var spewOn bool = false
var msecDelay int = 20 // number of milliseconds to delay between reads with no data
+var buf bytes.Buffer
+
func Script(cmds string) int {
// 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")
@@ -53,14 +58,26 @@ func SetStderr(newerr *os.File) {
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)
cmd := strings.TrimSpace(cmdline) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd)
if (len(cmdArgs) == 0) {
log.Println("END ", cmd)
- return 0 // nothing to do
+ return 0, "", fmt.Errorf("") // nothing to do
}
if (cmdArgs[0] == "cd") {
if (len(cmdArgs) > 1) {
@@ -68,7 +85,7 @@ func Run(cmdline string) int {
os.Chdir(cmdArgs[1])
}
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)]...)
@@ -122,6 +139,7 @@ func Run(cmdline string) int {
empty = true
} else {
log.Println("STDOUT: count = ", count)
+ io.WriteString(&buf, string(oneByte))
f.Write(oneByte[0:count])
f.Flush()
}
@@ -143,10 +161,14 @@ func Run(cmdline string) int {
stuff := err.(*exec.ExitError)
log.Println("ERROR ", stuff)
log.Println("END ", cmdline)
- return -1
+ return -1, "", err
}
- log.Println("END ", cmdline)
- return 0
+ // log.Println("shell.Run() END buf =", buf)
+ // 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 {