-
Notifications
You must be signed in to change notification settings - Fork 7
/
run_admin.go
216 lines (159 loc) · 4.27 KB
/
run_admin.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
// ThreatSpec package main
package main
import (
"fmt"
"github.com/jawher/mow.cli"
"github.com/pki-io/controller"
)
func adminCmd(cmd *cli.Cmd) {
cmd.Command("list", "List admins", adminListCmd)
cmd.Command("show", "Show an admin", adminShowCmd)
cmd.Command("invite", "Invite a new admin", adminInviteCmd)
cmd.Command("new", "Create a new admin", adminNewCmd)
cmd.Command("run", "Process admin tasks", adminRunCmd)
cmd.Command("complete", "Complete an admin invite", adminCompleteCmd)
cmd.Command("delete", "Delete an admin", adminDeleteCmd)
}
func adminListCmd(cmd *cli.Cmd) {
params := controller.NewAdminParams()
cmd.Action = func() {
app := NewAdminApp()
logger.Info("listing admins")
cont, err := controller.NewAdmin(app.env)
if err != nil {
app.Fatal(err)
}
admins, err := cont.List(params)
if err != nil {
app.Fatal(err)
}
table := app.NewTable()
table.SetHeader([]string{"Name", "Id"})
for _, admin := range admins {
table.Append([]string{admin.Name(), admin.Id()})
}
app.RenderTable(table)
}
}
func adminShowCmd(cmd *cli.Cmd) {
cmd.Spec = "NAME [OPTIONS]"
params := controller.NewAdminParams()
params.Name = cmd.StringArg("NAME", "", "name of admin")
cmd.Action = func() {
app := NewAdminApp()
logger.Info("showing admin")
cont, err := controller.NewAdmin(app.env)
if err != nil {
app.Fatal(err)
}
admin, err := cont.Show(params)
if err != nil {
app.Fatal(err)
}
if admin != nil {
table := app.NewTable()
adminData := [][]string{
[]string{"Name", admin.Name()},
[]string{"ID", admin.Id()},
}
table.AppendBulk(adminData)
app.RenderTable(table)
fmt.Println("")
fmt.Printf("Public signing key:\n%s\n", admin.Data.Body.PublicSigningKey)
fmt.Printf("Public encryption key:\n%s\n", admin.Data.Body.PublicEncryptionKey)
}
}
}
func adminInviteCmd(cmd *cli.Cmd) {
cmd.Spec = "NAME [OPTIONS]"
params := controller.NewAdminParams()
params.Name = cmd.StringArg("NAME", "", "name of admin")
cmd.Action = func() {
app := NewAdminApp()
logger.Info("creating admin invite")
cont, err := controller.NewAdmin(app.env)
if err != nil {
app.Fatal(err)
}
keyPair, err := cont.Invite(params)
if err != nil {
app.Fatal(err)
}
if len(keyPair) > 0 {
table := app.NewTable()
adminData := [][]string{
[]string{"Id", keyPair[0]},
[]string{"Key", keyPair[1]},
}
table.AppendBulk(adminData)
app.RenderTable(table)
}
}
}
func adminNewCmd(cmd *cli.Cmd) {
cmd.Spec = "NAME [OPTIONS]"
params := controller.NewAdminParams()
params.Name = cmd.StringArg("NAME", "", "name of admin")
params.InviteId = cmd.StringOpt("invite-id", "", "invite id")
params.InviteKey = cmd.StringOpt("invite-key", "", "invite key")
cmd.Action = func() {
app := NewAdminApp()
logger.Info("creating new admin")
cont, err := controller.NewAdmin(app.env)
if err != nil {
app.Fatal(err)
}
if err := cont.New(params); err != nil {
app.Fatal(err)
}
}
}
func adminRunCmd(cmd *cli.Cmd) {
params := controller.NewAdminParams()
cmd.Action = func() {
app := NewAdminApp()
logger.Info("running admin tasks")
cont, err := controller.NewAdmin(app.env)
if err != nil {
app.Fatal(err)
}
if err := cont.Run(params); err != nil {
app.Fatal(err)
}
}
}
func adminCompleteCmd(cmd *cli.Cmd) {
cmd.Spec = "NAME [OPTIONS]"
params := controller.NewAdminParams()
params.Name = cmd.StringArg("NAME", "", "name of admin")
params.InviteId = cmd.StringOpt("invite-id", "", "invite id")
params.InviteKey = cmd.StringOpt("invite-key", "", "invite key")
cmd.Action = func() {
app := NewAdminApp()
logger.Info("completing new admin")
cont, err := controller.NewAdmin(app.env)
if err != nil {
app.Fatal(err)
}
if err := cont.Complete(params); err != nil {
app.Fatal(err)
}
}
}
func adminDeleteCmd(cmd *cli.Cmd) {
cmd.Spec = "NAME [OPTIONS]"
params := controller.NewAdminParams()
params.Name = cmd.StringArg("NAME", "", "name of admin")
params.ConfirmDelete = cmd.StringOpt("confirm-delete", "", "reason for deleting admin")
cmd.Action = func() {
app := NewAdminApp()
logger.Info("deleting admin")
cont, err := controller.NewAdmin(app.env)
if err != nil {
app.Fatal(err)
}
if err := cont.Delete(params); err != nil {
app.Fatal(err)
}
}
}