github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/service/services.go (about)

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