-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (63 loc) · 1.7 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
var (
commandsFile = flag.String("commands-file", "", "commands file path, which contains a list of command to run")
isClearScreen = flag.Bool("clear-screen", false, "clear screen between each command")
waitDuration = flag.Duration("wait-duration", 5*time.Second, "wait duration between each command")
)
func main() {
flag.Parse()
if *commandsFile == "" {
fmt.Println("commands-file is required")
fmt.Println("Example: go run main.go --commands-file=commands.txt")
return
}
commandsFileBytes, err := os.ReadFile(*commandsFile)
if err != nil {
fmt.Printf("os: failed to read file: %s\n", err)
return
}
rawCommands := strings.Split(strings.TrimSpace(string(commandsFileBytes)), "\n")
commands := make([][2]string, 0, len(rawCommands))
for _, rawCommand := range rawCommands {
rawCommand = strings.TrimSpace(rawCommand)
if rawCommand == "" {
continue
}
// Command expect to be in the format of "command arg1 arg2 ..."
before, after, found := strings.Cut(rawCommand, " ")
if !found {
continue
}
commands = append(commands, [2]string{before, after})
}
// For is forever
i := 0
for {
i = i % len(commands)
before, after := commands[i][0], commands[i][1]
i += 1
cmd := exec.Command(before, strings.Split(after, " ")...)
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Printf("exec: failed to run command %s %s: %s\n", before, after, err)
break
}
time.Sleep(*waitDuration)
if *isClearScreen {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Printf("exec: failed to run command clear: %s\n", err)
break
}
}
}
}