-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
188 lines (172 loc) · 4.12 KB
/
main.go
File metadata and controls
188 lines (172 loc) · 4.12 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
package main
import (
"context"
"fmt"
"log"
"os"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "lobos/lobosctl/protos"
"github.com/urfave/cli/v3"
)
var (
globalFlags = []cli.Flag{
&cli.StringFlag{
Name: "endpoint",
Usage: "gRPC server endpoint",
Value: "127.0.0.1:50051",
},
}
nameFlag = &cli.StringFlag{
Name: "name",
Usage: "name of the new resource",
Required: true,
}
forceFlag = &cli.BoolFlag{
Name: "force",
}
accessKeyFlag = &cli.StringFlag{
Name: "access-key",
Usage: "S3 access key",
Required: false,
}
accessKeyReqFlag = &cli.StringFlag{
Name: "access-key",
Usage: "S3 access key",
Required: true,
}
secretFlag = &cli.StringFlag{
Name: "secret",
Usage: "S3 secret",
Required: false,
}
rmUserFlags = []cli.Flag{
nameFlag,
forceFlag,
}
userFlags = []cli.Flag{
nameFlag,
accessKeyFlag,
secretFlag,
}
keyRmFlags = []cli.Flag{
nameFlag,
accessKeyReqFlag,
}
)
var (
conn *grpc.ClientConn
client pb.ControlPlaneClient
)
func main() {
cmd := &cli.Command{
Name: "lobosctl",
Usage: "Interact with lobos control plane",
Flags: globalFlags,
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
endpoint := cmd.String("endpoint")
var err error
conn, err = grpc.NewClient(endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return ctx, fmt.Errorf("did not connect: %v", err)
}
client = pb.NewControlPlaneClient(conn)
return ctx, nil
},
Commands: []*cli.Command{
{
Name: "users",
Usage: "Add/Remove/List S3 users",
Commands: []*cli.Command{
{
Name: "add",
Usage: "Adds an user",
Flags: userFlags,
Action: func(ctx context.Context, cmd *cli.Command) error {
r, err := client.AddUser(ctx, &pb.User{
Name: cmd.String("name"),
Key: cmd.String("access-key"),
Secret: cmd.String("secret"),
})
if err != nil {
log.Fatalf("could not add user: %v", err)
}
fmt.Printf("User %s created. Access: %s, secret: %s\n", r.User.Name, r.User.Key, r.User.Secret)
return nil
},
},
{
Name: "list",
Usage: "List all users",
Action: func(ctx context.Context, cmd *cli.Command) error {
r, err := client.ListAllUsers(ctx, &pb.Filters{})
if err != nil {
log.Fatalf("Could not list all users: %v", err)
}
for _, u := range r.Users {
fmt.Printf("user: %s - key: %s - secret: %s\n", u.User.Name, u.User.Key, u.User.Secret)
}
return nil
},
},
{
Name: "rm",
Usage: "Remove an user",
Flags: rmUserFlags,
Action: func(ctx context.Context, cmd *cli.Command) error {
_, err := client.RmUser(ctx, &pb.RmUserParams{
Name: cmd.String("name"),
Force: cmd.Bool("force"),
})
if err != nil {
log.Fatalf("Could not list all users: %v", err)
}
return nil
},
},
},
},
{
Name: "keys",
Usage: "Add/Remove S3 access",
Commands: []*cli.Command{
{
Name: "add",
Usage: "Adds an S3 access",
Flags: userFlags,
Action: func(ctx context.Context, cmd *cli.Command) error {
r, err := client.AddKey(ctx, &pb.User{
Name: cmd.String("name"),
Key: cmd.String("access-key"),
Secret: cmd.String("secret"),
})
if err != nil {
log.Fatalf("could not add user: %v", err)
}
fmt.Printf("Access: %s, secret: %s\n", r.User.Key, r.User.Secret)
return nil
},
},
{
Name: "rm",
Usage: "Removes an S3 access",
Flags: keyRmFlags,
Action: func(ctx context.Context, cmd *cli.Command) error {
_, err := client.RmKey(ctx, &pb.User{
Name: cmd.String("name"),
Key: cmd.String("access-key"),
})
if err != nil {
log.Fatalf("could not add user: %v", err)
}
return nil
},
},
},
},
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}