-
Notifications
You must be signed in to change notification settings - Fork 217
/
generate.go
175 lines (151 loc) · 4.63 KB
/
generate.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
package eventing
import (
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/spaceuptech/space-cloud/space-cli/cmd/model"
"github.com/spaceuptech/space-cloud/space-cli/cmd/utils/input"
)
func generateEventingRule() (*model.SpecObject, error) {
project := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Enter Project ID"}, &project); err != nil {
return nil, err
}
ruleType := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Enter rule type"}, &ruleType); err != nil {
return nil, err
}
v := &model.SpecObject{
API: "/v1/config/projects/{project}/eventing/rules/{id}",
Type: "eventing-rule",
Meta: map[string]string{
"id": ruleType,
"project": project,
},
Spec: map[string]interface{}{
"rule": "allow",
},
}
return v, nil
}
func generateEventingSchema() (*model.SpecObject, error) {
project := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Enter Project ID"}, &project); err != nil {
return nil, err
}
ruleType := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Enter rule type"}, &ruleType); err != nil {
return nil, err
}
schema := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Enter Schema"}, &schema); err != nil {
return nil, err
}
v := &model.SpecObject{
API: "/v1/config/projects/{project}/eventing/schema/{id}",
Type: "eventing-schema",
Meta: map[string]string{
"id": ruleType,
"project": project,
},
Spec: map[string]interface{}{
"schema": schema,
},
}
return v, nil
}
func generateEventingConfig() (*model.SpecObject, error) {
project := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Enter project"}, &project); err != nil {
return nil, err
}
dbAlias := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Enter DB Alias"}, &dbAlias); err != nil {
return nil, err
}
v := &model.SpecObject{
API: "/v1/config/projects/{project}/eventing/config/{id}",
Type: "eventing-config",
Meta: map[string]string{
"project": project,
"id": "eventing-config",
},
Spec: map[string]interface{}{
"dbAlias": dbAlias,
"enabled": true,
},
}
return v, nil
}
func generateEventingTrigger() (*model.SpecObject, error) {
project := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Enter project"}, &project); err != nil {
return nil, err
}
triggerName := ""
if err := input.Survey.AskOne(&survey.Input{Message: "trigger name"}, &triggerName); err != nil {
return nil, err
}
source := ""
if err := input.Survey.AskOne(&survey.Select{Message: "Select source ", Options: []string{"Database", "File Storage", "Custom"}}, &source); err != nil {
return nil, err
}
operationType := ""
var dbAlias string
col := ""
options := map[string]interface{}{}
switch source {
case "Database":
if err := input.Survey.AskOne(&survey.Select{Message: "Select trigger operation", Options: []string{"DB_INSERT", "DB_UPDATE", "DB_DELETE"}}, &operationType); err != nil {
return nil, err
}
if err := input.Survey.AskOne(&survey.Input{Message: "Enter Database Alias "}, &dbAlias); err != nil {
return nil, err
}
if err := input.Survey.AskOne(&survey.Input{Message: "Enter collection/table name"}, &col); err != nil {
return nil, err
}
options = map[string]interface{}{"db": dbAlias, "col": col}
case "File Storage":
if err := input.Survey.AskOne(&survey.Select{Message: "Select trigger operation", Options: []string{"FILE_CREATE", "FILE_DELETE"}}, &operationType); err != nil {
return nil, err
}
case "Custom":
if err := input.Survey.AskOne(&survey.Input{Message: "Enter trigger type"}, &operationType); err != nil {
return nil, err
}
}
url := ""
if err := input.Survey.AskOne(&survey.Input{Message: "webhook url"}, &url); err != nil {
return nil, err
}
wantAdvancedSettings := ""
if err := input.Survey.AskOne(&survey.Input{Message: "Do you want advanced settings? (Y / n) ?", Default: "n"}, &wantAdvancedSettings); err != nil {
return nil, err
}
retries := 3
timeout := 5000
if strings.ToLower(wantAdvancedSettings) == "y" {
if err := input.Survey.AskOne(&survey.Input{Message: "Retries count", Default: "3"}, &retries); err != nil {
return nil, err
}
if err := input.Survey.AskOne(&survey.Input{Message: "Enter Timeout", Default: "5000"}, &timeout); err != nil {
return nil, err
}
}
v := &model.SpecObject{
API: "/v1/config/projects/{project}/eventing/triggers/{id}",
Type: "eventing-triggers",
Meta: map[string]string{
"project": project,
"id": triggerName,
},
Spec: map[string]interface{}{
"type": operationType,
"url": url,
"retries": retries,
"timeout": timeout,
"options": options,
},
}
return v, nil
}