github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/services_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/actor/sharedaction"
     8  	"code.cloudfoundry.org/cli/actor/v2action"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    10  	"code.cloudfoundry.org/cli/command"
    11  	"code.cloudfoundry.org/cli/command/v2/shared"
    12  )
    13  
    14  //go:generate counterfeiter . ServicesActor
    15  
    16  type ServicesActor interface {
    17  	GetServiceInstancesSummaryBySpace(spaceGUID string) ([]v2action.ServiceInstanceSummary, v2action.Warnings, error)
    18  }
    19  
    20  type ServicesCommand struct {
    21  	usage           interface{} `usage:"CF_NAME services"`
    22  	relatedCommands interface{} `related_commands:"create-service, marketplace"`
    23  
    24  	UI          command.UI
    25  	Config      command.Config
    26  	SharedActor command.SharedActor
    27  	Actor       ServicesActor
    28  }
    29  
    30  func (cmd *ServicesCommand) Setup(config command.Config, ui command.UI) error {
    31  	cmd.Config = config
    32  	cmd.UI = ui
    33  	cmd.SharedActor = sharedaction.NewActor(config)
    34  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    39  
    40  	return nil
    41  }
    42  
    43  func (cmd ServicesCommand) Execute(args []string) error {
    44  	err := cmd.SharedActor.CheckTarget(true, true)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	user, err := cmd.Config.CurrentUser()
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	cmd.UI.DisplayTextWithFlavor("Getting services in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    55  		map[string]interface{}{
    56  			"OrgName":     cmd.Config.TargetedOrganization().Name,
    57  			"SpaceName":   cmd.Config.TargetedSpace().Name,
    58  			"CurrentUser": user.Name,
    59  		})
    60  	cmd.UI.DisplayNewline()
    61  
    62  	instanceSummaries, warnings, err := cmd.Actor.GetServiceInstancesSummaryBySpace(cmd.Config.TargetedSpace().GUID)
    63  	cmd.UI.DisplayWarnings(warnings)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	if len(instanceSummaries) == 0 {
    69  		cmd.UI.DisplayText("No services found")
    70  		return nil
    71  	}
    72  
    73  	table := [][]string{{
    74  		cmd.UI.TranslateText("name"),
    75  		cmd.UI.TranslateText("service"),
    76  		cmd.UI.TranslateText("plan"),
    77  		cmd.UI.TranslateText("bound apps"),
    78  		cmd.UI.TranslateText("last operation"),
    79  	}}
    80  
    81  	var boundAppNames []string
    82  
    83  	for _, summary := range instanceSummaries {
    84  		serviceLabel := summary.Service.Label
    85  		if summary.ServiceInstance.Type == constant.ServiceInstanceTypeUserProvidedService {
    86  			serviceLabel = "user-provided"
    87  		}
    88  
    89  		boundAppNames = []string{}
    90  		for _, boundApplication := range summary.BoundApplications {
    91  			boundAppNames = append(boundAppNames, boundApplication.AppName)
    92  		}
    93  
    94  		table = append(table, []string{
    95  			summary.Name,
    96  			serviceLabel,
    97  			summary.ServicePlan.Name,
    98  			strings.Join(boundAppNames, ", "),
    99  			fmt.Sprintf("%s %s", summary.LastOperation.Type, summary.LastOperation.State)},
   100  		)
   101  	}
   102  	cmd.UI.DisplayTableWithHeader("", table, 3)
   103  
   104  	return nil
   105  }