summaryrefslogtreecommitdiff
path: root/run.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-01-25 00:39:14 -0600
committerJeff Carr <[email protected]>2024-01-25 00:39:14 -0600
commit41fe4a4659d50d70a835224405490588019d24ff (patch)
treedd6677fb17d11a4233568fddf0d3a141434bba10 /run.go
parente24c2c2eb3feb5083a74a9b2cb396bcbcb0ac52d (diff)
new homev0.13.0
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'run.go')
-rw-r--r--run.go50
1 files changed, 25 insertions, 25 deletions
diff --git a/run.go b/run.go
index 0788105..ed35653 100644
--- a/run.go
+++ b/run.go
@@ -1,14 +1,14 @@
package shell
import (
- "strings"
- "time"
- "os/exec"
+ "bufio"
"bytes"
- "io"
"fmt"
+ "io"
"os"
- "bufio"
+ "os/exec"
+ "strings"
+ "time"
"github.com/svent/go-nbreader"
@@ -31,7 +31,7 @@ func Run(cmdline string) string {
func (cmd *Shell) Run(cmdline string) string {
cmd.InitProcess(cmdline)
- if (cmd.Error != nil) {
+ if cmd.Error != nil {
return ""
}
cmd.Exec(cmdline)
@@ -39,17 +39,17 @@ func (cmd *Shell) Run(cmdline string) string {
}
func (cmd *Shell) InitProcess(cmdline string) {
- log.Log(RUN, "shell.InitProcess() START " + cmdline)
+ log.Log(RUN, "shell.InitProcess() START "+cmdline)
cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd.Cmdline)
- if (len(cmdArgs) == 0) {
+ if len(cmdArgs) == 0 {
cmd.Error = fmt.Errorf("cmdline == ''")
cmd.Done = true
return
}
- if (cmdArgs[0] == "cd") {
- if (len(cmdArgs) > 1) {
+ if cmdArgs[0] == "cd" {
+ if len(cmdArgs) > 1 {
log.Log(RUN, "os.Chdir()", cmd)
os.Chdir(cmdArgs[1])
}
@@ -65,17 +65,17 @@ func (cmd *Shell) FileCreate(out string) {
var newfile File
var iof io.ReadCloser
- if (out == "STDOUT") {
- iof, _ = cmd.Process.StdoutPipe()
+ if out == "STDOUT" {
+ iof, _ = cmd.Process.StdoutPipe()
} else {
- iof, _ = cmd.Process.StderrPipe()
+ iof, _ = cmd.Process.StderrPipe()
}
newfile.Fio = iof
newfile.Fbufio = bufio.NewReader(iof)
newfile.Fnbreader = nbreader.NewNBReader(newfile.Fbufio, 1024)
- if (out == "STDOUT") {
+ if out == "STDOUT" {
cmd.STDOUT = &newfile
} else {
cmd.STDERR = &newfile
@@ -85,10 +85,10 @@ func (cmd *Shell) FileCreate(out string) {
// NOTE: this might cause problems:
// always remove the newlines at the end ?
func (cmd *Shell) Exec(cmdline string) {
- log.Log(RUN, "shell.Run() START " + cmdline)
+ log.Log(RUN, "shell.Run() START "+cmdline)
cmd.InitProcess(cmdline)
- if (cmd.Error != nil) {
+ if cmd.Error != nil {
return
}
@@ -111,7 +111,7 @@ func (cmd *Shell) Exec(cmdline string) {
// time.Sleep(2 * time.Second) // putting this here doesn't help STDOUT flush()
- if (err != nil) {
+ if err != nil {
cmd.Fail = true
cmd.Error = err
log.Log(RUN, "process.Wait() END err =", err.Error())
@@ -125,10 +125,10 @@ func (cmd *Shell) Exec(cmdline string) {
func (cmd *Shell) Capture(f *File) {
log.Log(RUN, "nbrREADER() START")
- if (cmd.Buffer == nil) {
+ if cmd.Buffer == nil {
cmd.Buffer = new(bytes.Buffer)
}
- if (cmd.Buffer == nil) {
+ if cmd.Buffer == nil {
f.Dead = false
cmd.Error = fmt.Errorf("could not make buffer")
log.Error(cmd.Error, "f.Buffer == nil")
@@ -139,15 +139,15 @@ func (cmd *Shell) Capture(f *File) {
f.Dead = false
// loop that keeps trying to read from f
- for (f.Dead == false) {
- time.Sleep(time.Duration(msecDelay) * time.Millisecond) // only check the buffer 500 times a second
+ for f.Dead == false {
+ time.Sleep(time.Duration(msecDelay) * time.Millisecond) // only check the buffer 500 times a second
// set to false so it keeps retrying reads
f.Empty = false
// tight loop that reads 1024 bytes at a time until buffer is empty
// 1024 is set in f.BufferSize
- for (f.Empty == false) {
+ for f.Empty == false {
f.Empty = cmd.ReadToBuffer(f)
}
}
@@ -158,7 +158,7 @@ func (cmd *Shell) ReadToBuffer(f *File) bool {
log.Log(RUN, "ReadToBuffer() START")
nbr := f.Fnbreader
oneByte := make([]byte, 1024)
- if (nbr == nil) {
+ if nbr == nil {
// log.Debugln("ReadToBuffer() ERROR nbr is nil")
f.Dead = true
return true
@@ -166,13 +166,13 @@ func (cmd *Shell) ReadToBuffer(f *File) bool {
count, err := nbr.Read(oneByte)
f.TotalCount += count
- if (err != nil) {
+ if err != nil {
// log.Debugln("ReadToBuffer() file has closed with", err)
// log.Debugln("ReadToBuffer() count = ", count, "err = ", err)
f.Dead = true
return true
}
- if (count == 0) {
+ if count == 0 {
// log.Debugln("ReadToBuffer() START count == 0 return true")
return true
}