github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/servicebroker/service_brokers.go (about) 1 package servicebroker 2 3 import ( 4 "sort" 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 ListServiceBrokers struct { 17 ui terminal.UI 18 config coreconfig.Reader 19 repo api.ServiceBrokerRepository 20 } 21 22 type serviceBrokerTable []serviceBrokerRow 23 24 type serviceBrokerRow struct { 25 name string 26 url string 27 } 28 29 func init() { 30 commandregistry.Register(&ListServiceBrokers{}) 31 } 32 33 func (cmd *ListServiceBrokers) MetaData() commandregistry.CommandMetadata { 34 return commandregistry.CommandMetadata{ 35 Name: "service-brokers", 36 Description: T("List service brokers"), 37 Usage: []string{ 38 "CF_NAME service-brokers", 39 }, 40 } 41 } 42 43 func (cmd *ListServiceBrokers) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 44 usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), 45 T("No argument required"), 46 func() bool { 47 return len(fc.Args()) != 0 48 }, 49 ) 50 51 reqs := []requirements.Requirement{ 52 usageReq, 53 requirementsFactory.NewLoginRequirement(), 54 } 55 56 return reqs, nil 57 } 58 59 func (cmd *ListServiceBrokers) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 60 cmd.ui = deps.UI 61 cmd.config = deps.Config 62 cmd.repo = deps.RepoLocator.GetServiceBrokerRepository() 63 return cmd 64 } 65 66 func (cmd *ListServiceBrokers) Execute(c flags.FlagContext) error { 67 sbTable := serviceBrokerTable{} 68 69 cmd.ui.Say(T("Getting service brokers as {{.Username}}...\n", 70 map[string]interface{}{ 71 "Username": terminal.EntityNameColor(cmd.config.Username()), 72 })) 73 74 table := cmd.ui.Table([]string{T("name"), T("url")}) 75 foundBrokers := false 76 err := cmd.repo.ListServiceBrokers(func(serviceBroker models.ServiceBroker) bool { 77 sbTable = append(sbTable, serviceBrokerRow{ 78 name: serviceBroker.Name, 79 url: serviceBroker.URL, 80 }) 81 foundBrokers = true 82 return true 83 }) 84 if err != nil { 85 return err 86 } 87 88 sort.Sort(sbTable) 89 90 for _, sb := range sbTable { 91 table.Add(sb.name, sb.url) 92 } 93 94 err = table.Print() 95 if err != nil { 96 return err 97 } 98 99 if !foundBrokers { 100 cmd.ui.Say(T("No service brokers found")) 101 } 102 return nil 103 } 104 105 func (a serviceBrokerTable) Len() int { return len(a) } 106 func (a serviceBrokerTable) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 107 func (a serviceBrokerTable) Less(i, j int) bool { return a[i].name < a[j].name }