forked from HuaweiTech/cli
/
delete_shared_domain.go
96 lines (81 loc) · 2.51 KB
/
delete_shared_domain.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
package domain
import (
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type DeleteSharedDomain struct {
ui terminal.UI
config configuration.Reader
orgReq requirements.TargetedOrgRequirement
domainRepo api.DomainRepository
}
func NewDeleteSharedDomain(ui terminal.UI, config configuration.Reader, repo api.DomainRepository) (cmd *DeleteSharedDomain) {
cmd = new(DeleteSharedDomain)
cmd.ui = ui
cmd.config = config
cmd.domainRepo = repo
return
}
func (command *DeleteSharedDomain) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "delete-shared-domain",
Description: "Delete a shared domain",
Usage: "CF_NAME delete-shared-domain DOMAIN [-f]",
Flags: []cli.Flag{
cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
},
}
}
func (cmd *DeleteSharedDomain) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
err = errors.New("Incorrect Usage")
cmd.ui.FailWithUsage(c)
return
}
loginReq := requirementsFactory.NewLoginRequirement()
cmd.orgReq = requirementsFactory.NewTargetedOrgRequirement()
reqs = []requirements.Requirement{
loginReq,
cmd.orgReq,
}
return
}
func (cmd *DeleteSharedDomain) Run(c *cli.Context) {
domainName := c.Args()[0]
force := c.Bool("f")
cmd.ui.Say("Deleting domain %s as %s...",
terminal.EntityNameColor(domainName),
terminal.EntityNameColor(cmd.config.Username()),
)
domain, apiErr := cmd.domainRepo.FindByNameInOrg(domainName, cmd.orgReq.GetOrganizationFields().Guid)
switch apiErr.(type) {
case nil:
case *errors.ModelNotFoundError:
cmd.ui.Ok()
cmd.ui.Warn(apiErr.Error())
return
default:
cmd.ui.Failed("Error finding domain %s\n%s", domainName, apiErr.Error())
return
}
if !force {
answer := cmd.ui.Confirm(
`This domain is shared across all orgs.
Deleting it will remove all associated routes, and will make any app with this domain unreachable.
Are you sure you want to delete the domain %s? `, domainName)
if !answer {
return
}
}
apiErr = cmd.domainRepo.DeleteSharedDomain(domain.Guid)
if apiErr != nil {
cmd.ui.Failed("Error deleting domain %s\n%s", domainName, apiErr.Error())
return
}
cmd.ui.Ok()
}