github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/organization/orgs.go (about) 1 package organization 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/api/organizations" 5 "github.com/cloudfoundry/cli/cf/command_registry" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 . "github.com/cloudfoundry/cli/cf/i18n" 8 "github.com/cloudfoundry/cli/cf/models" 9 "github.com/cloudfoundry/cli/cf/requirements" 10 "github.com/cloudfoundry/cli/cf/terminal" 11 "github.com/cloudfoundry/cli/flags" 12 "github.com/cloudfoundry/cli/plugin/models" 13 ) 14 15 type ListOrgs struct { 16 ui terminal.UI 17 config core_config.Reader 18 orgRepo organizations.OrganizationRepository 19 pluginOrgsModel *[]plugin_models.GetOrgs_Model 20 pluginCall bool 21 } 22 23 func init() { 24 command_registry.Register(&ListOrgs{}) 25 } 26 27 func (cmd *ListOrgs) MetaData() command_registry.CommandMetadata { 28 return command_registry.CommandMetadata{ 29 Name: "orgs", 30 ShortName: "o", 31 Description: T("List all orgs"), 32 Usage: "CF_NAME orgs", 33 } 34 } 35 36 func (cmd *ListOrgs) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) { 37 if len(fc.Args()) != 0 { 38 cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("orgs")) 39 } 40 41 reqs = []requirements.Requirement{ 42 requirementsFactory.NewLoginRequirement(), 43 } 44 return 45 } 46 47 func (cmd *ListOrgs) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command { 48 cmd.ui = deps.Ui 49 cmd.config = deps.Config 50 cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository() 51 cmd.pluginOrgsModel = deps.PluginModels.Organizations 52 cmd.pluginCall = pluginCall 53 return cmd 54 } 55 56 func (cmd ListOrgs) Execute(fc flags.FlagContext) { 57 cmd.ui.Say(T("Getting orgs as {{.Username}}...\n", 58 map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())})) 59 60 noOrgs := true 61 table := cmd.ui.Table([]string{T("name")}) 62 63 orgs, apiErr := cmd.orgRepo.ListOrgs() 64 if apiErr != nil { 65 cmd.ui.Failed(apiErr.Error()) 66 } 67 for _, org := range orgs { 68 table.Add(org.Name) 69 noOrgs = false 70 } 71 72 table.Print() 73 74 if apiErr != nil { 75 cmd.ui.Failed(T("Failed fetching orgs.\n{{.ApiErr}}", 76 map[string]interface{}{"ApiErr": apiErr})) 77 return 78 } 79 80 if noOrgs { 81 cmd.ui.Say(T("No orgs found")) 82 } 83 84 if cmd.pluginCall { 85 cmd.populatePluginModel(orgs) 86 } 87 88 } 89 90 func (cmd *ListOrgs) populatePluginModel(orgs []models.Organization) { 91 for _, org := range orgs { 92 orgModel := plugin_models.GetOrgs_Model{} 93 orgModel.Name = org.Name 94 orgModel.Guid = org.Guid 95 *(cmd.pluginOrgsModel) = append(*(cmd.pluginOrgsModel), orgModel) 96 } 97 }