-
Notifications
You must be signed in to change notification settings - Fork 72
/
delete.go
162 lines (130 loc) · 4.15 KB
/
delete.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
package delete
import (
"github.com/AlecAivazis/survey/v2"
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil"
"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/localize"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/connectorutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"
"github.com/spf13/cobra"
)
type options struct {
id string
name string
outputFormat string
f *factory.Factory
skipConfirm bool
}
// NewDeleteCommand creates a new command to delete a connector
func NewDeleteCommand(f *factory.Factory) *cobra.Command {
opts := &options{
f: f,
}
cmd := &cobra.Command{
Use: "delete",
Short: f.Localizer.MustLocalize("connector.delete.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.delete.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.delete.cmd.example"),
Hidden: false,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
validOutputFormats := flagutil.ValidOutputFormats
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
}
if opts.name != "" && opts.id != "" {
return opts.f.Localizer.MustLocalizeError("service.error.idAndNameCannotBeUsed")
}
return runDelete(opts)
},
}
flags := connectorcmdutil.NewFlagSet(cmd, f)
flags.AddOutput(&opts.outputFormat)
_ = flags.AddConnectorID(&opts.id)
_ = flags.AddConnectorName(&opts.name)
flags.AddYes(&opts.skipConfirm)
return cmd
}
func runDelete(opts *options) error {
f := opts.f
var conn connection.Connection
conn, err := f.Connection()
if err != nil {
return err
}
api := conn.API()
connectorMgmt := api.ConnectorsMgmt()
var connector *connectormgmtclient.Connector
if opts.id != "" {
connector, err = connectorutil.GetConnectorByID(&connectorMgmt, opts.id, f)
if err != nil {
return err
}
}
if opts.name != "" {
connector, err = connectorutil.GetConnectorByName(&connectorMgmt, opts.name, f)
if err != nil {
return err
}
}
if connector == nil {
connector, err = contextutil.GetCurrentConnectorInstance(&conn, f)
if err != nil {
return err
}
}
opts.id = connector.GetId()
if !opts.skipConfirm {
confirm, promptErr := promptConfirmDelete(opts)
if promptErr != nil {
return promptErr
}
if !confirm {
opts.f.Logger.Debug("User has chosen to not delete connector cluster")
return nil
}
}
a := connectorMgmt.ConnectorsApi.DeleteConnector(f.Context, opts.id)
_, httpRes, err := a.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
return err
}
f.Logger.Info(icon.SuccessPrefix(), f.Localizer.MustLocalize("connector.delete.info.success"))
svcContext, err := f.ServiceContext.Load()
if err != nil {
return err
}
currCtx, err := contextutil.GetCurrentContext(svcContext, f.Localizer)
if err != nil {
return err
}
// this is not the current instance, our work here is done
if currCtx.ConnectorID != connector.GetId() {
return nil
}
// the connector that was deleted is set as the user's current cluster
// since it was deleted it should be removed from the context
currCtx.ConnectorID = ""
svcContext.Contexts[svcContext.CurrentContext] = *currCtx
if err := opts.f.ServiceContext.Save(svcContext); err != nil {
return err
}
return nil
}
func promptConfirmDelete(opts *options) (bool, error) {
promptConfirm := survey.Confirm{
Message: opts.f.Localizer.MustLocalize("connector.delete.confirmDialog.message", localize.NewEntry("ID", opts.id)),
}
var confirmDelete bool
if err := survey.AskOne(&promptConfirm, &confirmDelete); err != nil {
return false, err
}
return confirmDelete, nil
}