This repository has been archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (120 loc) · 3.18 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
131
132
133
134
135
136
137
138
139
140
package main
import (
"github.com/JReyLBC/JChecker/command"
"github.com/JReyLBC/JChecker/config"
"github.com/JReyLBC/JChecker/reply"
log "github.com/Sirupsen/logrus"
"encoding/csv"
"fmt"
"io"
"os"
"strings"
"sync"
)
func init() {
config.Execute()
}
func main() {
const thisFunc = "main.main()"
cntxLog := log.WithFields(log.Fields{
"func": thisFunc,
})
cfg := config.GetConfig()
cntxLog.WithFields(log.Fields{
"file": cfg.CommandConfigCSVFile,
})
if file, err := os.Open(cfg.CommandConfigCSVFile); err != nil {
cntxLog.Errorln("could not open command config CSV file")
cntxLog.Fatalln(err)
} else if records, err := readCSVRecs(file); err != nil {
cntxLog.Errorln("could not read command records")
cntxLog.Fatalln(err)
} else if cmds, err := buildCommands(records); err != nil {
cntxLog.Errorln("could not construct commands from records")
cntxLog.Fatal(err)
} else {
var wg sync.WaitGroup
wg.Add(len(cmds))
runCommands(cmds, &wg)
wg.Wait()
}
}
func readCSVRecs(file io.Reader) ([][]string, error) {
const thisFunc = "main.readCSVRecs()"
cntxLog := log.WithFields(log.Fields{
"func": thisFunc,
})
cntxLog.Debugln("setting up CSV reader")
csvReader := csv.NewReader(file)
csvReader.Comment = '#'
csvReader.FieldsPerRecord = command.NUM_FIELDS
csvReader.TrimLeadingSpace = true
cntxLog.Debugln("reading all records")
return csvReader.ReadAll()
}
func buildCommands(records [][]string) ([]command.Commander, error) {
const thisFunc = "main.buildCommands()"
cntxLog := log.WithFields(log.Fields{
"func": thisFunc,
})
cmds := []command.Commander{}
for recNum, record := range records {
record[command.COMMAND_FIELD] = strings.ToLower(record[command.COMMAND_FIELD])
switch record[command.COMMAND_FIELD] {
case command.CHASSIS_ENV_CMD:
cntxLog = cntxLog.WithFields(log.Fields{
"command": command.CHASSIS_ENV_CMD,
})
if cmd, err := command.ChassisEnvCmdFromRecord(record); err != nil {
cntxLog.Debugln("could not create ChassisEnvCmd")
cntxLog.Debugln(err)
return nil, err
} else {
cntxLog.Debugln("appending command")
cmds = append(cmds, cmd)
}
default:
cntxLog = cntxLog.WithFields(log.Fields{
"command": record[command.COMMAND_FIELD],
})
cntxLog.Debugln("unknown command")
return nil, fmt.Errorf("unknown command '%s' in record %d", record[command.COMMAND_FIELD], recNum+1)
}
}
return cmds, nil
}
func runCommands(cmds []command.Commander, wg *sync.WaitGroup) {
for _, cmd := range cmds {
replyChan, errChan := reply.ProcessChassisEnv(cmd.Run())
go func() {
const thisFunc = "main.runCommands.func()"
cntxLog := log.WithFields(log.Fields{
"func": thisFunc,
})
defer wg.Done()
done := false
for !done {
select {
case r, ok := <-replyChan:
if !ok {
cntxLog.Debugln("replyChan closed")
done = true
break
} else if outFile, err := cmd.OutFile(); err != nil {
cntxLog.Errorln(err)
} else if err := r.WriteCSV(outFile); err != nil {
cntxLog.Errorln(err)
}
case e, ok := <-errChan:
if !ok {
cntxLog.Debugln("errChan closed")
done = true
break
} else {
log.Errorln(e)
}
}
}
}()
}
}