github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/cf/commands/space/spaces.go (about) 1 package space 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/api/spaces" 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 "code.cloudfoundry.org/cli/plugin/models" 15 ) 16 17 type ListSpaces struct { 18 ui terminal.UI 19 config coreconfig.Reader 20 spaceRepo spaces.SpaceRepository 21 22 pluginModel *[]plugin_models.GetSpaces_Model 23 pluginCall bool 24 } 25 26 func init() { 27 commandregistry.Register(&ListSpaces{}) 28 } 29 30 func (cmd *ListSpaces) MetaData() commandregistry.CommandMetadata { 31 return commandregistry.CommandMetadata{ 32 Name: "spaces", 33 Description: T("List all spaces in an org"), 34 Usage: []string{ 35 T("CF_NAME spaces"), 36 }, 37 } 38 39 } 40 41 func (cmd *ListSpaces) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 42 usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), 43 T("No argument required"), 44 func() bool { 45 return len(fc.Args()) != 0 46 }, 47 ) 48 49 reqs := []requirements.Requirement{ 50 usageReq, 51 requirementsFactory.NewLoginRequirement(), 52 requirementsFactory.NewTargetedOrgRequirement(), 53 } 54 55 return reqs, nil 56 } 57 58 func (cmd *ListSpaces) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 59 cmd.ui = deps.UI 60 cmd.config = deps.Config 61 cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository() 62 cmd.pluginCall = pluginCall 63 cmd.pluginModel = deps.PluginModels.Spaces 64 return cmd 65 } 66 67 func (cmd *ListSpaces) Execute(c flags.FlagContext) error { 68 cmd.ui.Say(T("Getting spaces in org {{.TargetOrgName}} as {{.CurrentUser}}...\n", 69 map[string]interface{}{ 70 "TargetOrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 71 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 72 })) 73 74 foundSpaces := false 75 table := cmd.ui.Table([]string{T("name")}) 76 err := cmd.spaceRepo.ListSpaces(func(space models.Space) bool { 77 table.Add(space.Name) 78 foundSpaces = true 79 80 if cmd.pluginCall { 81 s := plugin_models.GetSpaces_Model{} 82 s.Name = space.Name 83 s.Guid = space.GUID 84 *(cmd.pluginModel) = append(*(cmd.pluginModel), s) 85 } 86 87 return true 88 }) 89 err = table.Print() 90 if err != nil { 91 return err 92 } 93 94 if err != nil { 95 return errors.New(T("Failed fetching spaces.\n{{.ErrorDescription}}", 96 map[string]interface{}{ 97 "ErrorDescription": err.Error(), 98 })) 99 } 100 101 if !foundSpaces { 102 cmd.ui.Say(T("No spaces found")) 103 } 104 return nil 105 }