github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/v3apps.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	"code.cloudfoundry.org/cli/cf/formatters"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  	"code.cloudfoundry.org/cli/cf/v3/models"
    14  	"code.cloudfoundry.org/cli/cf/v3/repository"
    15  
    16  	. "code.cloudfoundry.org/cli/cf/i18n"
    17  )
    18  
    19  type V3Apps struct {
    20  	ui         terminal.UI
    21  	config     coreconfig.ReadWriter
    22  	repository repository.Repository
    23  }
    24  
    25  func init() {
    26  	commandregistry.Register(&V3Apps{})
    27  }
    28  
    29  func (c *V3Apps) MetaData() commandregistry.CommandMetadata {
    30  	return commandregistry.CommandMetadata{
    31  		Name:        "v3apps",
    32  		Description: T("List all apps in the target space"),
    33  		Usage: []string{
    34  			"CF_NAME v3apps",
    35  		},
    36  		Hidden: true,
    37  	}
    38  }
    39  
    40  func (c *V3Apps) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    41  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(c),
    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 (c *V3Apps) SetDependency(deps commandregistry.Dependency, _ bool) commandregistry.Command {
    58  	c.ui = deps.UI
    59  	c.config = deps.Config
    60  	c.repository = deps.RepoLocator.GetV3Repository()
    61  
    62  	return c
    63  }
    64  
    65  func (c *V3Apps) Execute(fc flags.FlagContext) error {
    66  	applications, err := c.repository.GetApplications()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	processes := make([][]models.V3Process, len(applications))
    72  	routes := make([][]models.V3Route, len(applications))
    73  
    74  	for i, app := range applications {
    75  		ps, apiErr := c.repository.GetProcesses(app.Links.Processes.Href)
    76  		if apiErr != nil {
    77  			return apiErr
    78  		}
    79  		processes[i] = ps
    80  
    81  		rs, apiErr := c.repository.GetRoutes(app.Links.Routes.Href)
    82  		if apiErr != nil {
    83  			return apiErr
    84  		}
    85  		routes[i] = rs
    86  	}
    87  
    88  	table := c.ui.Table([]string{T("name"), T("requested state"), T("instances"), T("memory"), T("disk"), T("urls")})
    89  
    90  	for i := range applications {
    91  		c.addRow(table, applications[i], processes[i], routes[i])
    92  	}
    93  
    94  	err = table.Print()
    95  	if err != nil {
    96  		return err
    97  	}
    98  	return nil
    99  }
   100  
   101  type table interface {
   102  	Add(row ...string)
   103  	Print() error
   104  }
   105  
   106  func (c *V3Apps) addRow(
   107  	table table,
   108  	application models.V3Application,
   109  	processes []models.V3Process,
   110  	routes []models.V3Route,
   111  ) {
   112  	var webProcess models.V3Process
   113  	for i := range processes {
   114  		if processes[i].Type == "web" {
   115  			webProcess = processes[i]
   116  		}
   117  	}
   118  
   119  	var appRoutes []string
   120  	for _, route := range routes {
   121  		appRoutes = append(appRoutes, route.Host+route.Path)
   122  	}
   123  
   124  	table.Add(
   125  		application.Name,
   126  		strings.ToLower(application.DesiredState),
   127  		fmt.Sprintf("%d", application.TotalDesiredInstances),
   128  		formatters.ByteSize(webProcess.MemoryInMB*formatters.MEGABYTE),
   129  		formatters.ByteSize(webProcess.DiskInMB*formatters.MEGABYTE),
   130  		strings.Join(appRoutes, ", "),
   131  	)
   132  }