/
delete.go
89 lines (86 loc) · 2.59 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
package cliserv
import (
"fmt"
"github.com/containerum/chkit/pkg/context"
"github.com/containerum/chkit/pkg/model/service"
"github.com/containerum/chkit/pkg/util/activekit"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func Delete(ctx *context.Context) *cobra.Command {
var deleteServiceConfig = struct {
Force bool
}{}
command := &cobra.Command{
Use: "service",
Aliases: aliases,
Short: "delete service in specific namespace",
Long: "Delete service in namespace.",
Example: "chkit delete service service_label [-n namespace]",
Run: func(cmd *cobra.Command, args []string) {
logrus.Debugf("running command delete service")
switch len(args) {
case 0:
list, err := ctx.Client.GetServiceList(ctx.GetNamespace().ID)
if err != nil {
logrus.WithError(err).Errorf("unable to get service list")
activekit.Attention(err.Error())
return
}
var menu []*activekit.MenuItem
for _, srv := range list {
menu = append(menu, &activekit.MenuItem{
Label: srv.Name,
Action: func(srv service.Service) func() error {
return func() error {
if yes, _ := activekit.Yes(fmt.Sprintf("Do you really want delete service %q?", srv.Name)); !yes {
return nil
}
logrus.Debugf("deleting service %q from %q", srv.Name)
err := ctx.Client.DeleteService(ctx.GetNamespace().ID, srv.Name)
if err != nil {
logrus.WithError(err).Debugf("error while deleting service")
fmt.Printf("Unable to delete service %q :(\n%v", srv.Name, err)
ctx.Exit(1)
}
fmt.Printf("OK\n")
return nil
}
}(srv),
})
}
(&activekit.Menu{
Title: "Which service do you want to delete?",
Items: append(menu, []*activekit.MenuItem{
{
Label: "Exit",
},
}...),
}).Run()
case 1:
svcName := args[0]
if !deleteServiceConfig.Force {
if !activekit.YesNo(fmt.Sprintf("Do you really want delete service %q?", svcName)) {
ctx.Exit(0)
}
}
logrus.Debugf("deleting service %q from %q", svcName, ctx.GetNamespace().ID)
err := ctx.Client.DeleteService(ctx.GetNamespace().ID, svcName)
if err != nil {
logrus.WithError(err).Debugf("error while deleting service")
fmt.Printf("Unable to delete service %q :(\n%v", svcName, err)
ctx.Exit(1)
}
fmt.Printf("OK\n")
return
default:
logrus.Debugf("showing help")
cmd.Help()
return
}
},
}
command.PersistentFlags().
BoolVarP(&deleteServiceConfig.Force, "force", "f", false, "force delete without confirmation")
return command
}