Skip to content

Commit

Permalink
Add prompt option and exit-code return-value to RunCmdWithOutputParser
Browse files Browse the repository at this point in the history
  • Loading branch information
barbelity committed Feb 10, 2019
1 parent 6b614ac commit a83238b
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions io/cmd.go
Expand Up @@ -57,7 +57,7 @@ func RunCmd(config CmdConfig) error {
// Executes the command and captures the output.
// Analyze each line to match the provided regex.
// Returns the complete stdout output of the command.
func RunCmdWithOutputParser(config CmdConfig, regExpStruct ...*CmdOutputPattern) (string, error) {
func RunCmdWithOutputParser(config CmdConfig, prompt bool, regExpStruct ...*CmdOutputPattern) (out string, exitOk bool, err error) {
var wg sync.WaitGroup
for k, v := range config.GetEnv() {
os.Setenv(k, v)
Expand All @@ -66,22 +66,21 @@ func RunCmdWithOutputParser(config CmdConfig, regExpStruct ...*CmdOutputPattern)
cmd := config.GetCmd()
cmdReader, err := cmd.StdoutPipe()
if err != nil {
return "", err
return
}
defer cmdReader.Close()
scanner := bufio.NewScanner(cmdReader)
cmdReaderStderr, err := cmd.StderrPipe()
if err != nil {
return "", err
return
}
defer cmdReaderStderr.Close()
scannerStderr := bufio.NewScanner(cmdReaderStderr)
err = cmd.Start()
if err != nil {
return "", err
return
}
errChan := make(chan error)
var stdoutOutput string
wg.Add(1)
go func() {
for scanner.Scan() {
Expand All @@ -97,8 +96,10 @@ func RunCmdWithOutputParser(config CmdConfig, regExpStruct ...*CmdOutputPattern)
}
}
}
fmt.Println(line)
stdoutOutput += line + "\n"
if prompt {
fmt.Println(line)
}
out += line + "\n"
}
wg.Done()
}()
Expand All @@ -119,7 +120,9 @@ func RunCmdWithOutputParser(config CmdConfig, regExpStruct ...*CmdOutputPattern)
}
}
}
fmt.Fprintf(os.Stderr, line+"\n")
if prompt {
fmt.Fprintf(os.Stderr, line+"\n")
}
if scannerError != nil {
break
}
Expand All @@ -132,10 +135,20 @@ func RunCmdWithOutputParser(config CmdConfig, regExpStruct ...*CmdOutputPattern)
close(errChan)
}()

for err := range errChan {
return stdoutOutput, err
for err = range errChan {
return
}

err = cmd.Wait()
if err != nil {
return
}
exitOk = true
if _, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
exitOk = false
}
return stdoutOutput, nil
return
}

type CmdConfig interface {
Expand Down

0 comments on commit a83238b

Please sign in to comment.