github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/domain/delete_shared_domain.go (about) 1 package domain 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/api" 5 "github.com/cloudfoundry/cli/cf/command_metadata" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 "github.com/cloudfoundry/cli/cf/errors" 8 . "github.com/cloudfoundry/cli/cf/i18n" 9 "github.com/cloudfoundry/cli/cf/requirements" 10 "github.com/cloudfoundry/cli/cf/terminal" 11 "github.com/codegangsta/cli" 12 ) 13 14 type DeleteSharedDomain struct { 15 ui terminal.UI 16 config core_config.Reader 17 orgReq requirements.TargetedOrgRequirement 18 domainRepo api.DomainRepository 19 } 20 21 func NewDeleteSharedDomain(ui terminal.UI, config core_config.Reader, repo api.DomainRepository) (cmd *DeleteSharedDomain) { 22 cmd = new(DeleteSharedDomain) 23 cmd.ui = ui 24 cmd.config = config 25 cmd.domainRepo = repo 26 return 27 } 28 29 func (cmd *DeleteSharedDomain) Metadata() command_metadata.CommandMetadata { 30 return command_metadata.CommandMetadata{ 31 Name: "delete-shared-domain", 32 Description: T("Delete a shared domain"), 33 Usage: T("CF_NAME delete-shared-domain DOMAIN [-f]"), 34 Flags: []cli.Flag{ 35 cli.BoolFlag{Name: "f", Usage: T("Force deletion without confirmation")}, 36 }, 37 } 38 } 39 40 func (cmd *DeleteSharedDomain) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) { 41 if len(c.Args()) != 1 { 42 cmd.ui.FailWithUsage(c) 43 } 44 45 loginReq := requirementsFactory.NewLoginRequirement() 46 cmd.orgReq = requirementsFactory.NewTargetedOrgRequirement() 47 48 reqs = []requirements.Requirement{ 49 loginReq, 50 cmd.orgReq, 51 } 52 53 return 54 } 55 56 func (cmd *DeleteSharedDomain) Run(c *cli.Context) { 57 domainName := c.Args()[0] 58 force := c.Bool("f") 59 60 cmd.ui.Say(T("Deleting domain {{.DomainName}} as {{.Username}}...", 61 map[string]interface{}{ 62 "DomainName": terminal.EntityNameColor(domainName), 63 "Username": terminal.EntityNameColor(cmd.config.Username())})) 64 65 domain, apiErr := cmd.domainRepo.FindByNameInOrg(domainName, cmd.orgReq.GetOrganizationFields().Guid) 66 switch apiErr.(type) { 67 case nil: 68 case *errors.ModelNotFoundError: 69 cmd.ui.Ok() 70 cmd.ui.Warn(apiErr.Error()) 71 return 72 default: 73 cmd.ui.Failed(T("Error finding domain {{.DomainName}}\n{{.ApiErr}}", 74 map[string]interface{}{ 75 "DomainName": domainName, 76 "ApiErr": apiErr.Error()})) 77 return 78 } 79 80 if !force { 81 answer := cmd.ui.Confirm(T("This domain is shared across all orgs.\nDeleting it will remove all associated routes, and will make any app with this domain unreachable.\nAre you sure you want to delete the domain {{.DomainName}}? ", map[string]interface{}{"DomainName": domainName})) 82 83 if !answer { 84 return 85 } 86 } 87 88 apiErr = cmd.domainRepo.DeleteSharedDomain(domain.Guid) 89 if apiErr != nil { 90 cmd.ui.Failed(T("Error deleting domain {{.DomainName}}\n{{.ApiErr}}", 91 map[string]interface{}{"DomainName": domainName, "ApiErr": apiErr.Error()})) 92 return 93 } 94 95 cmd.ui.Ok() 96 }