-
Notifications
You must be signed in to change notification settings - Fork 111
/
delete.go
89 lines (74 loc) · 2.3 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 org
import (
"context"
"fmt"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)
func DeleteCmd(cfg *config.Config) *cobra.Command {
var force bool
deleteCmd := &cobra.Command{
Use: "delete <org-name>",
Short: "Delete organization",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cmdutil.Client(cfg)
if err != nil {
return err
}
defer client.Close()
// Find all the projects for the given org
res, err := client.ListProjectsForOrganization(context.Background(), &adminv1.ListProjectsForOrganizationRequest{OrganizationName: args[0]})
if err != nil {
return err
}
var projects []string
for _, proj := range res.Projects {
projects = append(projects, proj.Name)
}
if len(projects) > 0 {
fmt.Printf("Deleting %q will also delete these projects:\n", args[0])
for _, proj := range projects {
fmt.Printf("\t%s/%s\n", args[0], proj)
}
}
if !force {
fmt.Printf("Warn: Deleting the org %q will remove all metadata associated with the org\n", args[0])
msg := fmt.Sprintf("Enter %q to confirm deletion", args[0])
org, err := cmdutil.InputPrompt(msg, "")
if err != nil {
return err
}
if org != args[0] {
return fmt.Errorf("Entered incorrect name : %s", org)
}
}
for _, proj := range projects {
_, err := client.DeleteProject(context.Background(), &adminv1.DeleteProjectRequest{OrganizationName: args[0], Name: proj})
if err != nil {
return err
}
fmt.Printf("Deleted project %s/%s\n", args[0], proj)
}
_, err = client.DeleteOrganization(context.Background(), &adminv1.DeleteOrganizationRequest{Name: args[0]})
if err != nil {
return err
}
// If deleting the default org, set the default org to empty
if args[0] == cfg.Org {
err = dotrill.SetDefaultOrg("")
if err != nil {
return err
}
}
cmdutil.SuccessPrinter(fmt.Sprintf("Deleted organization: %v", args[0]))
return nil
},
}
deleteCmd.Flags().SortFlags = false
deleteCmd.Flags().BoolVar(&force, "force", false, "Delete forcefully, skips the confirmation")
return deleteCmd
}