-
Notifications
You must be signed in to change notification settings - Fork 414
/
action.go
307 lines (255 loc) · 6.24 KB
/
action.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
package main
import (
"fmt"
"io/ioutil"
"path"
"time"
"github.com/spf13/cobra"
"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
actionSDK "github.com/ovh/cds/sdk/action"
"github.com/ovh/cds/sdk/exportentities"
"github.com/ovh/cds/sdk/slug"
)
var actionCmd = cli.Command{
Name: "action",
Short: "Manage CDS action",
}
var actionBuiltinCmd = cli.Command{
Name: "builtin",
Short: "Manage CDS builtin action",
}
func action() *cobra.Command {
return cli.NewCommand(actionCmd, nil, []*cobra.Command{
cli.NewListCommand(actionListCmd, actionListRun, nil),
cli.NewGetCommand(actionShowCmd, actionShowRun, nil),
cli.NewCommand(actionDeleteCmd, actionDeleteRun, nil),
cli.NewCommand(actionDocCmd, actionDocRun, nil),
cli.NewCommand(actionImportCmd, actionImportRun, nil),
cli.NewCommand(actionExportCmd, actionExportRun, nil),
cli.NewCommand(actionBuiltinCmd, nil, []*cobra.Command{
cli.NewListCommand(actionBuiltinListCmd, actionBuiltinListRun, nil),
cli.NewGetCommand(actionBuiltinShowCmd, actionBuiltinShowRun, nil),
cli.NewCommand(actionBuiltinDocCmd, actionBuiltinDocRun, nil),
}),
})
}
func newActionDisplay(a sdk.Action) actionDisplay {
name := a.Name
if a.Group != nil {
name = fmt.Sprintf("%s/%s", a.Group.Name, a.Name)
}
ad := actionDisplay{
Fullname: name,
Type: a.Type,
}
if a.FirstAudit != nil {
ad.Created = fmt.Sprintf("On %s by %s", a.FirstAudit.Created.Format(time.RFC3339),
a.FirstAudit.AuditCommon.TriggeredBy)
} else {
ad.Created = "No audit found"
}
return ad
}
type actionDisplay struct {
Created string `cli:"Created"`
Fullname string `cli:"Fullname,key"`
Type string `cli:"Type"`
}
var actionListCmd = cli.Command{
Name: "list",
Short: "List CDS actions",
}
func actionListRun(v cli.Values) (cli.ListResult, error) {
as, err := client.ActionList()
if err != nil {
return nil, err
}
ads := make([]actionDisplay, len(as))
for i := range as {
ads[i] = newActionDisplay(as[i])
}
return cli.AsListResult(ads), nil
}
var actionShowCmd = cli.Command{
Name: "show",
Short: "Show a CDS action",
Args: []cli.Arg{
{Name: "action-path"},
},
}
func actionShowRun(v cli.Values) (interface{}, error) {
groupName, actionName, err := cli.ParsePath(v.GetString("action-path"))
if err != nil {
return nil, err
}
action, err := client.ActionGet(groupName, actionName)
if err != nil {
return nil, err
}
return newActionDisplay(*action), nil
}
var actionDeleteCmd = cli.Command{
Name: "delete",
Short: "Delete a CDS action",
Long: `Useful to delete a CDS action
cdsctl action delete myAction
# this will not fail if action does not exist
cdsctl action delete myActionNotExist --force
`,
Args: []cli.Arg{
{Name: "action-path"},
},
Flags: []cli.Flag{
{
Name: "force",
Usage: "if true, do not fail if action does not exist",
IsValid: func(s string) bool {
return s == "true" || s == "false"
},
Default: "false",
Type: cli.FlagBool,
},
},
}
func actionDeleteRun(v cli.Values) error {
groupName, actionName, err := cli.ParsePath(v.GetString("action-path"))
if err != nil {
return err
}
err = client.ActionDelete(groupName, actionName)
if v.GetBool("force") && sdk.ErrorIs(err, sdk.ErrNotFound) {
fmt.Println(err)
return nil
}
return err
}
var actionDocCmd = cli.Command{
Name: "doc",
Short: "Generate action documentation: cdsctl action doc <path-to-file>",
Args: []cli.Arg{
{Name: "path"},
},
}
func actionDocRun(v cli.Values) error {
p := v.GetString("path")
contentFile, format, err := exportentities.OpenPath(p)
if err != nil {
return err
}
defer contentFile.Close()
body, err := ioutil.ReadAll(contentFile)
if err != nil {
return err
}
var ea exportentities.Action
if err := exportentities.Unmarshal(body, format, &ea); err != nil {
return err
}
act, errapp := ea.GetAction()
if errapp != nil {
return errapp
}
fmt.Println(sdk.ActionInfoMarkdown(act, path.Base(p)))
return nil
}
var actionImportCmd = cli.Command{
Name: "import",
Short: "Import a CDS action",
Args: []cli.Arg{
{Name: "path"},
},
}
func actionImportRun(v cli.Values) error {
path := v.GetString("path")
contentFile, format, err := exportentities.OpenPath(path)
if err != nil {
return err
}
defer contentFile.Close() //nolint
formatStr, _ := exportentities.GetFormatStr(format)
if err := client.ActionImport(contentFile, formatStr); err != nil {
return err
}
fmt.Printf("%s successfully imported\n", path)
return nil
}
var actionExportCmd = cli.Command{
Name: "export",
Short: "Export a CDS action",
Args: []cli.Arg{
{Name: "action-path"},
},
Flags: []cli.Flag{
{
Name: "format",
Usage: "Specify export format (json or yaml)",
Default: "yaml",
},
},
}
func actionExportRun(v cli.Values) error {
groupName, actionName, err := cli.ParsePath(v.GetString("action-path"))
if err != nil {
return err
}
b, err := client.ActionExport(groupName, actionName, v.GetString("format"))
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
var actionBuiltinListCmd = cli.Command{
Name: "list",
Short: "List CDS builtin actions",
}
func actionBuiltinListRun(v cli.Values) (cli.ListResult, error) {
as, err := client.ActionBuiltinList()
if err != nil {
return nil, err
}
ads := make([]actionDisplay, len(as))
for i := range as {
ads[i] = newActionDisplay(as[i])
}
return cli.AsListResult(ads), nil
}
var actionBuiltinShowCmd = cli.Command{
Name: "show",
Short: "Show a CDS builtin action",
Args: []cli.Arg{
{Name: "action-name"},
},
}
func actionBuiltinShowRun(v cli.Values) (interface{}, error) {
action, err := client.ActionBuiltinGet(v.GetString("action-name"))
if err != nil {
return nil, err
}
return newActionDisplay(*action), nil
}
var actionBuiltinDocCmd = cli.Command{
Name: "doc",
Short: "Generate Builtin action documentation: cdsctl action builtin doc <name>",
Args: []cli.Arg{
{Name: "name"},
},
}
func actionBuiltinDocRun(v cli.Values) error {
n := v.GetString("name")
var found bool
var m actionSDK.Manifest
for i := range actionSDK.List {
if slug.Convert(actionSDK.List[i].Action.Name) == slug.Convert(n) {
found = true
m = actionSDK.List[i]
break
}
}
if !found {
return fmt.Errorf("Invalid given action name %s", n)
}
fmt.Println(m.Markdown())
return nil
}