github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/spaces_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/sharedaction"
     5  	"code.cloudfoundry.org/cli/actor/v2action"
     6  	"code.cloudfoundry.org/cli/command"
     7  	"code.cloudfoundry.org/cli/command/v6/shared"
     8  	"code.cloudfoundry.org/cli/util/ui"
     9  )
    10  
    11  //go:generate counterfeiter . SpacesActor
    12  
    13  type SpacesActor interface {
    14  	GetOrganizationSpaces(orgGUID string) ([]v2action.Space, v2action.Warnings, error)
    15  }
    16  
    17  type SpacesCommand struct {
    18  	usage           interface{} `usage:"CF_NAME spaces"`
    19  	relatedCommands interface{} `related_commands:"target"`
    20  
    21  	UI          command.UI
    22  	Config      command.Config
    23  	SharedActor command.SharedActor
    24  	Actor       SpacesActor
    25  }
    26  
    27  func (cmd *SpacesCommand) Setup(config command.Config, ui command.UI) error {
    28  	cmd.Config = config
    29  	cmd.UI = ui
    30  	cmd.SharedActor = sharedaction.NewActor(config)
    31  
    32  	ccClient, _, err := shared.NewClients(config, ui, true)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	cmd.Actor = v2action.NewActor(ccClient, nil, config)
    37  
    38  	return nil
    39  }
    40  
    41  func (cmd SpacesCommand) Execute([]string) error {
    42  	err := cmd.SharedActor.CheckTarget(true, false)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	user, err := cmd.Config.CurrentUser()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	cmd.UI.DisplayTextWithFlavor("Getting spaces in org {{.OrgName}} as {{.CurrentUser}}...", map[string]interface{}{
    53  		"OrgName":     cmd.Config.TargetedOrganization().Name,
    54  		"CurrentUser": user.Name,
    55  	})
    56  	cmd.UI.DisplayNewline()
    57  
    58  	spaces, warnings, err := cmd.Actor.GetOrganizationSpaces(cmd.Config.TargetedOrganization().GUID)
    59  	cmd.UI.DisplayWarnings(warnings)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	if len(spaces) == 0 {
    65  		cmd.UI.DisplayText("No spaces found.")
    66  	} else {
    67  		cmd.displaySpaces(spaces)
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func (cmd SpacesCommand) displaySpaces(spaces []v2action.Space) {
    74  	table := [][]string{{cmd.UI.TranslateText("name")}}
    75  	for _, space := range spaces {
    76  		table = append(table, []string{space.Name})
    77  	}
    78  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
    79  }