This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PipedExec.go
164 lines (137 loc) · 3.23 KB
/
PipedExec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) 2019-present unTill Pro, Ltd. and Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
Links
- https://stackoverflow.com/questions/25190971/golang-copy-exec-output-to-log
*/
package gochips
import (
"bytes"
"errors"
"io"
"os"
"os/exec"
"sync"
)
// https://github.com/b4b4r07/go-pipe/blob/master/README.md
// PipedExec allows to execute commands in pipe
type PipedExec struct {
cmds []*pipedCmd
}
// Stderr redirection
const (
StderrRedirectNone = iota
StderrRedirectStdout
StderrRedirectNull
)
type pipedCmd struct {
stderrRedirection int
cmd *exec.Cmd
}
func (Self *PipedExec) command(name string, stderrRedirection int, args ...string) *PipedExec {
cmd := exec.Command(name, args...)
lastIdx := len(Self.cmds) - 1
if lastIdx > -1 {
var err error
cmd.Stdin, err = Self.cmds[lastIdx].cmd.StdoutPipe()
PanicIfError(err)
} else {
cmd.Stdin = os.Stdin
}
Self.cmds = append(Self.cmds, &pipedCmd{stderrRedirection, cmd})
return Self
}
// GetCmd returns cmd with given index
func (Self *PipedExec) GetCmd(idx int) *exec.Cmd {
return Self.cmds[idx].cmd
}
// Command adds a command to a pipe
func (Self *PipedExec) Command(name string, args ...string) *PipedExec {
return Self.command(name, StderrRedirectNone, args...)
}
// WorkingDir sets working directory for the last command
func (Self *PipedExec) WorkingDir(wd string) *PipedExec {
pipedCmd := Self.cmds[len(Self.cmds)-1]
pipedCmd.cmd.Dir = wd
return Self
}
// Wait until all cmds finish
func (Self *PipedExec) Wait() error {
var firstErr error
for _, cmd := range Self.cmds {
err := cmd.cmd.Wait()
if nil != err && firstErr == nil {
firstErr = err
}
}
return firstErr
}
// Start all cmds
func (Self *PipedExec) Start(out io.Writer, err io.Writer) error {
for _, cmd := range Self.cmds {
if cmd.stderrRedirection == StderrRedirectNone && nil != err {
cmd.cmd.Stderr = err
}
}
lastIdx := len(Self.cmds) - 1
if lastIdx < 0 {
return errors.New("Empty command list")
}
if nil != out {
Self.cmds[lastIdx].cmd.Stdout = out
}
for _, cmd := range Self.cmds {
Verbose("PipedExec", cmd.cmd.Path, cmd.cmd.Args)
err := cmd.cmd.Start()
if nil != err {
return err
}
}
return nil
}
// Run starts the pipe
func (Self *PipedExec) Run(out io.Writer, err io.Writer) error {
e := Self.Start(out, err)
if nil != e {
return e
}
return Self.Wait()
}
// RunToStrings runs the pipe and saves outputs to strings
func (Self *PipedExec) RunToStrings() (stdout string, stderr string, err error) {
// _, stdoutw := io.Pipe()
// _, stderrw := io.Pipe()
var wg sync.WaitGroup
wg.Add(2)
lastCmd := Self.cmds[len(Self.cmds)-1]
stdoutPipe, err := lastCmd.cmd.StdoutPipe()
if nil != err {
return "", "", err
}
stderrPipe, err := lastCmd.cmd.StderrPipe()
if nil != err {
return "", "", err
}
err = Self.Start(nil, nil)
if nil != err {
return "", "", err
}
go func() {
defer wg.Done()
buf := new(bytes.Buffer)
buf.ReadFrom(stdoutPipe)
stdout = buf.String()
}()
go func() {
defer wg.Done()
buf := new(bytes.Buffer)
buf.ReadFrom(stderrPipe)
stderr = buf.String()
}()
wg.Wait()
return stdout, stderr, Self.Wait()
}