forked from gaulliath/operative-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_import.go
112 lines (93 loc) · 2.6 KB
/
session_import.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
package session_import
import (
"github.com/Oni-kuki/operative-framework/session"
"strconv"
"strings"
)
type ImportModule struct {
session.SessionModule
sess *session.Session `json:"-"`
}
func PushModuleImport(s *session.Session) *ImportModule {
mod := ImportModule{
sess: s,
}
mod.CreateNewParam("TARGET", "Source file path (csv)", "", true, session.STRING)
mod.CreateNewParam("DELIMITER", "CSV file separator", ";", true, session.STRING)
mod.CreateNewParam("PRIMARY", "Set a key of primary element", "0", true, session.STRING)
mod.CreateNewParam("LINKING", "Key of linked value. e.g: 0,5,3", "0", true, session.STRING)
mod.CreateNewParam("VERBOSE", "Display imported line output", "false", true, session.STRING)
return &mod
}
func (module *ImportModule) Name() string {
return "import.csv"
}
func (module *ImportModule) Description() string {
return "Import data from CSV"
}
func (module *ImportModule) Author() string {
return "Tristan Granier"
}
func (module *ImportModule) GetType() []string {
return []string{
session.T_TARGET_IMPORT,
}
}
func (module *ImportModule) GetInformation() session.ModuleInformation {
information := session.ModuleInformation{
Name: module.Name(),
Description: module.Description(),
Author: module.Author(),
Type: module.GetType(),
Parameters: module.Parameters,
}
return information
}
func (module *ImportModule) Start() {
targetId, err := module.GetParameter("TARGET")
if err != nil {
module.sess.Stream.Error(err.Error())
return
}
target, err := module.sess.GetTarget(targetId.Value)
if err != nil {
module.sess.Stream.Error(err.Error())
return
}
delimiter, err := module.GetParameter("DELIMITER")
if err != nil {
module.sess.Stream.Error(err.Error())
return
}
primary, err := module.GetParameter("PRIMARY")
if err != nil {
module.sess.Stream.Error(err.Error())
return
}
verbose, err := module.GetParameter("VERBOSE")
if err != nil {
module.sess.Stream.Error(err.Error())
return
}
main, err := strconv.Atoi(primary.Value)
if err != nil {
module.sess.Stream.Error("Primary parameter value is incorrect please use 'INTEGER'")
return
}
linking, err := module.GetParameter("LINKING")
if err != nil {
module.sess.Stream.Error(err.Error())
return
}
links := []int{}
values := strings.Split(linking.Value, ",")
for _, val := range values {
nb, err := strconv.Atoi(val)
if err != nil {
continue
}
links = append(links, nb)
}
module.sess.Stream.Backgound("Import as started")
go module.sess.ImportFromCsv(target.GetName(), delimiter.Value, main, module.sess.StringToBoolean(verbose.Value), links)
}