-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelete.go
More file actions
191 lines (161 loc) · 6.5 KB
/
delete.go
File metadata and controls
191 lines (161 loc) · 6.5 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
package cmd
import (
"context"
"fmt"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/elasticpath/epcc-cli/external/rest"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func NewDeleteCommand(parentCmd *cobra.Command) func() {
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Deletes a resource",
SilenceUsage: false,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("please specify a resource, epcc delete [RESOURCE], see epcc delete --help")
} else {
return fmt.Errorf("invalid resource [%s] specified, see all with epcc delete --help", args[0])
}
},
}
overrides := &httpclient.HttpParameterOverrides{
QueryParameters: nil,
OverrideUrlPath: "",
}
// Ensure that any new options here are added to the resetFunc
var allow404 = false
var ifAliasExists = ""
var ifAliasDoesNotExist = ""
var repeat uint32 = 1
var repeatDelay uint32 = 100
var ignoreErrors = false
var noBodyPrint = false
resetFunc := func() {
overrides.QueryParameters = nil
overrides.OverrideUrlPath = ""
allow404 = false
ifAliasExists = ""
ifAliasDoesNotExist = ""
noBodyPrint = false
repeat = 1
repeatDelay = 100
ignoreErrors = false
}
for _, resource := range resources.GetPluralResources() {
if resource.DeleteEntityInfo == nil {
continue
}
resource := resource
resourceName := resource.SingularName
var deleteResourceCommand = &cobra.Command{
Use: GetDeleteUsage(resource),
Short: GetDeleteShort(resource),
Long: GetDeleteLong(resource),
Example: GetDeleteExample(resource),
Args: GetArgFunctionForDelete(resource),
RunE: func(cmd *cobra.Command, args []string) error {
c := func(cmd *cobra.Command, args []string) error {
if ifAliasExists != "" {
aliasId := aliases.ResolveAliasValuesOrReturnIdentity(resource.JsonApiType, resource.AlternateJsonApiTypesForAliases, ifAliasExists, "id")
if aliasId == ifAliasExists {
// If the aliasId is the same as requested, it means an alias did not exist.
log.Infof("Alias [%s] does not exist, not continuing run", ifAliasExists)
return nil
}
}
if ifAliasDoesNotExist != "" {
aliasId := aliases.ResolveAliasValuesOrReturnIdentity(resource.JsonApiType, resource.AlternateJsonApiTypesForAliases, ifAliasDoesNotExist, "id")
if aliasId != ifAliasDoesNotExist {
// If the aliasId is different than the request then it does exist.
log.Infof("Alias [%s] does exist (value: %s), not continuing run", ifAliasDoesNotExist, aliasId)
return nil
}
}
body, err := rest.DeleteInternal(context.Background(), overrides, allow404, append([]string{resourceName}, args...))
if err != nil {
if body != "" {
if !noBodyPrint {
json.PrintJsonToStdout(body)
}
}
return err
}
if noBodyPrint {
return nil
} else {
return json.PrintJsonToStdout(body)
}
}
return repeater(c, repeat, repeatDelay, cmd, args, ignoreErrors)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// len(args) == 1 means first id
// lens(args) == 2 means second id.
// Replace ids with args in resourceURL
if resource.DeleteEntityInfo == nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
idCount, err := resources.GetNumberOfVariablesNeeded(resource.DeleteEntityInfo.Url)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
if len(args) < idCount {
// Must be for a resource completion
types, err := resources.GetTypesOfVariablesNeeded(resource.DeleteEntityInfo.Url)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
typeIdxNeeded := len(args)
if completionResource, ok := resources.GetResourceByName(types[typeIdxNeeded]); ok {
return completion.Complete(completion.Request{
Type: completion.CompleteAlias,
Resource: completionResource,
})
}
} else {
if (len(args)-idCount)%2 == 0 { // This is an attribute key
usedAttributes := make(map[string]int)
for i := idCount; i < len(args); i = i + 2 {
usedAttributes[args[i]] = 0
}
return completion.Complete(completion.Request{
Type: completion.CompleteAttributeKey,
Resource: resource,
Attributes: usedAttributes,
Verb: completion.Delete,
})
} else { // This is an attribute value
return completion.Complete(completion.Request{
Type: completion.CompleteAttributeValue,
Resource: resource,
Verb: completion.Delete,
Attribute: args[len(args)-1],
ToComplete: toComplete,
AllowTemplates: true,
})
}
}
return []string{}, cobra.ShellCompDirectiveNoFileComp
},
}
deleteCmd.AddCommand(deleteResourceCommand)
}
deleteCmd.PersistentFlags().StringVar(&overrides.OverrideUrlPath, "override-url-path", "", "Override the URL that will be used for the Request")
deleteCmd.PersistentFlags().StringSliceVarP(&overrides.QueryParameters, "query-parameters", "q", []string{}, "Pass in key=value an they will be added as query parameters")
deleteCmd.PersistentFlags().BoolVar(&allow404, "allow-404", allow404, "If set 404's will not be treated as errors")
deleteCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error")
deleteCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error")
deleteCmd.PersistentFlags().BoolVarP(&noBodyPrint, "silent", "s", false, "Don't print the body on success")
deleteCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist")
deleteCmd.PersistentFlags().Uint32VarP(&repeat, "repeat", "", 1, "Number of times to repeat the command")
deleteCmd.PersistentFlags().Uint32VarP(&repeatDelay, "repeat-delay", "", 100, "Delay (in ms) between repeats")
deleteCmd.PersistentFlags().BoolVarP(&ignoreErrors, "ignore-errors", "", false, "Don't return non zero on an error")
parentCmd.AddCommand(deleteCmd)
return resetFunc
}