-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommands.go
More file actions
418 lines (359 loc) · 9.07 KB
/
commands.go
File metadata and controls
418 lines (359 loc) · 9.07 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package cmd
import (
"fmt"
"os"
"strings"
"github.com/howeyc/gopass"
"github.com/lastbackend/cli/pkg/cli/config"
"github.com/lastbackend/cli/pkg/cli/envs"
"github.com/lastbackend/cli/pkg/cli/storage"
"github.com/lastbackend/cli/pkg/client"
"github.com/lastbackend/cli/pkg/client/genesis/http/v1/request"
"github.com/lastbackend/cli/pkg/util/filesystem"
"github.com/lastbackend/lastbackend/pkg/log"
"github.com/spf13/cobra"
)
const defaultHost = "https://api.lastbackend.com"
const version = "0.1.0"
func init() {
RootCmd.AddCommand(
loginCmd,
logoutCmd,
clusterCmd,
namespaceCmd,
routeCmd,
serviceCmd,
secretCmd,
configCmd,
volumeCmd,
versionCmd,
nodeCmd,
ingressCmd,
jobCmd,
)
}
var (
cfg = config.Get()
ctx = envs.Get()
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "lb",
Short: "Apps cloud hosting with integrated deployment tools",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var err error
cfg.Debug, err = cmd.Flags().GetBool("debug")
if err != nil {
fmt.Println("Invalid debug flag")
return
}
token, err := storage.GetToken()
if err != nil {
fmt.Println("There is no token in .lastbackend in homedir")
return
}
if cmd.Flag("token").Value != nil && len(cmd.Flag("token").Value.String()) != 0 {
token = cmd.Flag("token").Value.String()
}
host := defaultHost
config := &client.Config{Token: token}
tls, err := cmd.Flags().GetBool("tls")
if err != nil {
fmt.Println("Invalid tls flag")
return
}
if tls {
config.TLS = new(client.TLSConfig)
config.TLS.Insecure = false
config.TLS.CAFile = cmd.Flag("tlscacert").Value.String()
config.TLS.CertFile = cmd.Flag("tlscert").Value.String()
config.TLS.KeyFile = cmd.Flag("tlskey").Value.String()
}
cli := &client.Client{}
cli.Genesis = client.NewGenesisCluster(host, config)
cli.Registry = client.NewRegistryClient(host, config)
// ============================
// Use cluster from flag -C or --cluster
// ============================
config.Headers = make(map[string]string, 0)
cn := cmd.Flag("cluster").Value.String()
if len(cn) != 0 {
match := strings.Split(cn, ":")
switch len(match) {
case 1:
cluster, err := storage.GetLocalCluster(cn)
if err != nil {
panic(err)
}
if cluster == nil {
fmt.Println("cluster not found")
return
}
host = cluster.Endpoint
case 2:
config.Headers["X-Cluster-Name"] = cn
default:
fmt.Println("invalid data")
return
}
cli.Cluster = client.NewClusterClient(host, config)
ctx.SetClient(cli)
return
}
// ============================
// Use selected cluster
// ============================
cluster, err := storage.GetCluster()
if err != nil {
panic(err)
}
if cluster != "" {
switch cluster[:2] {
case "l.":
cluster, err := storage.GetLocalCluster(cluster[2:])
if err != nil {
log.Error(err.Error())
return
}
if cluster == nil {
fmt.Println("can not read cluster info: check cache data ($HOME/.lastbackend)")
return
}
host = cluster.Endpoint
config.Token = cluster.Token
case "r.":
config.Headers = make(map[string]string, 0)
config.Headers["X-Cluster-Name"] = cluster[2:]
default:
fmt.Println("can not read cluster info: check cache data ($HOME/.lastbackend)")
}
}
cli.Cluster = client.NewClusterClient(host, config)
ctx.SetClient(cli)
},
}
var namespaceCmd = &cobra.Command{
Use: "namespace",
Short: "Manage your namespaces",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
command := "[string]"
if len(args) != 0 {
command = args[0]
}
var ns = &cobra.Command{
Use: command,
Short: "Manage your a namespace",
}
cmd.AddCommand(ns)
if len(args) == 0 {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
return
}
// Attach sub command for namespace
ns.AddCommand(
serviceCmd,
secretCmd,
routeCmd,
)
if err := ns.Execute(); err != nil {
log.Error(err.Error())
return
}
},
}
var loginCmd = &cobra.Command{
Use: "login",
Short: "Log in to a Last.Backend",
Example: `
# Log in to a Last.Backend
lb login
Login: username
Password: ******`,
Run: func(cmd *cobra.Command, args []string) {
var (
login string
password string
)
fmt.Print("Login: ")
if _, err := fmt.Scan(&login); err != nil {
log.Error(err.Error())
return
}
fmt.Print("Password: ")
pass, err := gopass.GetPasswd()
if err != nil {
log.Error(err.Error())
return
}
password = string(pass)
fmt.Print("\r\n")
cli := envs.Get().GetClient()
opts := &request.AccountLoginOptions{
Login: login,
Password: password,
}
session, err := cli.Genesis.V1().Account().Login(envs.Background(), opts)
if err != nil {
fmt.Println(err.Error())
return
}
if err := storage.SetToken(session.Token); err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("Authorization successful!")
return
},
}
var logoutCmd = &cobra.Command{
Use: "logout",
Short: "Log out from a Last.Backend",
Example: `
# Log out from a Last.Backend
lb logout"`,
Run: func(cmd *cobra.Command, args []string) {
if err := storage.SetToken(""); err != nil {
fmt.Println(err)
return
}
},
}
var serviceCmd = &cobra.Command{
Use: "service",
Short: "Manage your service",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var jobCmd = &cobra.Command{
Use: "job",
Short: "Manage your job",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var secretCmd = &cobra.Command{
Use: "secret",
Short: "Manage your secret",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage your configs",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var volumeCmd = &cobra.Command{
Use: "volume",
Short: "Manage your volumes",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var routeCmd = &cobra.Command{
Use: "route",
Short: "Manage your route",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var clusterCmd = &cobra.Command{
Use: "cluster",
Short: "Manage your cluster",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var nodeCmd = &cobra.Command{
Use: "node",
Short: "Manage cluster nodes",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var ingressCmd = &cobra.Command{
Use: "ingress",
Short: "Manage cluster ingress servers",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
var discoveryCmd = &cobra.Command{
Use: "discovery",
Short: "Manage cluster discovery servers",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
log.Error(err.Error())
return
}
},
}
// Execute adds all child commands to the root command
func Execute() {
cobra.OnInitialize()
var getSSLPath = func(filepath string) string {
return strings.Join([]string{filesystem.HomeDir(), ".lastbackend", filepath}, string(os.PathSeparator))
}
RootCmd.PersistentFlags().StringP("cluster", "C", "", "Use cluster for operations")
RootCmd.PersistentFlags().Bool("debug", false, "Enable debug mode")
RootCmd.PersistentFlags().Bool("tls", false, "Use TLS")
RootCmd.PersistentFlags().String("tlscacert", "", fmt.Sprintf("Trust certs signed only by this CA (default \"%s\")", getSSLPath("ca.pem")))
RootCmd.PersistentFlags().String("tlscert", "", fmt.Sprintf("Path to TLS certificate file (default \"%s\")", getSSLPath("cert.pem")))
RootCmd.PersistentFlags().String("tlskey", "", fmt.Sprintf("Path to TLS key file (default \"%s\")", getSSLPath("key.pem")))
RootCmd.PersistentFlags().String("token", "", "Set access token for header")
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}