sys - amend PipeCmd

This commit is contained in:
Mahdi Abu Yasmine 2023-11-07 22:28:33 +01:00
parent 65a41f6873
commit e88b75c71f

View File

@ -74,15 +74,18 @@ func RunInteractiveCmd(cmd string, withStderr bool, buffers ...*bytes.Buffer) in
return ManageStatusCmd(icmd, icmd.Run()) return ManageStatusCmd(icmd, icmd.Run())
} }
func PipeCmd(cmd1 *exec.Cmd, cmd2 *exec.Cmd, buffers ...io.Writer) { func PipeCmd(cmd1 *exec.Cmd, cmd2 *exec.Cmd, buffers ...io.Writer) int {
status := -1
// fmt.Println("> Pipe Commands") // fmt.Println("> Pipe Commands")
if len(buffers) > 0 { if len(buffers) > 0 {
errIndex := 0
pr, pw := io.Pipe() pr, pw := io.Pipe()
cmd1.Stdout = pw cmd1.Stdout = pw
cmd2.Stdin = pr cmd2.Stdin = pr
cmd2.Stdout = buffers[0] cmd2.Stdout = buffers[errIndex]
if len(buffers) > 1 { if len(buffers) > 1 {
cmd2.Stderr = buffers[1] errIndex = 1
cmd2.Stderr = buffers[errIndex]
} }
cmd1.Start() cmd1.Start()
@ -92,16 +95,20 @@ func PipeCmd(cmd1 *exec.Cmd, cmd2 *exec.Cmd, buffers ...io.Writer) {
defer pw.Close() defer pw.Close()
err1 := cmd1.Wait() err1 := cmd1.Wait()
if err1 != nil { if err1 != nil {
fmt.Printf("error1 : %v\n", err1) buffers[errIndex].Write([]byte(fmt.Sprintf(" Error pipe in : %v\n", err1)))
} }
}() }()
err2 := cmd2.Wait() err2 := cmd2.Wait()
if err2 != nil { if err2 != nil {
fmt.Printf("error2 : %v\n", err2) buffers[errIndex].Write([]byte(fmt.Sprintf(" Error pipe out : %v\n", err2)))
status = 1
} else {
status = 0
} }
// fmt.Println("< Pipe Commands") // fmt.Println("< Pipe Commands")
} }
return status
} }
func UpdateTermSize() error { func UpdateTermSize() error {