github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/domain/domains.go (about) 1 package domain 2 3 import ( 4 "errors" 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/flags" 10 . "code.cloudfoundry.org/cli/cf/i18n" 11 "code.cloudfoundry.org/cli/cf/models" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 ) 15 16 type ListDomains struct { 17 ui terminal.UI 18 config coreconfig.Reader 19 domainRepo api.DomainRepository 20 routingAPIRepo api.RoutingAPIRepository 21 } 22 23 func init() { 24 commandregistry.Register(&ListDomains{}) 25 } 26 27 func (cmd *ListDomains) MetaData() commandregistry.CommandMetadata { 28 return commandregistry.CommandMetadata{ 29 Name: "domains", 30 Description: T("List domains in the target org"), 31 Usage: []string{ 32 "CF_NAME domains", 33 }, 34 } 35 } 36 37 func (cmd *ListDomains) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 38 usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), 39 T("No argument required"), 40 func() bool { 41 return len(fc.Args()) != 0 42 }, 43 ) 44 45 reqs := []requirements.Requirement{ 46 usageReq, 47 requirementsFactory.NewLoginRequirement(), 48 requirementsFactory.NewTargetedOrgRequirement(), 49 } 50 51 return reqs, nil 52 } 53 54 func (cmd *ListDomains) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 55 cmd.ui = deps.UI 56 cmd.config = deps.Config 57 cmd.domainRepo = deps.RepoLocator.GetDomainRepository() 58 cmd.routingAPIRepo = deps.RepoLocator.GetRoutingAPIRepository() 59 60 return cmd 61 } 62 63 func (cmd *ListDomains) Execute(c flags.FlagContext) error { 64 org := cmd.config.OrganizationFields() 65 66 cmd.ui.Say(T("Getting domains in org {{.OrgName}} as {{.Username}}...", 67 map[string]interface{}{ 68 "OrgName": terminal.EntityNameColor(org.Name), 69 "Username": terminal.EntityNameColor(cmd.config.Username())})) 70 71 domains, err := cmd.getDomains(org.GUID) 72 if err != nil { 73 return errors.New(T("Failed fetching domains.\n{{.Error}}", map[string]interface{}{"Error": err.Error()})) 74 } 75 76 table := cmd.ui.Table([]string{T("name"), T("status"), T("type")}) 77 78 for _, domain := range domains { 79 if domain.Shared { 80 table.Add(domain.Name, T("shared"), domain.RouterGroupType) 81 } 82 } 83 84 for _, domain := range domains { 85 if !domain.Shared { 86 table.Add(domain.Name, T("owned"), domain.RouterGroupType) 87 } 88 } 89 90 err = table.Print() 91 if err != nil { 92 return err 93 } 94 95 if len(domains) == 0 { 96 cmd.ui.Say(T("No domains found")) 97 } 98 return nil 99 } 100 101 func (cmd *ListDomains) getDomains(orgGUID string) ([]models.DomainFields, error) { 102 domains := []models.DomainFields{} 103 err := cmd.domainRepo.ListDomainsForOrg(orgGUID, func(domain models.DomainFields) bool { 104 domains = append(domains, domain) 105 return true 106 }) 107 108 if err != nil { 109 return []models.DomainFields{}, err 110 } 111 112 return domains, nil 113 }