github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/space/spaces.go (about)

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