github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/domain/delete_domain.go (about) 1 package domain 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/api" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/errors" 10 "code.cloudfoundry.org/cli/cf/flags" 11 . "code.cloudfoundry.org/cli/cf/i18n" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 ) 15 16 type DeleteDomain struct { 17 ui terminal.UI 18 config coreconfig.Reader 19 orgReq requirements.TargetedOrgRequirement 20 domainRepo api.DomainRepository 21 } 22 23 func init() { 24 commandregistry.Register(&DeleteDomain{}) 25 } 26 27 func (cmd *DeleteDomain) MetaData() commandregistry.CommandMetadata { 28 fs := make(map[string]flags.FlagSet) 29 fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")} 30 31 return commandregistry.CommandMetadata{ 32 Name: "delete-domain", 33 Description: T("Delete a domain"), 34 Usage: []string{ 35 T("CF_NAME delete-domain DOMAIN [-f]"), 36 }, 37 Flags: fs, 38 } 39 } 40 41 func (cmd *DeleteDomain) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 42 if len(fc.Args()) != 1 { 43 cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-domain")) 44 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1) 45 } 46 47 loginReq := requirementsFactory.NewLoginRequirement() 48 cmd.orgReq = requirementsFactory.NewTargetedOrgRequirement() 49 50 reqs := []requirements.Requirement{ 51 loginReq, 52 cmd.orgReq, 53 } 54 55 return reqs, nil 56 } 57 58 func (cmd *DeleteDomain) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 59 cmd.ui = deps.UI 60 cmd.config = deps.Config 61 cmd.domainRepo = deps.RepoLocator.GetDomainRepository() 62 return cmd 63 } 64 65 func (cmd *DeleteDomain) Execute(c flags.FlagContext) error { 66 domainName := c.Args()[0] 67 domain, err := cmd.domainRepo.FindByNameInOrg(domainName, cmd.orgReq.GetOrganizationFields().GUID) 68 69 switch err.(type) { 70 case nil: 71 if domain.Shared { 72 return errors.New(T("domain {{.DomainName}} is a shared domain, not an owned domain.", 73 map[string]interface{}{ 74 "DomainName": domainName})) 75 } 76 case *errors.ModelNotFoundError: 77 cmd.ui.Ok() 78 cmd.ui.Warn(err.Error()) 79 return nil 80 default: 81 return errors.New(T("Error finding domain {{.DomainName}}\n{{.APIErr}}", 82 map[string]interface{}{"DomainName": domainName, "APIErr": err.Error()})) 83 } 84 85 if !c.Bool("f") { 86 if !cmd.ui.ConfirmDelete(T("domain"), domainName) { 87 return nil 88 } 89 } 90 91 cmd.ui.Say(T("Deleting domain {{.DomainName}} as {{.Username}}...", 92 map[string]interface{}{ 93 "DomainName": terminal.EntityNameColor(domainName), 94 "Username": terminal.EntityNameColor(cmd.config.Username())})) 95 96 err = cmd.domainRepo.Delete(domain.GUID) 97 if err != nil { 98 return errors.New(T("Error deleting domain {{.DomainName}}\n{{.APIErr}}", 99 map[string]interface{}{"DomainName": domainName, "APIErr": err.Error()})) 100 } 101 102 cmd.ui.Ok() 103 return nil 104 }