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

     1  package service
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	. "github.com/cloudfoundry/cli/cf/i18n"
     8  	"github.com/cloudfoundry/cli/flags"
     9  	"github.com/cloudfoundry/cli/plugin/models"
    10  
    11  	"github.com/cloudfoundry/cli/cf/api"
    12  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    13  	"github.com/cloudfoundry/cli/cf/requirements"
    14  	"github.com/cloudfoundry/cli/cf/terminal"
    15  )
    16  
    17  type ListServices struct {
    18  	ui                 terminal.UI
    19  	config             core_config.Reader
    20  	serviceSummaryRepo api.ServiceSummaryRepository
    21  	pluginModel        *[]plugin_models.GetServices_Model
    22  	pluginCall         bool
    23  }
    24  
    25  func init() {
    26  	command_registry.Register(&ListServices{})
    27  }
    28  
    29  func (cmd ListServices) MetaData() command_registry.CommandMetadata {
    30  	return command_registry.CommandMetadata{
    31  		Name:        "services",
    32  		ShortName:   "s",
    33  		Description: T("List all service instances in the target space"),
    34  		Usage:       "CF_NAME services",
    35  	}
    36  }
    37  
    38  func (cmd ListServices) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    39  	if len(fc.Args()) != 0 {
    40  		cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("services"))
    41  	}
    42  	reqs = append(reqs,
    43  		requirementsFactory.NewLoginRequirement(),
    44  		requirementsFactory.NewTargetedSpaceRequirement(),
    45  	)
    46  	return
    47  }
    48  
    49  func (cmd *ListServices) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    50  	cmd.ui = deps.Ui
    51  	cmd.config = deps.Config
    52  	cmd.serviceSummaryRepo = deps.RepoLocator.GetServiceSummaryRepository()
    53  	cmd.pluginModel = deps.PluginModels.Services
    54  	cmd.pluginCall = pluginCall
    55  	return cmd
    56  }
    57  
    58  func (cmd ListServices) Execute(fc flags.FlagContext) {
    59  	cmd.ui.Say(T("Getting services in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    60  		map[string]interface{}{
    61  			"OrgName":     terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    62  			"SpaceName":   terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    63  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    64  		}))
    65  
    66  	serviceInstances, apiErr := cmd.serviceSummaryRepo.GetSummariesInCurrentSpace()
    67  
    68  	if apiErr != nil {
    69  		cmd.ui.Failed(apiErr.Error())
    70  		return
    71  	}
    72  
    73  	cmd.ui.Ok()
    74  	cmd.ui.Say("")
    75  
    76  	if len(serviceInstances) == 0 {
    77  		cmd.ui.Say(T("No services found"))
    78  		return
    79  	}
    80  
    81  	table := terminal.NewTable(cmd.ui, []string{T("name"), T("service"), T("plan"), T("bound apps"), T("last operation")})
    82  
    83  	for _, instance := range serviceInstances {
    84  		var serviceColumn string
    85  		var serviceStatus string
    86  
    87  		if instance.IsUserProvided() {
    88  			serviceColumn = T("user-provided")
    89  		} else {
    90  			serviceColumn = instance.ServiceOffering.Label
    91  		}
    92  		serviceStatus = ServiceInstanceStateToStatus(instance.LastOperation.Type, instance.LastOperation.State, instance.IsUserProvided())
    93  
    94  		table.Add(
    95  			instance.Name,
    96  			serviceColumn,
    97  			instance.ServicePlan.Name,
    98  			strings.Join(instance.ApplicationNames, ", "),
    99  			serviceStatus,
   100  		)
   101  		if cmd.pluginCall {
   102  			s := plugin_models.GetServices_Model{
   103  				Name: instance.Name,
   104  				Guid: instance.Guid,
   105  				ServicePlan: plugin_models.GetServices_ServicePlan{
   106  					Name: instance.ServicePlan.Name,
   107  					Guid: instance.ServicePlan.Guid,
   108  				},
   109  				Service: plugin_models.GetServices_ServiceFields{
   110  					Name: instance.ServiceOffering.Label,
   111  				},
   112  				ApplicationNames: instance.ApplicationNames,
   113  				LastOperation: plugin_models.GetServices_LastOperation{
   114  					Type:  instance.LastOperation.Type,
   115  					State: instance.LastOperation.State,
   116  				},
   117  				IsUserProvided: instance.IsUserProvided(),
   118  			}
   119  
   120  			*(cmd.pluginModel) = append(*(cmd.pluginModel), s)
   121  		}
   122  
   123  	}
   124  
   125  	table.Print()
   126  }