forked from redhat-developer/odo
/
delete.go
113 lines (96 loc) · 3.04 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
package application
import (
"fmt"
odoUtil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/application"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/odo/cli/project"
"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/openshift/odo/pkg/util"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"
)
const deleteRecommendedCommandName = "delete"
var (
deleteExample = ktemplates.Examples(` # Delete the application
%[1]s myapp`)
)
// DeleteOptions encapsulates the options for the odo command
type DeleteOptions struct {
appName string
force bool
*genericclioptions.Context
}
// NewDeleteOptions creates a new DeleteOptions instance
func NewDeleteOptions() *DeleteOptions {
return &DeleteOptions{}
}
// Complete completes DeleteOptions after they've been created
func (o *DeleteOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
o.Context = genericclioptions.NewContext(cmd)
o.appName = o.Application
if len(args) == 1 {
// If app name passed, consider it for deletion
o.appName = args[0]
}
return
}
// Validate validates the DeleteOptions based on completed values
func (o *DeleteOptions) Validate() (err error) {
if o.Context.Project == "" || o.appName == "" {
return odoUtil.ThrowContextError()
}
if !util.CheckOutputFlag(o.OutputFlag) {
return fmt.Errorf("given output format %s is not supported", o.OutputFlag)
}
exist, err := application.Exists(o.appName, o.Client)
if !exist {
return fmt.Errorf("%s app does not exists", o.appName)
}
return err
}
// Run contains the logic for the odo command
func (o *DeleteOptions) Run() (err error) {
if log.IsJSON() {
err = application.Delete(o.Client, o.appName)
if err != nil {
return err
}
return nil
}
// Print App Information which will be deleted
err = printDeleteAppInfo(o.Client, o.appName, o.Project)
if err != nil {
return err
}
if o.force || ui.Proceed(fmt.Sprintf("Are you sure you want to delete the application: %v from project: %v", o.appName, o.Project)) {
err = application.Delete(o.Client, o.appName)
if err != nil {
return err
}
log.Infof("Deleted application: %s from project: %v", o.appName, o.Project)
} else {
log.Infof("Aborting deletion of application: %v", o.appName)
}
return
}
// NewCmdDelete implements the odo command.
func NewCmdDelete(name, fullName string) *cobra.Command {
o := NewDeleteOptions()
command := &cobra.Command{
Use: name,
Short: "Delete the given application",
Long: "Delete the given application",
Example: fmt.Sprintf(deleteExample, fullName),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
command.Flags().BoolVarP(&o.force, "force", "f", false, "Delete application without prompting")
project.AddProjectFlag(command)
completion.RegisterCommandHandler(command, completion.AppCompletionHandler)
return command
}