-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
183 lines (165 loc) · 5.76 KB
/
loader.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
// Copyright © 2018 Antoine GIRARD <antoine.girard@sapk.fr>
package loader
import (
"github.com/c-bata/go-prompt"
"github.com/rs/zerolog/log"
"github.com/sapk/genesys-tools/tool/format"
"github.com/sapk/go-genesys/api/client"
)
var data map[string][]map[string]interface{}
func init() {
data = make(map[string][]map[string]interface{})
}
func ListObject(c *client.Client, t string) []map[string]interface{} {
log.Debug().Interface("type", t).Msgf("ListObject")
if l, ok := data[t]; ok {
log.Debug().Interface("list", l).Msgf("ListObject in cache")
return l //don't get list allready done
}
var list []map[string]interface{}
c.ListObject(t, &list)
log.Debug().Interface("list", list).Msgf("ListObject fetched")
if t == "CfgTenant" {
list = append(list, map[string]interface{}{"name": "Environment", "dbid": "1"})
}
data[t] = list
return list
}
type Loader struct {
FormatCreate func(*client.Client, map[string]interface{}, map[string]string) map[string]interface{}
FormatUpdate func(*client.Client, map[string]interface{}, map[string]interface{}, map[string]string) map[string]interface{}
}
var LoaderList = map[string]Loader{
"default": Loader{
FormatCreate: func(c *client.Client, obj map[string]interface{}, defaults map[string]string) map[string]interface{} {
log.Debug().Interface("in", obj).Msgf("default.FormatCreate")
//cleanObj(obj, "dbid", "hostdbid", "appprototypedbid") //TODO find matching prototype for app //TODO ask for password
cleanObj(obj, "dbid")
if tenant, exist := obj["tenantdbid"]; exist {
obj["tenantdbid"] = searchFor(c, "CfgTenant", tenant.(string), defaults)
}
if folder, exist := obj["folderid"]; exist {
obj["folderid"] = searchFor(c, "CfgFolder", folder.(string), defaults)
}
/*
if userproperties, exist := obj["userproperties"]; exist {
obj["userproperties"] = cleanEmptyAnnexes(userproperties.(map[string]interface{}))
}
*/
log.Debug().Interface("out", obj).Msgf("default.FormatCreate")
return obj
},
FormatUpdate: func(c *client.Client, src, obj map[string]interface{}, defaults map[string]string) map[string]interface{} {
//TODO find matching prototype for app //TODO ask for password
obj["dbid"] = src["dbid"]
//TODO use default of src
if tenant, exist := src["tenantdbid"]; exist {
obj["tenantdbid"] = searchFor(c, "CfgTenant", tenant.(string), defaults)
}
if folder, exist := src["folderid"]; exist {
obj["folderid"] = searchFor(c, "CfgFolder", folder.(string), defaults)
}
/*
if userproperties, exist := obj["userproperties"]; exist {
obj["userproperties"] = cleanEmptyAnnexes(userproperties.(map[string]interface{}))
}
*/
return obj
},
},
}
/*Some key failed but this is not the solution you are looking for
func cleanEmptyAnnexes(o map[string]interface{}) interface{} {
var obj object.Userproperties
err := mapstructure.Decode(o, &obj)
if err != nil {
log.Warnf("Fail to convert to Userproperties -> Skipping cleaning")
return o
}
for id, val := range obj.Property {
if val.Key == "" {
obj.Property = append(obj.Property[:id], obj.Property[id+1:]...)
}
}
return obj
}
*/
func searchFor(c *client.Client, t string, id string, defaults map[string]string) string {
//TODO search for seam id
//TODO history
//Use default value by default
if def, ok := defaults[t]; ok {
log.Debug().Interface("def", def).Interface("type", t).Msgf("Using default value")
return def
}
list := ListObject(c, t)
log.Debug().Interface("list", list).Interface("type", t).Msgf("Fetched list")
log.Info().Msgf("Please choose a %s :", t)
val := prompt.Input("> ", func(d prompt.Document) []prompt.Suggest {
//log.Print(d.Text)
// if d.Text == "" {
// d.Text = "TEST"
// }
//log.Interface("list", list).Info("Fetched list")
//TODO put id corresponding obj if any first
s := make([]prompt.Suggest, len(list))
for i, o := range list {
//log.Interface("obj", o).Info("Add to list")
s[i] = prompt.Suggest{o["dbid"].(string), format.GetFileName(o)}
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
})
log.Info().Msgf("You selected " + val)
if val == "" {
switch t {
case "CfgTenant":
return "1"
}
}
return val
}
func cleanObj(obj map[string]interface{}, ids ...string) {
for _, id := range ids {
_, ok := obj[id]
if ok {
delete(obj, id)
}
}
}
//TODO match for each format like format
func FilterBy(obj map[string]interface{}, data []map[string]interface{}, cmp func(map[string]interface{}, map[string]interface{}) bool) []map[string]interface{} {
ret := make([]map[string]interface{}, 0)
for _, o := range data {
if cmp(obj, o) {
log.Info().Interface("obj", o).Msg("Matched object")
ret = append(ret, o) //TODO best allocate
}
}
return ret
}
func MatchId(src, dst map[string]interface{}) bool {
log.Debug().Str("cmp", "dbid").Interface("src", src["dbid"]).Interface("dst", dst["dbid"]).Interface("result", src["dbid"] == dst["dbid"]).Msg("Matching dbid")
return src["dbid"] == dst["dbid"]
}
func MatchByEl(src, dst map[string]interface{}, el string) bool {
log.Debug().Str("cmp", el).Interface("src", src[el]).Interface("dst", dst[el]).Interface("result", src[el] == dst[el]).Msg("Matching name")
return src[el] == dst[el]
}
func MatchName(src, dst map[string]interface{}) bool {
if src["name"] != nil && dst["name"] != nil {
return MatchByEl(src, dst, "name")
}
if src["username"] != nil && dst["username"] != nil {
return MatchByEl(src, dst, "username")
}
if src["number"] != nil && dst["number"] != nil {
return MatchByEl(src, dst, "number")
}
if src["logincode"] != nil && dst["logincode"] != nil {
return MatchByEl(src, dst, "logincode")
}
return false
}
func MatchIdName(src, dst map[string]interface{}) bool { //TODO Manage Person (username)
return MatchName(src, dst) && MatchId(src, dst)
}