-
Notifications
You must be signed in to change notification settings - Fork 4
/
delete.go
66 lines (55 loc) · 1.77 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
package clusters
import (
"context"
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/jetstack/jsctl/internal/client"
"github.com/jetstack/jsctl/internal/cluster"
internalerrors "github.com/jetstack/jsctl/internal/command/errors"
"github.com/jetstack/jsctl/internal/command/types"
"github.com/jetstack/jsctl/internal/config"
"github.com/jetstack/jsctl/internal/prompt"
)
// Delete returns a new cobra.Command for deleting a cluster in the JSCP api
func Delete(run types.RunFunc, apiURL *string) *cobra.Command {
var force bool
cmd := &cobra.Command{
Use: "delete name",
Short: "Deletes a cluster from the organization",
Args: cobra.MatchAll(cobra.ExactArgs(1)),
Run: run(func(ctx context.Context, args []string) error {
cnf, ok := config.FromContext(ctx)
if !ok || cnf.Organization == "" {
return internalerrors.ErrNoOrganizationName
}
http := client.New(ctx, *apiURL)
name := args[0]
if name == "" {
return errors.New("you must specify a cluster name")
}
if !force {
ok, err := prompt.YesNo(os.Stdin, os.Stdout, "Are you sure you want to delete cluster %s from organization %s?", name, cnf.Organization)
switch {
case err != nil:
return fmt.Errorf("failed to prompt: %w", err)
case !ok:
return nil
}
}
err := cluster.Delete(ctx, http, cnf.Organization, name)
switch {
case errors.Is(err, cluster.ErrNoCluster):
return fmt.Errorf("cluster %s does not exist in organization %s", name, cnf.Organization)
case err != nil:
return fmt.Errorf("failed to delete cluster: %w", err)
}
fmt.Printf("Cluster %s was successfully deleted\n", name)
return nil
}),
}
flags := cmd.PersistentFlags()
flags.BoolVar(&force, "force", false, "Do not prompt for confirmation")
return cmd
}