github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/apps.go (about)

     1  package application
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/flags"
     9  	. "code.cloudfoundry.org/cli/cf/i18n"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/plugin/models"
    12  
    13  	"code.cloudfoundry.org/cli/cf/api"
    14  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    15  	"code.cloudfoundry.org/cli/cf/formatters"
    16  	"code.cloudfoundry.org/cli/cf/requirements"
    17  	"code.cloudfoundry.org/cli/cf/terminal"
    18  	"code.cloudfoundry.org/cli/cf/uihelpers"
    19  )
    20  
    21  type ListApps struct {
    22  	ui             terminal.UI
    23  	config         coreconfig.Reader
    24  	appSummaryRepo api.AppSummaryRepository
    25  
    26  	pluginAppModels *[]plugin_models.GetAppsModel
    27  	pluginCall      bool
    28  }
    29  
    30  func init() {
    31  	commandregistry.Register(&ListApps{})
    32  }
    33  
    34  func (cmd *ListApps) MetaData() commandregistry.CommandMetadata {
    35  	return commandregistry.CommandMetadata{
    36  		Name:        "apps",
    37  		ShortName:   "a",
    38  		Description: T("List all apps in the target space"),
    39  		Usage: []string{
    40  			"CF_NAME apps",
    41  		},
    42  	}
    43  }
    44  
    45  func (cmd *ListApps) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    46  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    47  		T("No argument required"),
    48  		func() bool {
    49  			return len(fc.Args()) != 0
    50  		},
    51  	)
    52  
    53  	reqs := []requirements.Requirement{
    54  		usageReq,
    55  		requirementsFactory.NewLoginRequirement(),
    56  		requirementsFactory.NewTargetedSpaceRequirement(),
    57  	}
    58  
    59  	return reqs, nil
    60  }
    61  
    62  func (cmd *ListApps) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    63  	cmd.ui = deps.UI
    64  	cmd.config = deps.Config
    65  	cmd.appSummaryRepo = deps.RepoLocator.GetAppSummaryRepository()
    66  	cmd.pluginAppModels = deps.PluginModels.AppsSummary
    67  	cmd.pluginCall = pluginCall
    68  	return cmd
    69  }
    70  
    71  func (cmd *ListApps) Execute(c flags.FlagContext) error {
    72  	cmd.ui.Say(T("Getting apps in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
    73  		map[string]interface{}{
    74  			"OrgName":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    75  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    76  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    77  
    78  	apps, err := cmd.appSummaryRepo.GetSummariesInCurrentSpace()
    79  
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	cmd.ui.Ok()
    85  	cmd.ui.Say("")
    86  
    87  	if len(apps) == 0 {
    88  		cmd.ui.Say(T("No apps found"))
    89  		return nil
    90  	}
    91  
    92  	table := cmd.ui.Table([]string{
    93  		T("name"),
    94  		T("requested state"),
    95  		T("instances"),
    96  		T("memory"),
    97  		T("disk"),
    98  		// Hide this column #117189491
    99  		// T("app ports"),
   100  		T("urls"),
   101  	})
   102  
   103  	for _, application := range apps {
   104  		var urls []string
   105  		for _, route := range application.Routes {
   106  			urls = append(urls, route.URL())
   107  		}
   108  
   109  		appPorts := make([]string, len(application.AppPorts))
   110  		for i, p := range application.AppPorts {
   111  			appPorts[i] = strconv.Itoa(p)
   112  		}
   113  
   114  		table.Add(
   115  			application.Name,
   116  			uihelpers.ColoredAppState(application.ApplicationFields),
   117  			uihelpers.ColoredAppInstances(application.ApplicationFields),
   118  			formatters.ByteSize(application.Memory*formatters.MEGABYTE),
   119  			formatters.ByteSize(application.DiskQuota*formatters.MEGABYTE),
   120  			// Hide this column #117189491
   121  			// strings.Join(appPorts, ", "),
   122  			strings.Join(urls, ", "),
   123  		)
   124  	}
   125  
   126  	err = table.Print()
   127  	if err != nil {
   128  		return err
   129  	}
   130  	if cmd.pluginCall {
   131  		cmd.populatePluginModel(apps)
   132  	}
   133  	return nil
   134  }
   135  
   136  func (cmd *ListApps) populatePluginModel(apps []models.Application) {
   137  	for _, app := range apps {
   138  		appModel := plugin_models.GetAppsModel{}
   139  		appModel.Name = app.Name
   140  		appModel.Guid = app.GUID
   141  		appModel.TotalInstances = app.InstanceCount
   142  		appModel.RunningInstances = app.RunningInstances
   143  		appModel.Memory = app.Memory
   144  		appModel.State = app.State
   145  		appModel.DiskQuota = app.DiskQuota
   146  		appModel.AppPorts = app.AppPorts
   147  
   148  		*(cmd.pluginAppModels) = append(*(cmd.pluginAppModels), appModel)
   149  
   150  		for _, route := range app.Routes {
   151  			r := plugin_models.GetAppsRouteSummary{}
   152  			r.Host = route.Host
   153  			r.Guid = route.GUID
   154  			r.Domain.Guid = route.Domain.GUID
   155  			r.Domain.Name = route.Domain.Name
   156  			r.Domain.OwningOrganizationGuid = route.Domain.OwningOrganizationGUID
   157  			r.Domain.Shared = route.Domain.Shared
   158  
   159  			(*(cmd.pluginAppModels))[len(*(cmd.pluginAppModels))-1].Routes = append((*(cmd.pluginAppModels))[len(*(cmd.pluginAppModels))-1].Routes, r)
   160  		}
   161  
   162  	}
   163  }