-
Notifications
You must be signed in to change notification settings - Fork 72
/
enable.go
260 lines (204 loc) · 7.06 KB
/
enable.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
package enable
import (
"context"
"net/http"
"github.com/AlecAivazis/survey/v2"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/core/servicecontext"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client"
)
type options struct {
IO *iostreams.IOStreams
Connection factory.ConnectionFunc
Logger logging.Logger
localizer localize.Localizer
Context context.Context
ServiceContext servicecontext.IContext
ruleType string
config string
registryID string
artifactID string
group string
}
// NewEnableCommand creates a new command for enabling rule
// nolint:funlen
func NewEnableCommand(f *factory.Factory) *cobra.Command {
opts := &options{
IO: f.IOStreams,
Connection: f.Connection,
Logger: f.Logger,
localizer: f.Localizer,
Context: f.Context,
ServiceContext: f.ServiceContext,
}
cmd := &cobra.Command{
Use: "enable",
Short: f.Localizer.MustLocalize("registry.rule.enable.cmd.description.short"),
Long: f.Localizer.MustLocalize("registry.rule.enable.cmd.description.long"),
Example: f.Localizer.MustLocalize("registry.rule.enable.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
validator := rulecmdutil.Validator{
Localizer: opts.localizer,
}
var missingFlags []string
if opts.ruleType == "" {
missingFlags = append(missingFlags, "rule-type")
}
if opts.config == "" {
missingFlags = append(missingFlags, "config")
}
if !opts.IO.CanPrompt() && len(missingFlags) > 0 {
return flagutil.RequiredWhenNonInteractiveError(missingFlags...)
}
if len(missingFlags) == 2 {
err = runInteractivePrompt(opts)
if err != nil {
return err
}
} else if len(missingFlags) > 0 {
return flagutil.RequiredWhenNonInteractiveError(missingFlags...)
}
err = validator.ValidateRuleType(opts.ruleType)
if err != nil {
return err
}
isValid, configs := validator.IsValidRuleConfig(opts.ruleType, opts.config)
if !isValid {
return opts.localizer.MustLocalizeError("registry.rule.common.error.invalidRuleConfig",
localize.NewEntry("RuleType", opts.ruleType),
localize.NewEntry("Config", opts.config),
localize.NewEntry("ValidConfigList", cmdutil.StringSliceToListStringWithQuotes(configs)),
)
}
registryInstance, err := contextutil.GetCurrentRegistryInstance(f)
if err != nil {
return err
}
opts.registryID = registryInstance.GetId()
return runEnable(opts)
},
}
flags := rulecmdutil.NewFlagSet(cmd, f)
flags.AddRegistryInstance(&opts.registryID)
flags.AddArtifactID(&opts.artifactID)
flags.AddGroup(&opts.group)
flags.AddConfig(&opts.config)
flags.AddRuleType(&opts.ruleType)
return cmd
}
func runEnable(opts *options) error {
var httpRes *http.Response
var newErr error
conn, err := opts.Connection()
if err != nil {
return err
}
dataAPI, _, err := conn.API().ServiceRegistryInstance(opts.registryID)
if err != nil {
return err
}
rule := registryinstanceclient.Rule{
Config: rulecmdutil.GetMappedConfigValue(opts.config),
Type: rulecmdutil.GetMappedRuleType(opts.ruleType),
}
if opts.artifactID == "" {
opts.Logger.Info(
opts.localizer.MustLocalize("registry.rule.enable.log.info.enabling.globalRules",
localize.NewEntry("RuleType", opts.ruleType),
localize.NewEntry("Configuration", opts.config),
localize.NewEntry("ID", opts.registryID),
),
)
req := dataAPI.GlobalRulesApi.CreateGlobalRule(opts.Context)
req = req.Rule(rule)
httpRes, newErr = req.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
} else {
if opts.group == registrycmdutil.DefaultArtifactGroup {
opts.Logger.Info(opts.localizer.MustLocalize("registry.artifact.common.message.no.group", localize.NewEntry("DefaultArtifactGroup", registrycmdutil.DefaultArtifactGroup)))
}
opts.Logger.Info(
opts.localizer.MustLocalize("registry.rule.enable.log.info.enabling.artifactRules",
localize.NewEntry("RuleType", opts.ruleType),
localize.NewEntry("Configuration", opts.config),
localize.NewEntry("ArtifactID", opts.artifactID),
),
)
req := dataAPI.ArtifactRulesApi.CreateArtifactRule(opts.Context, opts.group, opts.artifactID)
req = req.Rule(rule)
httpRes, newErr = req.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
}
ruleErrHandler := &rulecmdutil.RuleErrHandler{
Localizer: opts.localizer,
}
if newErr != nil {
if httpRes == nil {
return registrycmdutil.TransformInstanceError(newErr)
}
switch httpRes.StatusCode {
case http.StatusNotFound:
return ruleErrHandler.ArtifactNotFoundError(opts.artifactID)
case http.StatusConflict:
return ruleErrHandler.ConflictError(opts.ruleType)
default:
return registrycmdutil.TransformInstanceError(newErr)
}
}
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("registry.rule.enable.log.info.ruleEnabled"))
return nil
}
func runInteractivePrompt(opts *options) (err error) {
ruleTypePrompt := &survey.Select{
Message: opts.localizer.MustLocalize("registry.rule.enable.input.ruleType.message"),
Help: opts.localizer.MustLocalize("registry.rule.common.flag.ruleType"),
Options: rulecmdutil.ValidRuleTypes,
}
err = survey.AskOne(ruleTypePrompt, &opts.ruleType)
if err != nil {
return err
}
configOptions := rulecmdutil.ValidRuleConfigs[opts.ruleType]
configPrompt := &survey.Select{
Message: opts.localizer.MustLocalize("registry.rule.enable.input.config.message"),
Help: opts.localizer.MustLocalize("registry.rule.common.flag.config"),
Options: configOptions,
}
err = survey.AskOne(configPrompt, &opts.config)
if err != nil {
return err
}
artifactIDPrompt := &survey.Input{
Message: opts.localizer.MustLocalize("registry.rule.enable.input.artifactID.message"),
Help: opts.localizer.MustLocalize("registry.rule.enable.input.artifactID.help"),
}
err = survey.AskOne(artifactIDPrompt, &opts.artifactID)
if err != nil {
return err
}
groupPrompt := &survey.Input{
Message: opts.localizer.MustLocalize("registry.rule.enable.input.group.message"),
Help: opts.localizer.MustLocalize("registry.rule.enable.input.group.help"),
Default: registrycmdutil.DefaultArtifactGroup,
}
err = survey.AskOne(groupPrompt, &opts.group)
if err != nil {
return err
}
return nil
}