forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_space_command.go
103 lines (83 loc) · 2.78 KB
/
delete_space_command.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
package v2
import (
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/v2/shared"
)
//go:generate counterfeiter . DeleteSpaceActor
type DeleteSpaceActor interface {
DeleteSpaceByNameAndOrganizationName(spaceName string, orgName string) (v2action.Warnings, error)
}
type DeleteSpaceCommand struct {
RequiredArgs flag.Space `positional-args:"yes"`
Force bool `short:"f" description:"Force deletion without confirmation"`
Org string `short:"o" description:"Delete space within specified org"`
usage interface{} `usage:"CF_NAME delete-space SPACE [-o ORG] [-f]"`
Config command.Config
UI command.UI
SharedActor command.SharedActor
Actor DeleteSpaceActor
}
func (cmd *DeleteSpaceCommand) Setup(config command.Config, ui command.UI) error {
cmd.Config = config
cmd.UI = ui
cmd.SharedActor = sharedaction.NewActor()
ccClient, uaaClient, err := shared.NewClients(config, ui, true)
if err != nil {
return err
}
cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
return nil
}
func (cmd DeleteSpaceCommand) Execute(args []string) error {
var (
err error
orgName string
)
if cmd.Org != "" {
err = cmd.SharedActor.CheckTarget(cmd.Config, false, false)
orgName = cmd.Org
} else {
err = cmd.SharedActor.CheckTarget(cmd.Config, true, false)
orgName = cmd.Config.TargetedOrganization().Name
}
if err != nil {
return shared.HandleError(err)
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return shared.HandleError(err)
}
if !cmd.Force {
promptMessage := "Really delete the space {{.SpaceName}}?"
deleteSpace, promptErr := cmd.UI.DisplayBoolPrompt(false, promptMessage, map[string]interface{}{"SpaceName": cmd.RequiredArgs.Space})
if promptErr != nil {
return promptErr
}
if !deleteSpace {
cmd.UI.DisplayText("Delete cancelled")
return nil
}
}
cmd.UI.DisplayTextWithFlavor("Deleting space {{.TargetSpace}} in org {{.TargetOrg}} as {{.CurrentUser}}...",
map[string]interface{}{
"TargetSpace": cmd.RequiredArgs.Space,
"TargetOrg": orgName,
"CurrentUser": user.Name,
})
warnings, err := cmd.Actor.DeleteSpaceByNameAndOrganizationName(cmd.RequiredArgs.Space, orgName)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return shared.HandleError(err)
}
cmd.UI.DisplayOK()
if cmd.Config.TargetedOrganization().Name == orgName &&
cmd.Config.TargetedSpace().Name == cmd.RequiredArgs.Space {
cmd.Config.UnsetSpaceInformation()
cmd.UI.DisplayText("TIP: No space targeted, use '{{.CfTargetCommand}}' to target a space.",
map[string]interface{}{"CfTargetCommand": cmd.Config.BinaryName() + " target -s"})
}
return nil
}