-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (123 loc) · 3.42 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
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
package main
import (
"dmfcta/action"
"dmfcta/apputils"
"fmt"
"github.com/fsnotify/fsnotify"
"log"
"os"
"os/exec"
"regexp"
"strconv"
"time"
)
func main() {
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// User define
myAction := action.New(watcher)
// Add job
actionMap := map[string]string{}
criteriaMap := map[string]string{}
shellMap := map[string]string{}
timeoutMap := map[string]string{}
actionKey := apputils.GetEnv("DMFCTA_ACTION_KEY", "ACTION")
criteriaKey := apputils.GetEnv("DMFCTA_CRITERIA_KEY", "CRITERIA")
shellKey := apputils.GetEnv("DMFCTA_SHELL_KEY", "SHELL")
timeoutKey := apputils.GetEnv("DMFCTA_TIMEOUT_KEY", "TIMEOUT")
pattern := fmt.Sprintf(`(%s|%s|%s|%s)_(\d+)=(.+)`, actionKey, criteriaKey, shellKey, timeoutKey)
rg, _ := regexp.Compile(pattern)
for _, kv := range os.Environ() {
res := rg.FindAllStringSubmatch(kv, -1)
for i := range res {
//like Java: match.group(1), match.group(2), etc
//fmt.Printf("key: %s, number: %s, value: %s\n", res[i][1], res[i][2], res[i][3])
switch res[i][1] {
case actionKey:
actionMap[fmt.Sprintf("number_%s", res[i][2])] = res[i][3]
case criteriaKey:
criteriaMap[fmt.Sprintf("number_%s", res[i][2])] = res[i][3]
case shellKey:
shellMap[fmt.Sprintf("number_%s", res[i][2])] = res[i][3]
case timeoutKey:
timeoutMap[fmt.Sprintf("number_%s", res[i][2])] = res[i][3]
}
}
}
// Merge actions
var jobs []action.JobInput
for k, a := range actionMap {
jobs = append(jobs, action.JobInput{
CMD: a,
SHELLTYPE: func() string {
finalShell := ""
if shell, ok := shellMap[k]; ok {
finalShell = shell
}
if finalShell == "" {
for _, s := range []string{action.ShShell, action.BashShell, action.ZshShell} {
if _, err := exec.LookPath(s); err == nil {
finalShell = s
break
}
}
}
// Set default shell
// Loop to check the existent shell, take one
if _, err := exec.LookPath(finalShell); err != nil {
return ""
}
return finalShell
}(),
Timeout: func() time.Duration {
if timeout, ok := timeoutMap[k]; ok {
// timeout is like: 3600, in second
intTimeout, err := strconv.ParseInt(timeout, 10, 0)
if err == nil {
return time.Duration(intTimeout) * time.Second
}
}
// Set default timeout
return time.Minute
}(),
Endpoints: func() []string {
if criteria, ok := criteriaMap[k]; ok {
return []string{criteria}
}
return []string{}
}(),
})
}
// Check for jobs with the same action
// Create a map to store unique jobs
uniqueJobs := make(map[string]action.JobInput)
// Filter and retrieve unique jobs
for _, job := range jobs {
key := fmt.Sprintf("%s-%s-%s", job.CMD, job.SHELLTYPE, job.Timeout)
// Add the job to the map if it doesn't exist
if _, ok := uniqueJobs[key]; !ok {
uniqueJobs[key] = job
} else {
job.Endpoints = append(uniqueJobs[key].Endpoints, job.Endpoints...)
uniqueJobs[key] = job
}
}
for _, job := range uniqueJobs {
myAction.Add(job)
log.Printf("Action: %s", job.CMD)
log.Printf("Shell: %s", job.SHELLTYPE)
log.Printf("Timeout: %s", job.Timeout)
log.Println("Endpoints:")
for _, endpoint := range job.Endpoints {
log.Printf("- %s", endpoint)
}
log.Println("-------------------------------------")
}
myAction.ListenToDoSignal()
// Block main goroutine forever.
<-make(chan struct{})
}