-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgic.go
338 lines (320 loc) · 8.43 KB
/
gic.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/nevermosby/gic-cli-tool/config"
gic "github.com/nevermosby/gic-cloud-sdk-go"
"github.com/nevermosby/promptui"
"github.com/urfave/cli/v2"
)
const (
GICBaseUrl = "https://api2.capitalonline.net"
)
var (
app = &cli.App{}
p = fmt.Println
)
func init() {
// TODO:
// help text
}
func info() {
app.Name = "GIC CLI "
app.Usage = "GIC CLI is designed for CDS Cloud Platform, manage your own cloud resource easily"
app.Authors = []*cli.Author{
&cli.Author{
Name: "Wen-Quan(David) Li",
Email: "[email protected]",
},
}
app.Version = "v1.0.0"
app.Copyright = "MIT (c) 2020 Wen-Quan Li"
}
func command() {
app.Commands = []*cli.Command{
{
Name: "login",
// Category: "login",
Aliases: []string{"l"},
Usage: "login GIC platform to get the token and store it locally",
Action: func(c *cli.Context) error {
fmt.Println("Start to login: ", c.Args().First())
if c.NArg() == 0 {
// if there is no args provided, check the legacy config
// load the config file first, check the token is created less than one hour
token := config.CheckToken()
if token != "" {
// p("token is already valid: ",token)
p("logged already")
} else {
p("Token is expired, pls login mannually")
// do the interactive input
usernameProm := promptui.Prompt{
Label: "Username",
}
username, err := usernameProm.Run()
if err != nil {
fmt.Printf("username input failed %v\n", err)
}
pwdProm := promptui.Prompt{
Label: "Password",
// Validate: validate,
Mask: ' ',
}
pwd, err := pwdProm.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
}
// use sdk to login to get token
// then store the info into config file
gicLogin2Save(username, pwd)
}
} else {
fmt.Println("list the command args:", c.Args())
if c.Args().Get(0) == "help" {
p("show the help text")
} else {
p("pls login without args")
}
}
return nil
},
},
{
Name: "os",
Aliases: []string{"os"},
Usage: "manage the available os, you can `list` `search`",
Subcommands: []*cli.Command{
{
Name: "list",
Category: "os",
Usage: "list all the availabe os",
Action: func(c *cli.Context) error {
token := config.CheckToken()
if token != "" {
// p("logged already")
var client = &gic.Client{}
client.Init(GICBaseUrl, "")
client.LoginWithToken(token)
osList, err := client.ListOS()
if err != nil {
log.Fatal(err)
}
for _, os := range osList {
p("os display name: ", os.DisplayName)
}
} else {
p("Pls login first.")
}
return nil
},
},
{
Name: "search",
Category: "os",
Usage: "search for all the availabe os with keyword",
ArgsUsage: "[keyword]",
Action: func(c *cli.Context) error {
keyword := c.Args().First()
token := config.CheckToken()
var matchedOS []string
var displayName string
if token != "" {
// p("logged already")
var client = &gic.Client{}
client.Init(GICBaseUrl, "")
client.LoginWithToken(token)
osList, err := client.ListOS()
if err != nil {
log.Fatal(err)
}
for _, os := range osList {
displayName = os.DisplayName
if strings.Contains(strings.ToLower(displayName), keyword) {
matchedOS = append(matchedOS, displayName)
}
}
for _, mos := range matchedOS {
p("Found:", mos)
}
} else {
p("Pls login first.")
}
return nil
},
},
},
},
{
// TODO: add options to specifiy the datacenter resource
Name: "datacenter",
Aliases: []string{"dc"},
Usage: "manage the gic datacenter instances, you can `list`,`create`,`delete`,`info`",
Subcommands: []*cli.Command{
{
Name: "list",
Category: "dc",
Usage: "list all the datecenter instances",
Action: func(c *cli.Context) error {
p("start to list all the datecenter instace: ", c.Args().First())
token := config.CheckToken()
if token != "" {
// p("token is already valid: ",token)
// p("logged already")
var client = &gic.Client{}
client.Init(GICBaseUrl, "")
client.LoginWithToken(token)
datacenters, err := client.ListDataCenter()
if err != nil {
log.Fatal(err)
}
for _, d := range datacenters {
p("datacenter name:", d.SiteName, ",datacenter resource name:", d.Resource.Name)
}
} else {
p("Pls login first.")
}
return nil
},
},
{
Name: "create",
Category: "dc",
Usage: "create a datacenter instance",
Action: func(c *cli.Context) error {
fmt.Println("start to create a datecenter instace: ", c.Args().First())
return nil
},
},
{
Name: "delete",
Category: "dc",
Usage: "delete an existing datacenter",
Action: func(c *cli.Context) error {
fmt.Println("start to delete an existing datecenter: ", c.Args().First())
return nil
},
},
{
Name: "info",
Category: "dc",
Usage: "show the detail info for an existing datacenter",
Action: func(c *cli.Context) error {
fmt.Println("start to reset os for an existing datacenter: ", c.Args().First())
return nil
},
},
},
},
{
// TODO: add options to specifiy the vm resource
Name: "virtualmachine",
Aliases: []string{"vm"},
Usage: "manage the gic normal vm instances, you can `list`,`create`,`delete`,`resetos`",
Subcommands: []*cli.Command{
{
Name: "list",
Category: "vm",
Usage: "list all the vm instances in a datacenter",
Action: func(c *cli.Context) error {
fmt.Println("start to list all the vm instace in a datacenter: ", c.Args().First())
return nil
},
},
{
Name: "create",
Category: "vm",
Usage: "create a vm instance",
Action: func(c *cli.Context) error {
fmt.Println("start to create a vm instace: ", c.Args().First())
return nil
},
},
{
Name: "delete",
Category: "vm",
Usage: "delete an existing vm",
Action: func(c *cli.Context) error {
fmt.Println("start to delete an existing vm: ", c.Args().First())
return nil
},
},
{
Name: "restart",
Category: "vm",
Usage: "restart an existing vm",
Action: func(c *cli.Context) error {
fmt.Println("start to restart an existing vm: ", c.Args().First())
return nil
},
},
{
Name: "shutdown",
Category: "vm",
Usage: "shutdown an existing vm",
Action: func(c *cli.Context) error {
fmt.Println("start to shutdown an existing vm: ", c.Args().First())
return nil
},
},
{
Name: "poweron",
Category: "vm",
Usage: "poweron an existing vm",
Action: func(c *cli.Context) error {
fmt.Println("start to poweron an existing vm: ", c.Args().First())
return nil
},
},
{
Name: "resetos",
Category: "vm",
Usage: "reset os for an existing vm",
Action: func(c *cli.Context) error {
fmt.Println("start to reset os for an existing vm: ", c.Args().First())
return nil
},
},
},
},
}
}
// the default behaviour for other commands
func noArgs(c *cli.Context) error {
// cli.ShowAppHelp(c)
return cli.NewExitError("No such commands provided: "+"'"+c.Args().First()+"'"+". Run 'gic help' for usage", 2)
}
// use sdk to login to get token
// then store the info into config file
func gicLogin2Save(username string, pwd string) {
var client = &gic.Client{}
client.Init(GICBaseUrl, "")
client.Login(username, pwd)
p("login token insider gicLogin2Save:", client.Token)
configFile, err := config.Load("")
if err != nil {
log.Fatal(err)
}
configFile.Url = GICBaseUrl
configFile.Username = username
configFile.Cred = pwd
configFile.Token.Val = client.Token
configFile.Token.CreatedAt = time.Now().Format(time.RFC3339)
// then store the info into config file
configFile.Save()
}
func main() {
info()
command()
// default action behaviour
app.Action = noArgs
err := app.Run(os.Args)
// for debug
// err := app.Run([]string{"login"})
if err != nil {
log.Fatal(err)
}
}