-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathresources.go
More file actions
273 lines (241 loc) · 8.66 KB
/
resources.go
File metadata and controls
273 lines (241 loc) · 8.66 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
package parser
import (
"fmt"
"github.com/drycc/workflow-cli/internal/commands"
"github.com/drycc/workflow-cli/internal/completion"
"github.com/drycc/workflow-cli/internal/template"
"github.com/drycc/workflow-cli/pkg/i18n"
"github.com/spf13/cobra"
)
// NewResourcesCommand creates a command for managing resources.
func NewResourcesCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "resources",
Short: i18n.T("Manage resources for your applications"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.ResourcesList(app, results)
},
}
cmd.PersistentFlags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
cmd.AddCommand(resourcesServicesCommand(cmdr))
cmd.AddCommand(resourcesPlansCommand(cmdr))
cmd.AddCommand(resourcesCreateCommand(cmdr))
cmd.AddCommand(resourcesListCommand(cmdr))
cmd.AddCommand(resourcesDescribeCommand(cmdr))
cmd.AddCommand(resourcesUpdateCommand(cmdr))
cmd.AddCommand(resourcesBindCommand(cmdr))
cmd.AddCommand(resourcesUnbindCommand(cmdr))
cmd.AddCommand(resourcesDestroyCommand(cmdr))
return cmd
}
func resourcesServicesCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "services",
Short: i18n.T("List all available resource services"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.ResourcesServices(results)
},
}
return cmd
}
func resourcesPlansCommand(cmdr *commands.DryccCmd) *cobra.Command {
resourceServiceCompletion := completion.ResourceServiceCompletion{ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "plans <service>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc resources plans redis",
map[string]string{
"<service>": i18n.T("The service name for plans"),
},
),
Short: i18n.T("List all available plans for a resource service"),
ValidArgsFunction: resourceServiceCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
service := args[0]
results, _ := commands.ResponseLimit(limit)
return cmdr.ResourcesPlans(service, results)
},
}
return cmd
}
func resourcesCreateCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
values string
path string
confirm string
}
resourceCreateCompletion := completion.ResourceCreateCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "create <name> <service> <plan> [<param>=<value>...]",
Args: cobra.MinimumNArgs(3),
Example: template.CustomExample(
"drycc resources create myredis redis standard-128 -f file.yaml",
map[string]string{
"<name>": i18n.T("This resource instance alias"),
"<service>": i18n.T("The resource's service"),
"<plan>": i18n.T("The service's plan"),
"<param>": i18n.T("The resource instance parameters key"),
"<value>": i18n.T("The resource instance parameters value"),
},
),
Short: i18n.T("Create a resource for the application"),
ValidArgsFunction: resourceCreateCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
plan := fmt.Sprintf("%s:%s", args[1], args[2])
params := args[3:]
if flags.values != "" {
params = nil
}
return cmdr.ResourcesCreate(app, plan, name, params, flags.values)
},
}
cmd.Flags().StringVarP(&flags.values, "values", "f", "", i18n.T("Specify values in a YAML file. If set, params will be discard"))
return cmd
}
func resourcesListCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: i18n.T("List resources in the application"),
RunE: func(_ *cobra.Command, _ []string) error {
results, _ := commands.ResponseLimit(limit)
return cmdr.ResourcesList(app, results)
},
}
cmd.Flags().IntVarP(&limit, "limit", "l", 0, i18n.T("The maximum number of results to display"))
return cmd
}
func resourcesDescribeCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
name string
details bool
}
resourceCompletion := completion.ResourceCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "describe <name>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc resources describe myredis",
map[string]string{
"<name>": i18n.T("This resource instance alias"),
},
),
Short: i18n.T("Get a resource's detail in the application"),
ValidArgsFunction: resourceCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.ResourceGet(app, name, flags.details)
},
}
cmd.Flags().BoolVar(&flags.details, "details", false, i18n.T("Show extra details provided to resource"))
return cmd
}
func resourcesUpdateCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
values string
path string
}
resourceUpdateCompletion := completion.ResourceUpdateCompletion{AppID: &app, ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "update <name> <service> <plan> [<param>=<value>...]",
Args: cobra.MinimumNArgs(3),
Example: template.CustomExample(
"drycc resources update myredis redis standard-128 -f file.yaml",
map[string]string{
"<name>": i18n.T("This resource instance alias"),
"<service>": i18n.T("The resource's service"),
"<plan>": i18n.T("The service's plan"),
"<param>": i18n.T("The resource instance parameters key"),
"<value>": i18n.T("The resource instance parameters value"),
},
),
Short: i18n.T("Update a resource from the application"),
ValidArgsFunction: resourceUpdateCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
plan := fmt.Sprintf("%s:%s", args[1], args[2])
params := args[2:]
if flags.values != "" {
params = nil
}
return cmdr.ResourcePut(app, plan, name, params, flags.values)
},
}
cmd.Flags().StringVarP(&flags.values, "values", "f", "", i18n.T("Specify values in a YAML file. If set, params will be discard"))
return cmd
}
func resourcesDestroyCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
name string
confirm string
}
resourceCompletion := completion.ResourceCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "destroy <name> --confirm=<resource>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc resources destroy myredis",
map[string]string{
"<name>": i18n.T("The resource instance alias name"),
},
),
Short: i18n.T("Delete a resource from the application"),
ValidArgsFunction: resourceCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.ResourceDelete(app, name, flags.confirm)
},
}
cmd.Flags().StringVar(&flags.confirm, "confirm", "", i18n.T(`skips the prompt for the resource name. <resource> is the uniquely identifiable
name for the resource`))
return cmd
}
func resourcesBindCommand(cmdr *commands.DryccCmd) *cobra.Command {
resourceCompletion := completion.ResourceCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "bind <name>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc resources bind myredis",
map[string]string{
"<name>": i18n.T("The resource instance alias name"),
},
),
Short: i18n.T("Bind a resource for an application"),
ValidArgsFunction: resourceCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.ResourceBind(app, name)
},
}
return cmd
}
func resourcesUnbindCommand(cmdr *commands.DryccCmd) *cobra.Command {
resourceCompletion := completion.ResourceCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "unbind <name>",
Args: cobra.ExactArgs(1),
Example: template.CustomExample(
"drycc resources unbind myredis",
map[string]string{
"<name>": i18n.T("The resource instance alias name"),
},
),
Short: i18n.T("unbind a resources for an application"),
ValidArgsFunction: resourceCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
name := args[0]
return cmdr.ResourceUnbind(app, name)
},
}
return cmd
}