/
gf_cli.go
260 lines (205 loc) · 5.81 KB
/
gf_cli.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
GloFlow application and media management/publishing platform
Copyright (C) 2020 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_core
import (
"os"
"fmt"
"strings"
"os/exec"
"bufio"
"io"
)
//-------------------------------------------------
type GFcliCmdInfo struct {
CmdLst []string
StdinDataStr *string // data to be passed via stdin
EnvVarsMap map[string]string
DirStr string
ViewOutputBool bool
}
//-------------------------------------------------
// RUN_STANDARD
func CLIrunStandard(pCmdLst []string,
pEnvVarsMap map[string]string,
pRuntimeSys *RuntimeSys) ([]string, []string, *GFerror) {
cliInfo := &GFcliCmdInfo{
CmdLst: pCmdLst,
EnvVarsMap: pEnvVarsMap,
DirStr: "",
ViewOutputBool: true,
}
stdoutLst, stderrLst, gfErr := CLIrun(cliInfo, pRuntimeSys)
return stdoutLst, stderrLst, gfErr
}
//-------------------------------------------------
// RUN
func CLIrun(pCmdInfo *GFcliCmdInfo,
pRuntimeSys *RuntimeSys) ([]string, []string, *GFerror) {
stdoutCh, stderrCh, gfErr := CLIrunCore(pCmdInfo,
true,
pRuntimeSys)
if gfErr != nil {
return nil, nil, gfErr
}
stdoutLst, stderrLst := consumeOutputs(stdoutCh, stderrCh)
return stdoutLst, stderrLst, nil
}
//-------------------------------------------------
func CLIrunCore(pCmdInfo *GFcliCmdInfo,
pWaitForCompletionBool bool,
pRuntimeSys *RuntimeSys) (chan string, chan string, *GFerror) {
cmdStr := strings.Join(pCmdInfo.CmdLst, " ")
fmt.Printf("%s\n", cmdStr)
cmdNameStr := pCmdInfo.CmdLst[0]
cmdArgsLst := pCmdInfo.CmdLst[1:]
p := exec.Command(cmdNameStr, cmdArgsLst...)
// CMD_DIR
if pCmdInfo.DirStr != "" {
p.Dir = pCmdInfo.DirStr
}
// STDIN
stdin, err := p.StdinPipe()
if err != nil {
gfErr := ErrorCreate("failed to get STDIN pipe of a CLI process",
"cli_run_error",
map[string]interface{}{"cmd": cmdStr,},
err, "gf_core", pRuntimeSys)
return nil, nil, gfErr
}
// defer stdin.Close()
// STDOUT
cmdStdoutReader, _ := p.StdoutPipe()
cmdStdoutBuffer := bufio.NewReader(cmdStdoutReader)
// STDERR
cmdStderrReader, _ := p.StderrPipe()
cmdStderrBuffer := bufio.NewReader(cmdStderrReader)
// done_ch := make(chan bool)
//----------------------
// START
err = p.Start()
if err != nil {
gfErr := ErrorCreate("failed to Start a CLI command",
"cli_run_error",
map[string]interface{}{"cmd": cmdStr,},
err, "gf_core", pRuntimeSys)
return nil, nil, gfErr
}
//----------------------
// STDIN - input is written to stdin after the process is started
if pCmdInfo.StdinDataStr != nil {
io.WriteString(stdin, fmt.Sprintf("%s\n", *pCmdInfo.StdinDataStr))
}
//----------------------
// STDOUT
stdOutCh := make(chan string, 10000)
go func(p_stdOutCh chan string) {
for {
l, err := cmdStdoutBuffer.ReadString('\n')
if fmt.Sprint(err) == "EOF" {
p_stdOutCh <- "EOF"
// done_ch <- true
return
}
if err != nil {
continue
}
if pCmdInfo.ViewOutputBool {
fmt.Printf("%s", l)
}
lStr := strings.TrimSuffix(l, "\n")
p_stdOutCh <- lStr
}
}(stdOutCh)
//----------------------
// STDERR
stdErrCh := make(chan string, 10000)
go func() {
for {
l, err := cmdStderrBuffer.ReadString('\n')
// if fmt.Sprint(err) == "EOF" {
// done_ch <- true
// return
// }
if err != nil {
continue
}
if pCmdInfo.ViewOutputBool {
fmt.Printf("%s", l)
}
lStr := strings.TrimSuffix(l, "\n")
stdErrCh <- lStr
}
}()
//----------------------
// WAIT
if pWaitForCompletionBool {
err = p.Wait()
if err != nil {
stdoutLst, stderrLst := consumeOutputs(stdOutCh, stdErrCh)
gfErr := ErrorCreate("failed to Wait for a CLI command to complete",
"cli_run_error",
map[string]interface{}{
"cmd": cmdStr,
"stdout_lst": stdoutLst,
"stderr_lst": stderrLst,
},
err, "gf_core", pRuntimeSys)
return nil, nil, gfErr
}
}
//----------------------
return stdOutCh, stdErrCh, nil // done_ch, nil
}
//-------------------------------------------------
func consumeOutputs(pStdoutCh chan string, pStderrCh chan string) ([]string, []string) {
stdoutLst := []string{}
stderrLst := []string{}
for {
select {
case stdoutLineStr := <- pStdoutCh:
if stdoutLineStr == "EOF" {
return stdoutLst, stderrLst
} else {
stdoutLst = append(stdoutLst, stdoutLineStr)
}
case stderr_l_str := <- pStderrCh:
stderrLst = append(stdoutLst, stderr_l_str)
// case _ = <- done_ch:
// return stdoutLst, stderrLst
}
}
return nil, nil
}
//-------------------------------------------------
// prompt the user for a confirmation to proceed or not
func CLIprompt(pMsgStr string, pRuntimeSys *RuntimeSys) (bool, *GFerror) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [y/n]: ", pMsgStr)
textStr, _ := reader.ReadString('\n')
textCleanStr := strings.TrimSuffix(textStr, "\n")
if textCleanStr == "y" {
return true, nil
} else if textCleanStr == "n" {
return false, nil
} else {
gfErr := ErrorCreate("invalid confirmation input added by user (y|n) accepted",
"generic_error",
map[string]interface{}{"text_str": textCleanStr,},
nil, "gf_core", pRuntimeSys)
return false, gfErr
}
return false, nil
}