forked from redhat-developer/odo
/
delete.go
102 lines (89 loc) · 3.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
package url
import (
"fmt"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/odo/cli/ui"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/odo/util/completion"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
)
const deleteRecommendedCommandName = "delete"
var (
urlDeleteShortDesc = `Delete a URL`
urlDeleteLongDesc = ktemplates.LongDesc(`Delete the given URL, hence making the service inaccessible.`)
urlDeleteExample = ktemplates.Examples(` # Delete a URL to a component
%[1]s myurl
`)
)
// URLDeleteOptions encapsulates the options for the odo url delete command
type URLDeleteOptions struct {
localConfigInfo *config.LocalConfigInfo
componentContext string
urlName string
urlForceDeleteFlag bool
*genericclioptions.Context
}
// NewURLDeleteOptions creates a new UrlDeleteOptions instance
func NewURLDeleteOptions() *URLDeleteOptions {
return &URLDeleteOptions{}
}
// Complete completes URLDeleteOptions after they've been Deleted
func (o *URLDeleteOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
o.Context = genericclioptions.NewContext(cmd)
o.urlName = args[0]
o.localConfigInfo, err = config.NewLocalConfigInfo(o.componentContext)
return
}
// Validate validates the URLDeleteOptions based on completed values
func (o *URLDeleteOptions) Validate() (err error) {
//exists, err := url.Exists(o.Client, o.localConfigInfo, o.urlName, o.Component(), o.Application)
//if err != nil {
// return err
//}
var exists bool
urls := o.localConfigInfo.GetUrl()
for _, url := range urls {
if url.Name == o.urlName {
exists = true
}
}
if !exists {
return fmt.Errorf("the URL %s does not exist within the component %s", o.urlName, o.Component())
}
return
}
// Run contains the logic for the odo url delete command
func (o *URLDeleteOptions) Run() (err error) {
if o.urlForceDeleteFlag || ui.Proceed(fmt.Sprintf("Are you sure you want to delete the url %v", o.urlName)) {
err = o.localConfigInfo.DeleteUrl(o.urlName)
if err != nil {
return err
}
log.Successf("URL %s removed from the config file", o.urlName)
log.Info("To delete URL on the OpenShift Cluster, please use `odo push`")
} else {
return fmt.Errorf("aborting deletion of URL: %v", o.urlName)
}
return
}
// NewCmdURLDelete implements the odo url delete command.
func NewCmdURLDelete(name, fullName string) *cobra.Command {
o := NewURLDeleteOptions()
urlDeleteCmd := &cobra.Command{
Use: name + " [url name]",
Short: urlDeleteShortDesc,
Long: urlDeleteLongDesc,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
Example: fmt.Sprintf(urlDeleteExample, fullName),
}
urlDeleteCmd.Flags().BoolVarP(&o.urlForceDeleteFlag, "force", "f", false, "Delete url without prompting")
genericclioptions.AddContextFlag(urlDeleteCmd, &o.componentContext)
completion.RegisterCommandHandler(urlDeleteCmd, completion.URLCompletionHandler)
completion.RegisterCommandFlagHandler(urlDeleteCmd, "context", completion.FileCompletionHandler)
return urlDeleteCmd
}