-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (229 loc) · 5.9 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
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 main
import (
"github.com/daptin/daptin-cli/src"
daptinClient "github.com/daptin/daptin-go-client"
"github.com/ghodss/yaml"
"github.com/urfave/cli/v2"
"io/ioutil"
"log"
"os"
)
func main() {
configFile, _ := os.UserHomeDir()
daptinCliConfig := src.DaptinCliConfig{}
configFileEnv, ok := os.LookupEnv("DAPTIN_CLI_CONFIG")
if ok {
configFile = configFileEnv
} else {
dirPath := configFile + string(os.PathSeparator) + ".daptin"
if _, err := os.Stat(dirPath); err != nil {
err = os.Mkdir(dirPath, 0644)
}
configFile = dirPath + string(os.PathSeparator) + "config.yaml"
}
if _, err := os.Stat(configFile); err == nil {
configFileBytes, _ := ioutil.ReadFile(configFile)
err = yaml.Unmarshal(configFileBytes, &daptinCliConfig)
} else {
_, _ = os.Create(configFile)
}
for _, config := range daptinCliConfig.Hosts {
if config.Name == daptinCliConfig.CurrentContextName {
daptinCliConfig.Context = config
break
}
}
appController := src.NewApplicationController(daptinCliConfig, configFile)
app := &cli.App{
Before: func(context *cli.Context) error {
if daptinCliConfig.CurrentContextName == "" {
daptinCliConfig.Context.Endpoint = context.String("endpoint")
}
if daptinCliConfig.CurrentContextName != "" {
var sH src.DaptinHostEndpoint
for _, h := range daptinCliConfig.Hosts {
if h.Name == daptinCliConfig.CurrentContextName {
sH = h
break
}
}
if sH.Name != "" {
daptinCliConfig.Context = sH
}
}
var daptinClientInstance daptinClient.DaptinClient
if daptinCliConfig.Context.Token == "" {
daptinClientInstance = daptinClient.NewDaptinClient(daptinCliConfig.Context.Endpoint, false)
} else {
daptinClientInstance = daptinClient.NewDaptinClientWithAuthToken(daptinCliConfig.Context.Endpoint, daptinCliConfig.Context.Token, false)
}
appController.SetDaptinClient(daptinClientInstance)
worlds, err := getAllItems("world", daptinClientInstance)
if err != nil {
return err
}
appController.SetWorlds(worlds)
actions, err := getAllItems("action", daptinClientInstance)
if err != nil {
return err
}
appController.SetActions(actions)
outputRenderer := context.String("output")
switch outputRenderer {
case "table":
appController.SetRenderer(src.NewTableRenderer())
case "json":
appController.SetRenderer(src.NewJsonRenderer())
}
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
DefaultText: "~/.daptin/config.yaml",
EnvVars: []string{"DAPTIN_CLI_CONFIG"},
},
&cli.StringFlag{
Name: "output, o",
Usage: "output format",
DefaultText: "table",
Value: "table",
EnvVars: []string{"DAPTIN_CLI_OUTPUT"},
},
&cli.StringFlag{
Name: "endpoint",
Usage: "endpoint",
DefaultText: "http://localhost:6336",
Value: "http://localhost:6336",
EnvVars: []string{"DAPTIN_ENDPOINT"},
},
&cli.BoolFlag{
Name: "debug, v",
Usage: "Print trace logsf",
},
},
Commands: []*cli.Command{
{
Name: "set-context",
Aliases: []string{"sc"},
Usage: "set the default context by name in config.yaml",
Action: appController.SetContext,
},
{
Name: "signin",
Usage: "sign in",
Flags: []cli.Flag{},
Action: appController.ActionSignIn,
},
{
Name: "signin_with_2fa",
Usage: "Sign in with 2FA",
Flags: []cli.Flag{},
Action: appController.ActionVerifyOtp,
},
{
Name: "signup",
Usage: "sign up",
Flags: []cli.Flag{},
Action: appController.ActionSignUp,
},
{
Name: "describe",
Usage: "show schema",
Subcommands: []*cli.Command{
{
Name: "table",
Action: appController.ActionShowWorldSchema,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "columns",
Usage: "comma separated column names to output",
Required: false,
Value: "",
},
},
},
{
Name: "action",
Action: appController.ActionShowActionSchema,
Flags: []cli.Flag{},
},
},
},
{
Name: "list",
Usage: "list entity",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "filter",
Usage: "filter by keyword",
Required: false,
},
&cli.StringFlag{
Name: "columns",
Usage: "columns to print",
Required: false,
},
&cli.IntFlag{
Name: "pageSize",
Usage: "number of items per page",
Required: false,
Value: 10,
DefaultText: "10",
},
&cli.IntFlag{
Name: "pageNumber",
Usage: "page number",
Required: false,
Value: 0,
DefaultText: "0",
},
},
Action: appController.ActionListEntity,
},
{
Name: "get-by-id",
Usage: "get table row by reference_id",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "reference_id",
Usage: "row reference_id to fetch",
Required: false,
},
&cli.StringFlag{
Name: "columns",
Usage: "columns to print",
Required: false,
},
},
Action: appController.ActionGetById,
},
{
Name: "execute",
Usage: "execute an action",
Flags: []cli.Flag{},
Action: appController.ActionExecute,
},
},
Version: "v0.0.1",
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version", Aliases: []string{"v"},
Usage: "print only the version",
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func getAllItems(entityName string, daptinClientInstance daptinClient.DaptinClient) ([]map[string]interface{}, error) {
allWorlds, err := daptinClientInstance.FindAll(entityName, daptinClient.DaptinQueryParameters{
"page[size]": 500,
})
if err != nil {
return nil, err
}
worlds := src.MapArray(allWorlds, "attributes")
return worlds, nil
}