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

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