forked from gaulliath/operative-framework
/
module.go
244 lines (216 loc) · 5.59 KB
/
module.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
package session
import (
"errors"
"github.com/graniet/go-pretty/table"
"github.com/joho/godotenv"
"os"
"os/user"
"strconv"
"strings"
)
const (
INT = 1
STRING = 2
BOOL = 3
FLOAT = 4
)
type Module interface {
Start()
Name() string
Author() string
Description() string
GetType() []string
ListArguments()
GetExport() []OpfResults
SetExport(result OpfResults)
GetResults() []string
GetInformation() ModuleInformation
CheckRequired() bool
SetParameter(name string, value string) (bool, error)
GetParameter(name string) (Param, error)
GetAllParameters() []Param
WithProgram(name string) bool
GetExternal() []string
CreateNewParam(name string, description string, value string, isRequired bool, paramType int)
}
type ModuleEvent struct {
ModuleName string
Results interface{}
}
type Param struct {
Name string `json:"name"`
Description string `json:"description"`
Value string `json:"value"`
IsRequired bool `json:"is_required"`
ParamType int `json:"param_type"`
}
type SessionModule struct {
Module
Export []OpfResults
Parameters []Param `json:"parameters"`
History []string `json:"history"`
External []string `json:"external"`
Results []string
}
type ModuleInformation struct {
Name string `json:"name"`
Description string `json:"description"`
Author string `json:"author"`
Type []string `json:"type"`
Parameters []Param `json:"parameters"`
}
func (s *Session) SearchModule(name string) (Module, error) {
for _, module := range s.Modules {
if module.Name() == name {
return module, nil
}
}
return nil, errors.New("error: This module not found")
}
func (module *SessionModule) GetParameter(name string) (Param, error) {
for _, param := range module.Parameters {
if strings.ToUpper(param.Name) == strings.ToUpper(name) {
return param, nil
}
}
return Param{}, errors.New("parameter not found")
}
func (module *SessionModule) SetParameter(name string, value string) (bool, error) {
for k, param := range module.Parameters {
if strings.ToUpper(param.Name) == strings.ToUpper(name) {
module.Parameters[k].Value = value
return true, nil
}
}
return false, errors.New("argument not found")
}
func (module *SessionModule) CheckRequired() bool {
for _, param := range module.Parameters {
if param.IsRequired == true {
switch param.ParamType {
case STRING:
if param.Value == "" {
return false
}
case INT:
value, _ := strconv.Atoi(param.Value)
if value == 0 {
return false
}
case BOOL:
value := strings.TrimSpace(param.Value)
if value == "" {
return false
}
}
}
}
return true
}
func (module *SessionModule) CreateNewParam(name string, description string, value string, isRequired bool, paramType int) {
newParam := Param{
Name: strings.ToUpper(name),
Value: value,
Description: description,
IsRequired: isRequired,
ParamType: paramType,
}
module.Parameters = append(module.Parameters, newParam)
}
func (module *SessionModule) WithProgram(name string) bool {
module.External = append(module.External, name)
return true
}
func (module *SessionModule) ListArguments() {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetAllowedColumnLengths([]int{40, 40})
t.AppendHeader(table.Row{"argument", "description", "value", "required", "type"})
if len(module.Parameters) > 0 {
for _, argument := range module.Parameters {
argumentType := ""
argumentRequired := ""
if argument.ParamType == STRING {
argumentType = "STRING"
} else if argument.ParamType == INT {
argumentType = "INTEGER"
} else if argument.ParamType == BOOL {
argumentType = "BOOLEAN"
}
if argument.IsRequired == true {
argumentRequired = "YES"
} else {
argumentRequired = "NO"
}
if argument.Value == "" {
argument.Value = "NO DEFAULT"
}
t.AppendRow([]interface{}{argument.Name, argument.Description, argument.Value, argumentRequired, argumentType})
}
} else {
t.AppendRow([]interface{}{"No argument."})
}
t.Render()
}
func (module *SessionModule) SetExport(result OpfResults) {
module.Export = append(module.Export, result)
}
func (module *SessionModule) GetExport() []OpfResults {
return module.Export
}
func (module *SessionModule) GetAllParameters() []Param {
return module.Parameters
}
func (module *SessionModule) GetResults() []string {
return module.Results
}
func (module *SessionModule) GetExternal() []string {
return module.External
}
func (s *Session) ListModules() {
t := s.Stream.GenerateTable()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{
"NAME",
"DESCRIPTION",
"TYPE",
})
for _, module := range s.Modules {
t.AppendRow(table.Row{
module.Name(),
module.Description(),
strings.Join(module.GetType(), ","),
})
}
s.Stream.Render(t)
}
func (s *Session) ParseModuleConfig() {
u, _ := user.Current()
for _, module := range s.Modules {
fileName := u.HomeDir + "/.opf/external/modules/" + module.Name() + ".conf"
if _, err := os.Stat(fileName); os.IsNotExist(err) {
continue
}
configuration, err := godotenv.Read(fileName)
if err == nil {
s.Config.Modules[module.Name()] = configuration
}
}
}
func (s *Session) LoadModuleConfiguration(module string) (map[string]string, error) {
if _, ok := s.Config.Modules[module]; !ok {
return nil, errors.New("Configuration not found")
}
return s.Config.Modules[module], nil
}
func (s *Session) WithFilter(module Module) bool {
useFilter := false
filter, err := module.GetParameter("FILTER")
if err == nil {
_, err := s.SearchFilter(filter.Value)
if err == nil {
useFilter = true
}
}
return useFilter
}