-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash.go
More file actions
358 lines (292 loc) · 8.05 KB
/
bash.go
File metadata and controls
358 lines (292 loc) · 8.05 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package bash
import (
"io"
"os"
"os/exec"
)
/*
Run will run a bash command based on the given args
note: stdin is piped to the os logs
@dir: a directory to run the command in (set to an empty string to disable)
@env: an optional list of environment variables (set to nil to disable)
[optional]
@liveOutput[0]: set to true to pipe stdout and stderr to the os
@liveOutput[1]: set to false to only pipe stdout to the os, and keep stderr hidden
*/
func Run(args []string, dir string, env []string, liveOutput ...bool) (output []byte, err error) {
arg1 := args[0]
args = args[1:]
cmd := exec.Command(arg1, args...)
if dir != "" {
cmd.Dir = dir
}
if cmd.Env == nil {
cmd.Env = os.Environ()
}
if env != nil {
cmd.Env = append(cmd.Env, env...)
}
cmd.Stdin = os.Stdin
if len(liveOutput) != 0 && liveOutput[0] == true {
cmd.Stdout = os.Stdout
if len(liveOutput) <= 1 || liveOutput[1] == true {
cmd.Stderr = os.Stderr
}
return []byte{}, cmd.Run()
}
return cmd.CombinedOutput()
}
/*
RunRaw will run an unescaped (unquoted) bash command as a different user
this method uses `bash -c` to get around the auto quotes added by golang
note: user input is Not recommended for this method
note: stdin is piped to the os logs
@cmdStr: the command to run
@dir: a directory to run the command in (set to an empty string to disable)
@env: an optional list of environment variables (set to nil to disable)
[optional]
@liveOutput[0]: set to true to pipe stdout and stderr to the os
@liveOutput[1]: set to false to only pipe stdout to the os, and keep stderr hidden
*/
func RunRaw(cmdStr string, dir string, env []string, liveOutput ...bool) (output []byte, err error) {
cmd := exec.Command(`bash`, `-c`, cmdStr)
if dir != "" {
cmd.Dir = dir
}
if cmd.Env == nil {
cmd.Env = os.Environ()
}
if env != nil {
cmd.Env = append(cmd.Env, env...)
}
cmd.Stdin = os.Stdin
if len(liveOutput) != 0 && liveOutput[0] == true {
cmd.Stdout = os.Stdout
if len(liveOutput) <= 1 || liveOutput[1] == true {
cmd.Stderr = os.Stderr
}
return []byte{}, cmd.Run()
}
return cmd.CombinedOutput()
}
/*
RunUser will run an unescaped (unquoted) bash command as a specified user
this method uses `runuser -l [user] -c`
note: user input is Not recommended for this method
note: stdin is piped to the os logs
@cmdStr: the command to run
@user: the username to run the command as
@dir: a directory to run the command in (set to an empty string to disable)
@env: an optional list of environment variables (set to nil to disable)
[optional]
@liveOutput[0]: set to true to pipe stdout and stderr to the os
@liveOutput[1]: set to false to only pipe stdout to the os, and keep stderr hidden
*/
func RunUser(cmdStr string, user string, dir string, env []string, liveOutput ...bool) (output []byte, err error) {
cmd := exec.Command(`runuser`, `-l`, user, `-c`, cmdStr)
if dir != "" {
cmd.Dir = dir
}
if cmd.Env == nil {
cmd.Env = os.Environ()
}
if env != nil {
cmd.Env = append(cmd.Env, env...)
}
cmd.Stdin = os.Stdin
if len(liveOutput) != 0 && liveOutput[0] == true {
cmd.Stdout = os.Stdout
if len(liveOutput) <= 1 || liveOutput[1] == true {
cmd.Stderr = os.Stderr
}
return []byte{}, cmd.Run()
}
return cmd.CombinedOutput()
}
/*
RunUserFile will run a bash file as a specified user
this method uses `pkexec --user [user]` to simulate a user in a normal desktop environment
note: user input is Not recommended for this method
note: stdin is piped to the os logs
@file: the file to run
@user: the username to run the command as
@dir: a directory to run the command in (set to an empty string to disable)
@env: an optional list of environment variables (set to nil to disable)
[optional]
@liveOutput[0]: set to true to pipe stdout and stderr to the os
@liveOutput[1]: set to false to only pipe stdout to the os, and keep stderr hidden
*/
func RunUserFile(file string, args []string, user string, dir string, env []string, liveOutput ...bool) (output []byte, err error) {
cmd := exec.Command(`pkexec`, append([]string{`--user`, user, file}, args...)...)
if dir != "" {
cmd.Dir = dir
}
if cmd.Env == nil {
cmd.Env = os.Environ()
}
if env != nil {
cmd.Env = append(cmd.Env, env...)
}
cmd.Stdin = os.Stdin
if len(liveOutput) != 0 && liveOutput[0] == true {
cmd.Stdout = os.Stdout
if len(liveOutput) <= 1 || liveOutput[1] == true {
cmd.Stderr = os.Stderr
}
return []byte{}, cmd.Run()
}
return cmd.CombinedOutput()
}
/*
Pipe allows you to pipe multiple bash commands
[example (bash)]
echo "test" | tee -a "./test.txt"
[example (go)]
bash.Pipe(".", []string{"echo", "test"}, []string{"tee", "-a", "./test.txt"})
@dir: a directory to run the command in (set to an empty string to disable)
*/
func Pipe(dir string, args ...[]string){
if len(args) == 1 {
arg1 := args[0][0]
args1 := args[0][1:]
c1 := exec.Command(arg1, args1...)
c1.Stdout = os.Stdout
}
cmd := []*exec.Cmd{}
arg0 := args[0][0]
args0 := args[0][1:]
cmd = append(cmd, exec.Command(arg0, args0...))
cmd[0].Stdin = os.Stdin
if dir != "" {
cmd[0].Dir = dir
}
if cmd[0].Env == nil {
cmd[0].Env = os.Environ()
}
for i := 1; i < len(args); i++ {
arg0 = args[i][0]
args0 = args[i][1:]
cmd = append(cmd, exec.Command(arg0, args0...))
pr, pw := io.Pipe()
cmd[i-1].Stdout = pw
cmd[i].Stdin = pr
if dir != "" {
cmd[i].Dir = dir
}
if cmd[i].Env == nil {
cmd[i].Env = os.Environ()
}
cmd[i-1].Start()
go func(i int){
defer pw.Close()
cmd[i-1].Wait()
}(i)
}
cmd[len(cmd)-1].Stdout = os.Stdout
cmd[len(cmd)-1].Start()
cmd[len(cmd)-1].Wait()
}
/*
PipeMultiDir allows you to pipe multiple bash commands with a different directory for each of them
note: the first arg is the directory
[example]
bash.PipeMultiDir([]string{"/dir1", "cat", "test.txt"}, []string{"./dir2", "tee", "-a", "./test.txt"})
*/
func PipeMultiDir(args ...[]string){
if len(args) == 1 {
arg1 := args[0][0]
args1 := args[0][1:]
c1 := exec.Command(arg1, args1...)
c1.Stdout = os.Stdout
}
cmd := []*exec.Cmd{}
dir := args[0][0]
arg0 := args[0][1]
args0 := args[0][2:]
cmd = append(cmd, exec.Command(arg0, args0...))
cmd[0].Stdin = os.Stdin
if dir != "" {
cmd[0].Dir = dir
}
if cmd[0].Env == nil {
cmd[0].Env = os.Environ()
}
for i := 1; i < len(args); i++ {
dir = args[i][0]
arg0 = args[i][1]
args0 = args[i][2:]
cmd = append(cmd, exec.Command(arg0, args0...))
pr, pw := io.Pipe()
cmd[i-1].Stdout = pw
cmd[i].Stdin = pr
if dir != "" {
cmd[i].Dir = dir
}
if cmd[i].Env == nil {
cmd[i].Env = os.Environ()
}
cmd[i-1].Start()
go func(i int){
defer pw.Close()
cmd[i-1].Wait()
}(i)
}
cmd[len(cmd)-1].Stdout = os.Stdout
cmd[len(cmd)-1].Start()
cmd[len(cmd)-1].Wait()
}
/*
PipeMultiDirEnv is just like the 'PipeMultiDir' method, but it also allows you to add custom envirronment vars
note: the first arg is the directory
[example]
bash.PipeMultiDirEnv([]string{`MyEnvVar=CustomValue`}, []string{"/dir1", "cat", "test.txt"}, []string{"./dir2", "tee", "-a", "./test.txt"})
*/
func PipeMultiDirEnv(env []string, args ...[]string){
if len(args) == 1 {
arg1 := args[0][0]
args1 := args[0][1:]
c1 := exec.Command(arg1, args1...)
c1.Stdout = os.Stdout
}
cmd := []*exec.Cmd{}
dir := args[0][0]
arg0 := args[0][1]
args0 := args[0][2:]
cmd = append(cmd, exec.Command(arg0, args0...))
cmd[0].Stdin = os.Stdin
if dir != "" {
cmd[0].Dir = dir
}
if cmd[0].Env == nil {
cmd[0].Env = os.Environ()
}
if env != nil {
cmd[0].Env = append(cmd[0].Env, env...)
}
for i := 1; i < len(args); i++ {
dir = args[i][0]
arg0 = args[i][1]
args0 = args[i][2:]
cmd = append(cmd, exec.Command(arg0, args0...))
pr, pw := io.Pipe()
cmd[i-1].Stdout = pw
cmd[i].Stdin = pr
if dir != "" {
cmd[i].Dir = dir
}
if cmd[i].Env == nil {
cmd[i].Env = os.Environ()
}
if env != nil {
cmd[i].Env = append(cmd[i].Env, env...)
}
cmd[i-1].Start()
go func(i int){
defer pw.Close()
cmd[i-1].Wait()
}(i)
}
cmd[len(cmd)-1].Stdout = os.Stdout
cmd[len(cmd)-1].Start()
cmd[len(cmd)-1].Wait()
}