github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/isolation_segments_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"strings"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/command"
     9  	"code.cloudfoundry.org/cli/command/v6/shared"
    10  	"code.cloudfoundry.org/cli/util/ui"
    11  )
    12  
    13  //go:generate counterfeiter . IsolationSegmentsActor
    14  
    15  type IsolationSegmentsActor interface {
    16  	GetIsolationSegmentSummaries() ([]v3action.IsolationSegmentSummary, v3action.Warnings, error)
    17  }
    18  
    19  type IsolationSegmentsCommand struct {
    20  	usage           interface{} `usage:"CF_NAME isolation-segments"`
    21  	relatedCommands interface{} `related_commands:"enable-org-isolation, create-isolation-segment"`
    22  
    23  	UI          command.UI
    24  	Config      command.Config
    25  	SharedActor command.SharedActor
    26  	Actor       IsolationSegmentsActor
    27  }
    28  
    29  func (cmd *IsolationSegmentsCommand) Setup(config command.Config, ui command.UI) error {
    30  	cmd.UI = ui
    31  	cmd.Config = config
    32  	sharedActor := sharedaction.NewActor(config)
    33  	cmd.SharedActor = sharedActor
    34  
    35  	client, _, err := shared.NewV3BasedClients(config, ui, true)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	cmd.Actor = v3action.NewActor(client, config, sharedActor, nil)
    40  
    41  	return nil
    42  }
    43  
    44  func (cmd IsolationSegmentsCommand) Execute(args []string) error {
    45  	err := cmd.SharedActor.CheckTarget(false, false)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	user, err := cmd.Config.CurrentUser()
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	cmd.UI.DisplayTextWithFlavor("Getting isolation segments as {{.CurrentUser}}...", map[string]interface{}{
    56  		"CurrentUser": user.Name,
    57  	})
    58  
    59  	summaries, warnings, err := cmd.Actor.GetIsolationSegmentSummaries()
    60  	cmd.UI.DisplayWarnings(warnings)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	cmd.UI.DisplayOK()
    65  
    66  	table := [][]string{
    67  		{
    68  			cmd.UI.TranslateText("name"),
    69  			cmd.UI.TranslateText("orgs"),
    70  		},
    71  	}
    72  
    73  	for _, summary := range summaries {
    74  		table = append(
    75  			table,
    76  			[]string{
    77  				summary.Name,
    78  				strings.Join(summary.EntitledOrgs, ", "),
    79  			},
    80  		)
    81  	}
    82  
    83  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
    84  	return nil
    85  }