-
Notifications
You must be signed in to change notification settings - Fork 5
/
shell.go
175 lines (135 loc) · 3.96 KB
/
shell.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
package shell
import (
"context"
"fmt"
"github.com/esrrhs/gohome/loggo"
"os/exec"
"path/filepath"
"time"
)
func Run(script string, silent bool, param ...string) (string, error) {
script = filepath.Clean(script)
script = filepath.ToSlash(script)
if !silent {
loggo.Info("shell Run start %v %v ", script, fmt.Sprint(param))
}
var tmpparam []string
tmpparam = append(tmpparam, script)
tmpparam = append(tmpparam, param...)
begin := time.Now()
cmd := exec.Command("sh", tmpparam...)
out, err := cmd.CombinedOutput()
outstr := string(out)
if err != nil {
loggo.Warn("shell Run fail %v %v", cmd.Args, outstr)
return "", err
}
if !silent {
loggo.Info("shell Run ok %v %v", cmd.Args, time.Now().Sub(begin))
loggo.Info("%v", outstr)
}
return outstr, nil
}
func RunTimeout(script string, silent bool, timeout int, param ...string) (string, error) {
script = filepath.Clean(script)
script = filepath.ToSlash(script)
d := time.Now().Add(time.Duration(timeout) * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel() // releases resources if slowOperation completes before timeout elapses
if !silent {
loggo.Info("shell Run start %v %v %v ", script, timeout, fmt.Sprint(param))
}
var tmpparam []string
tmpparam = append(tmpparam, script)
tmpparam = append(tmpparam, param...)
begin := time.Now()
cmd := exec.CommandContext(ctx, "sh", tmpparam...)
out, err := cmd.CombinedOutput()
outstr := string(out)
if err != nil {
loggo.Warn("shell Run fail %v %v %v", cmd.Args, outstr, ctx.Err())
return "", err
}
if !silent {
loggo.Info("shell Run ok %v %v", cmd.Args, time.Now().Sub(begin))
loggo.Info("%v", outstr)
}
return outstr, nil
}
func RunCommand(command string, silent bool) (string, error) {
if !silent {
loggo.Info("shell RunCommand start %v ", command)
}
begin := time.Now()
cmd := exec.Command("bash", "-c", command)
out, err := cmd.CombinedOutput()
outstr := string(out)
if err != nil {
loggo.Warn("shell RunCommand fail %v %v %v", cmd.Args, outstr, err)
return "", err
}
if !silent {
loggo.Info("shell RunCommand ok %v %v", cmd.Args, time.Now().Sub(begin))
loggo.Info("%v", outstr)
}
return outstr, nil
}
func RunExe(exe string, silent bool, param ...string) (string, error) {
exe = filepath.Clean(exe)
exe = filepath.ToSlash(exe)
if !silent {
loggo.Info("shell Run start %v %v ", exe, fmt.Sprint(param))
}
begin := time.Now()
cmd := exec.Command(exe, param...)
out, err := cmd.CombinedOutput()
outstr := string(out)
if err != nil {
loggo.Warn("shell Run fail %v %v %v", cmd.Args, outstr, err)
return "", err
}
if !silent {
loggo.Info("shell Run ok %v %v", cmd.Args, time.Now().Sub(begin))
loggo.Info("%v", outstr)
}
return outstr, nil
}
func RunExeTimeout(exe string, silent bool, timeout int, param ...string) (string, error) {
d := time.Now().Add(time.Duration(timeout) * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel() // releases resources if slowOperation completes before timeout elapses
exe = filepath.Clean(exe)
exe = filepath.ToSlash(exe)
if !silent {
loggo.Info("shell Run start %v %v ", exe, fmt.Sprint(param))
}
begin := time.Now()
cmd := exec.CommandContext(ctx, exe, param...)
out, err := cmd.CombinedOutput()
outstr := string(out)
if err != nil {
loggo.Warn("shell Run fail %v %v %v", cmd.Args, outstr, err)
return "", err
}
if !silent {
loggo.Info("shell Run ok %v %v", cmd.Args, time.Now().Sub(begin))
loggo.Info("%v", outstr)
}
return outstr, nil
}
func RunExeRaw(exe string, silent bool, param ...string) string {
exe = filepath.Clean(exe)
exe = filepath.ToSlash(exe)
if !silent {
loggo.Info("shell Run start %v %v ", exe, fmt.Sprint(param))
}
begin := time.Now()
cmd := exec.Command(exe, param...)
out, _ := cmd.CombinedOutput()
outstr := string(out)
if !silent {
loggo.Info("shell Run ok %v %v", cmd.Args, time.Now().Sub(begin))
loggo.Info("%v", outstr)
}
return outstr
}