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

     1  package quota
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	. "code.cloudfoundry.org/cli/cf/i18n"
     9  
    10  	"code.cloudfoundry.org/cli/cf/api/quotas"
    11  	"code.cloudfoundry.org/cli/cf/api/resources"
    12  	"code.cloudfoundry.org/cli/cf/commandregistry"
    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  )
    18  
    19  type showQuota struct {
    20  	ui        terminal.UI
    21  	config    coreconfig.Reader
    22  	quotaRepo quotas.QuotaRepository
    23  }
    24  
    25  func init() {
    26  	commandregistry.Register(&showQuota{})
    27  }
    28  
    29  func (cmd *showQuota) MetaData() commandregistry.CommandMetadata {
    30  	return commandregistry.CommandMetadata{
    31  		Name: "quota",
    32  		Usage: []string{
    33  			T("CF_NAME quota QUOTA"),
    34  		},
    35  		Description: T("Show quota info"),
    36  	}
    37  }
    38  
    39  func (cmd *showQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    40  	if len(fc.Args()) != 1 {
    41  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("quota"))
    42  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    43  	}
    44  
    45  	reqs := []requirements.Requirement{
    46  		requirementsFactory.NewLoginRequirement(),
    47  	}
    48  
    49  	return reqs, nil
    50  }
    51  
    52  func (cmd *showQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    53  	cmd.ui = deps.UI
    54  	cmd.config = deps.Config
    55  	cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository()
    56  	return cmd
    57  }
    58  
    59  func (cmd *showQuota) Execute(c flags.FlagContext) error {
    60  	quotaName := c.Args()[0]
    61  	cmd.ui.Say(T("Getting quota {{.QuotaName}} info as {{.Username}}...", map[string]interface{}{"QuotaName": quotaName, "Username": cmd.config.Username()}))
    62  
    63  	quota, err := cmd.quotaRepo.FindByName(quotaName)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	cmd.ui.Ok()
    69  
    70  	var megabytes string
    71  	if quota.InstanceMemoryLimit == -1 {
    72  		megabytes = T("unlimited")
    73  	} else {
    74  		megabytes = formatters.ByteSize(quota.InstanceMemoryLimit * formatters.MEGABYTE)
    75  	}
    76  
    77  	servicesLimit := strconv.Itoa(quota.ServicesLimit)
    78  	if servicesLimit == "-1" {
    79  		servicesLimit = T("unlimited")
    80  	}
    81  
    82  	appInstanceLimit := strconv.Itoa(quota.AppInstanceLimit)
    83  	if quota.AppInstanceLimit == resources.UnlimitedAppInstances {
    84  		appInstanceLimit = T("unlimited")
    85  	}
    86  
    87  	reservedRoutePorts := string(quota.ReservedRoutePorts)
    88  	if reservedRoutePorts == resources.UnlimitedReservedRoutePorts {
    89  		reservedRoutePorts = T("unlimited")
    90  	}
    91  
    92  	table := cmd.ui.Table([]string{"", ""})
    93  	table.Add(T("Total Memory"), formatters.ByteSize(quota.MemoryLimit*formatters.MEGABYTE))
    94  	table.Add(T("Instance Memory"), megabytes)
    95  	table.Add(T("Routes"), fmt.Sprint(quota.RoutesLimit))
    96  	table.Add(T("Services"), servicesLimit)
    97  	table.Add(T("Paid service plans"), formatters.Allowed(quota.NonBasicServicesAllowed))
    98  	table.Add(T("App instance limit"), appInstanceLimit)
    99  	if reservedRoutePorts != "" {
   100  		table.Add(T("Reserved Route Ports"), reservedRoutePorts)
   101  	}
   102  	err = table.Print()
   103  	if err != nil {
   104  		return err
   105  	}
   106  	return nil
   107  }