/
batchcmd.go
executable file
·286 lines (271 loc) · 6.95 KB
/
batchcmd.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
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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"time"
loghelper "github.com/hyper0x/batchcmd/helper/log"
)
const (
separator = "------------------------------------------------------------"
)
// logMap represents the log dictionary.
var logMap = loghelper.NewMap()
// appendLog is used to append the log.
func appendLog(path string, level loghelper.Level, content string) {
if !verboseLog && level < loghelper.LEVEL_INFO {
return
}
oneLog := loghelper.NewOne(level, content)
logMap.Append(path, oneLog)
}
var (
command string
parentDirs string
depth int
isMock bool
timeout int64
verboseLog bool
)
func init() {
flag.StringVar(&command, "c", "",
"The command you want to execute")
flag.StringVar(&parentDirs, "p", "",
"The parent path of target directory(the command will be executed there). \n"+
"Note that multiple path needs to separated by commas ','.")
flag.IntVar(&depth, "d", 1,
"The max search depth for the target directory in parent path(s). ")
flag.BoolVar(&isMock, "m", false,
"Only mock. (the command will not be executed)")
flag.Int64Var(&timeout, "t", 30,
"The seconds for timeout of per command.")
flag.BoolVar(&verboseLog, "v", false,
"Print the verbose logs.")
if verboseLog {
log.SetFlags(log.LstdFlags)
} else {
log.SetFlags(0)
}
}
// findAllSubDirs find all of sub dirs of specified path base on depth-first.
func findAllSubDirs(filePath string, isParentDir bool, depth int, pathCh chan<- string) error {
if depth < 0 {
return nil
}
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("open file: %s", err)
}
fileInfo, err := file.Stat()
if err != nil {
return fmt.Errorf("get file stat: %s", err)
}
if !fileInfo.IsDir() {
return nil
}
subPaths, err := file.Readdirnames(-1)
if err != nil {
return fmt.Errorf("read subdir name: %s", err)
}
if len(subPaths) == 0 {
one := loghelper.NewOne(loghelper.LEVEL_WARN,
fmt.Sprintf("ignore empty dir '%s'.", filePath))
log.Println(one)
return nil
}
if !isParentDir {
pathCh <- filePath
}
if depth == 0 {
return nil
}
newDepth := depth - 1
for _, subPath := range subPaths {
if strings.HasPrefix(subPath, ".") {
continue
}
absSubPath := filepath.Join(filePath, subPath)
err := findAllSubDirs(absSubPath, false, newDepth, pathCh)
if err != nil {
return err
}
}
return nil
}
// findAllTargetDirs check all of parent dirs, and find their sub dirs concurrently.
func findAllTargetDirs(parentDirs []string, depth int, dirCh chan<- string) {
var absParentDirs []string
for _, parentDir := range parentDirs {
one := loghelper.NewOne(loghelper.LEVEL_INFO,
fmt.Sprintf("check parent dir '%s'.", parentDir))
log.Println(one)
if filepath.IsAbs(parentDir) {
absParentDirs = append(absParentDirs, parentDir)
} else {
absParentDir, err := filepath.Abs(parentDir)
if err != nil {
one := loghelper.NewOne(loghelper.LEVEL_ERROR,
fmt.Sprintf("abs parent dir '%s': %s", parentDir, err))
log.Println(one)
continue
}
absParentDirs = append(absParentDirs, absParentDir)
}
}
var wg sync.WaitGroup
wg.Add(len(absParentDirs))
for _, absParentDir := range absParentDirs {
go func(dir string) {
defer func() {
wg.Done()
}()
err := findAllSubDirs(dir, true, depth, dirCh)
if err != nil {
appendLog(dir,
loghelper.LEVEL_ERROR,
fmt.Sprintf("find all subdirs: %s", err))
}
}(absParentDir)
}
go func() {
wg.Wait()
close(dirCh)
}()
}
func executeCommand(
targetDir string,
command string,
ctx context.Context,
cancel context.CancelFunc) {
appendLog(targetDir, loghelper.LEVEL_DEBUG, "entry into target dir.")
err := os.Chdir(targetDir)
if err != nil {
appendLog(targetDir,
loghelper.LEVEL_ERROR,
fmt.Sprintf("change dir: %s", err))
return
}
cmdWithArgs := strings.Split(command, " ")
var cmd *exec.Cmd
cmdLength := len(cmdWithArgs)
realCmd := cmdWithArgs[0]
args := cmdWithArgs[1:cmdLength]
appendLog(targetDir, loghelper.LEVEL_DEBUG,
fmt.Sprintf("execute command '%s'.", command))
if cmdLength > 1 {
cmd = exec.CommandContext(ctx, realCmd, args...)
} else {
cmd = exec.CommandContext(ctx, realCmd)
}
result, err := cmd.CombinedOutput()
if err != nil {
appendLog(targetDir, loghelper.LEVEL_ERROR,
fmt.Sprintf("run command '%s': %s", command, err))
appendLog(targetDir, loghelper.LEVEL_ERROR,
fmt.Sprintf("prompt: \n%s", string(result)))
return
}
appendLog(targetDir, loghelper.LEVEL_INFO,
fmt.Sprintf("output: \n%s", string(result)))
}
func main() {
flag.Parse()
if isMock {
log.Println("Starting... (in mock environment)")
} else {
log.Println("Starting... (in formal environment)")
}
if len(command) == 0 {
log.Println("The argument '-c' is NOT specified!")
return
}
var allParentDirs []string
if len(parentDirs) > 0 {
allParentDirs = strings.Split(parentDirs, ",")
} else {
defaultParentDir, err := os.Getwd()
if err != nil {
log.Println("Work Dir Getting Error:", err)
return
}
allParentDirs = []string{defaultParentDir}
}
paraDesc := "Parameters: " +
"\n Command: %s" +
"\n Parent Dirs: %s" +
"\n Depth: %d" +
"\n Mock: %v" +
"\n Timeout(per command): %ds" +
"\n"
log.Printf(paraDesc,
command, allParentDirs, depth, isMock, timeout)
log.Println("")
dirCh := make(chan string, 100*len(allParentDirs))
findAllTargetDirs(allParentDirs, depth, dirCh)
if isMock {
var targetDirs []string
for targetDir := range dirCh {
targetDirs = append(targetDirs, targetDir)
}
log.Println(separator)
sort.Strings(targetDirs)
log.Printf("Target dirs(%d): \n", len(targetDirs))
for i, targetDir := range targetDirs {
log.Printf(" %d. %s\n", i+1, targetDir)
}
log.Println(separator)
log.Println("The command(s) execution has been ignored in mock mode.")
return
}
workFunc := func(wg *sync.WaitGroup) {
defer wg.Done()
timeoutDuration := time.Duration(timeout) * time.Second
for targetDir := range dirCh {
ctx, cancel := context.WithTimeout(
context.Background(), timeoutDuration)
executeCommand(targetDir, command, ctx, cancel)
}
}
pNum := runtime.GOMAXPROCS(-1)
var wg sync.WaitGroup
wg.Add(pNum)
for i := 0; i < pNum; i++ {
go workFunc(&wg)
}
wg.Wait()
var summaries []string
logMap.Range(func(key string, list loghelper.List) bool {
log.Println(separator)
log.Printf("Target Dir: %s\n", key)
errorCount := 0
for index, one := range list.GetAll() {
log.Printf(" %d. %s\n", index+1, one.String())
if one.Level() >= loghelper.LEVEL_ERROR {
errorCount++
}
}
var summary string
if errorCount > 0 {
summary = fmt.Sprintf("%s: failure(%d).", key, errorCount)
} else {
summary = fmt.Sprintf("%s: success.", key)
}
summaries = append(summaries, summary)
return true
})
log.Println(separator)
log.Println("Summary: ")
for i, s := range summaries {
log.Printf(" %d. %s\n", i+1, s)
}
log.Println(separator)
log.Println("The command(s) execution has been finished.")
}