-
Notifications
You must be signed in to change notification settings - Fork 4
/
delete_core_instance.go
84 lines (68 loc) · 2.04 KB
/
delete_core_instance.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
package coreinstance
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"golang.org/x/term"
"github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/confirm"
)
func NewCmdDeleteCoreInstance(config *cfg.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "core_instance",
Aliases: []string{"instance", "core_instance"},
Short: "Delete a core instance from a Kubernetes cluster.",
}
cmd.AddCommand(
NewCmdDeleteCoreInstanceOperator(config, nil),
)
return cmd
}
func NewCmdDeleteCoreInstances(config *cfg.Config) *cobra.Command {
var confirmed bool
cmd := &cobra.Command{
Use: "core_instances",
Short: "Delete many core instances from project",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
aa, err := config.Cloud.CoreInstances(ctx, config.ProjectID, types.CoreInstancesParams{
Last: cfg.Ptr(uint(0)),
})
if err != nil {
return fmt.Errorf("could not prefetch core instances to delete: %w", err)
}
if len(aa.Items) == 0 {
cmd.Println("No core instances to delete")
return nil
}
if !confirmed {
cmd.Printf("You are about to delete:\n\n%s\n\nAre you sure you want to delete all of them? (y/N) ", strings.Join(completer.CoreInstanceKeys(aa.Items), "\n"))
confirmed, err := confirm.Read(cmd.InOrStdin())
if err != nil {
return err
}
if !confirmed {
cmd.Println("Aborted")
return nil
}
}
coreInstanceIDs := make([]string, len(aa.Items))
for i, a := range aa.Items {
coreInstanceIDs[i] = a.ID
}
err = config.Cloud.DeleteCoreInstances(ctx, config.ProjectID, coreInstanceIDs...)
if err != nil {
return fmt.Errorf("delete core instances: %w", err)
}
cmd.Printf("Successfully deleted %d core instances\n", len(coreInstanceIDs))
return nil
},
}
isNonInteractive := os.Stdin == nil || !term.IsTerminal(int(os.Stdin.Fd()))
fs := cmd.Flags()
fs.BoolVarP(&confirmed, "yes", "y", isNonInteractive, "Confirm deletion")
return cmd
}