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

     1  package quota
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	. "github.com/cloudfoundry/cli/cf/i18n"
     8  	"github.com/cloudfoundry/cli/flags"
     9  
    10  	"github.com/cloudfoundry/cli/cf/api/quotas"
    11  	"github.com/cloudfoundry/cli/cf/command_registry"
    12  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    13  	"github.com/cloudfoundry/cli/cf/formatters"
    14  	"github.com/cloudfoundry/cli/cf/requirements"
    15  	"github.com/cloudfoundry/cli/cf/terminal"
    16  )
    17  
    18  type ListQuotas struct {
    19  	ui        terminal.UI
    20  	config    core_config.Reader
    21  	quotaRepo quotas.QuotaRepository
    22  }
    23  
    24  func init() {
    25  	command_registry.Register(&ListQuotas{})
    26  }
    27  
    28  func (cmd *ListQuotas) MetaData() command_registry.CommandMetadata {
    29  	return command_registry.CommandMetadata{
    30  		Name:        "quotas",
    31  		Description: T("List available usage quotas"),
    32  		Usage:       T("CF_NAME quotas"),
    33  	}
    34  }
    35  
    36  func (cmd *ListQuotas) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    37  	if len(fc.Args()) != 0 {
    38  		cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("quotas"))
    39  	}
    40  
    41  	reqs = []requirements.Requirement{
    42  		requirementsFactory.NewLoginRequirement(),
    43  	}
    44  	return
    45  }
    46  
    47  func (cmd *ListQuotas) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    48  	cmd.ui = deps.Ui
    49  	cmd.config = deps.Config
    50  	cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository()
    51  	return cmd
    52  }
    53  
    54  func (cmd *ListQuotas) Execute(c flags.FlagContext) {
    55  	cmd.ui.Say(T("Getting quotas as {{.Username}}...", map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())}))
    56  
    57  	quotas, apiErr := cmd.quotaRepo.FindAll()
    58  
    59  	if apiErr != nil {
    60  		cmd.ui.Failed(apiErr.Error())
    61  		return
    62  	}
    63  	cmd.ui.Ok()
    64  	cmd.ui.Say("")
    65  
    66  	table := terminal.NewTable(cmd.ui, []string{T("name"), T("total memory limit"), T("instance memory limit"), T("routes"), T("service instances"), T("paid service plans")})
    67  
    68  	var megabytes string
    69  	for _, quota := range quotas {
    70  		if quota.InstanceMemoryLimit == -1 {
    71  			megabytes = T("unlimited")
    72  		} else {
    73  			megabytes = formatters.ByteSize(quota.InstanceMemoryLimit * formatters.MEGABYTE)
    74  		}
    75  
    76  		servicesLimit := strconv.Itoa(quota.ServicesLimit)
    77  		if quota.ServicesLimit == -1 {
    78  			servicesLimit = T("unlimited")
    79  		}
    80  
    81  		table.Add(
    82  			quota.Name,
    83  			formatters.ByteSize(quota.MemoryLimit*formatters.MEGABYTE),
    84  			megabytes,
    85  			fmt.Sprintf("%d", quota.RoutesLimit),
    86  			fmt.Sprintf(servicesLimit),
    87  			formatters.Allowed(quota.NonBasicServicesAllowed),
    88  		)
    89  	}
    90  
    91  	table.Print()
    92  }