-
Notifications
You must be signed in to change notification settings - Fork 1
/
exec.go
82 lines (70 loc) · 1.86 KB
/
exec.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
package commander
import (
"bytes"
"errors"
"os"
"os/exec"
"regexp"
"strings"
)
var Exec Execute = newExec()
type Execute interface {
ExecPipeCommand(name string, arg ...string) (string, string, error)
ExecStdCommand(name string, arg ...string) (string, error)
ExecPipeStatementCommand(statement string) (string, string, error)
ExecStdStatementCommand(statement string) (string, error)
SplitCommandStatement(statement string) (result []string)
}
type _Exec struct {
}
func newExec() *_Exec {
return &_Exec{}
}
func (e _Exec) ExecPipeCommand(name string, arg ...string) (string, string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command(name, arg...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
return stdout.String(), stderr.String(), err
}
if err := cmd.Wait(); err != nil {
return stdout.String(), stderr.String(), err
}
return stdout.String(), stderr.String(), nil
}
func (e _Exec) ExecStdCommand(name string, arg ...string) (string, error) {
stdout, stderr, err := e.ExecPipeCommand(name, arg...)
os.Stdout.WriteString(stdout)
os.Stderr.WriteString(stderr)
if err != nil {
os.Stderr.WriteString(err.Error())
return strings.TrimSpace(stdout + stderr), err
} else if len(stderr) != 0 {
return stdout, errors.New(stderr)
}
return stdout, nil
}
func (e _Exec) ExecPipeStatementCommand(statement string) (string, string, error) {
return e.ExecPipeCommand(
"/bin/bash",
"-c",
statement,
)
}
func (e _Exec) ExecStdStatementCommand(statement string) (string, error) {
return e.ExecStdCommand(
"/bin/bash",
"-c",
statement,
)
}
func (e _Exec) SplitCommandStatement(statement string) (result []string) {
result = regexp.MustCompile(`([^\\]"[\w\s\S]+?[^\\]")|([^\s\\]+)`).
FindAllString(statement, -1)
for i, str := range result {
result[i] = strings.TrimSpace(str)
}
return
}