/
app_delete.go
104 lines (88 loc) · 2.53 KB
/
app_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
package cmd
import (
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/cli/print"
"github.com/replicatedhq/replicated/pkg/logger"
"github.com/replicatedhq/replicated/pkg/types"
"github.com/spf13/cobra"
)
func (r *runners) InitAppDelete(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "delete NAME",
Short: "delete kots apps",
Long: `Delete a kots app. There is no undo for this operation, use with caution.`,
RunE: r.deleteApp,
SilenceUsage: true,
}
parent.AddCommand(cmd)
cmd.Flags().BoolVarP(&r.args.deleteAppForceYes, "force", "f", false, "Skip confirmation prompt. There is no undo for this action.")
cmd.Flags().StringVar(&r.outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")
return cmd
}
func (r *runners) deleteApp(_ *cobra.Command, args []string) error {
log := logger.NewLogger(r.w)
if len(args) != 1 {
return errors.New("missing app slug or id")
}
appName := args[0]
log.ActionWithSpinner("Fetching App")
app, err := r.kotsAPI.GetApp(appName)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "list apps")
}
log.FinishSpinner()
apps := []types.AppAndChannels{{App: app}}
err = print.Apps(r.outputFormat, r.w, apps)
if err != nil {
return errors.Wrap(err, "print app")
}
if !r.args.deleteAppForceYes {
answer, err := promptConfirmDelete()
if err != nil {
return errors.Wrap(err, "confirm deletion")
}
if answer != "yes" {
return errors.New("prompt declined")
}
}
log.ActionWithSpinner("Deleting App")
err = r.kotsAPI.DeleteKOTSApp(app.ID)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "delete app")
}
log.FinishSpinner()
return nil
}
var templates = &promptui.PromptTemplates{
Prompt: "{{ . | bold }} ",
Valid: "{{ . | green }} ",
Invalid: "{{ . | red }} ",
Success: "{{ . | bold }} ",
}
func promptConfirmDelete() (string, error) {
prompt := promptui.Prompt{
Label: "Delete the above listed application? There is no undo:",
Templates: templates,
Default: "",
Validate: func(input string) error {
// "no" will exit with a "prompt declined" error, just in case they don't think to ctrl+c
if input == "no" || input == "yes" {
return nil
}
return errors.New(`only "yes" will be accepted`)
},
}
for {
result, err := prompt.Run()
if err != nil {
if err == promptui.ErrInterrupt {
return "", errors.New("interrupted")
}
continue
}
return result, nil
}
}