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

     1  package spacequota
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/spacequotas"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/formatters"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type ListSpaceQuotas struct {
    18  	ui             terminal.UI
    19  	config         coreconfig.Reader
    20  	spaceQuotaRepo spacequotas.SpaceQuotaRepository
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&ListSpaceQuotas{})
    25  }
    26  
    27  func (cmd *ListSpaceQuotas) MetaData() commandregistry.CommandMetadata {
    28  	return commandregistry.CommandMetadata{
    29  		Name:        "space-quotas",
    30  		Description: T("List available space resource quotas"),
    31  		Usage: []string{
    32  			T("CF_NAME space-quotas"),
    33  		},
    34  	}
    35  }
    36  
    37  func (cmd *ListSpaceQuotas) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    38  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    39  		T("No argument required"),
    40  		func() bool {
    41  			return len(fc.Args()) != 0
    42  		},
    43  	)
    44  
    45  	reqs := []requirements.Requirement{
    46  		usageReq,
    47  		requirementsFactory.NewLoginRequirement(),
    48  		requirementsFactory.NewTargetedOrgRequirement(),
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *ListSpaceQuotas) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.spaceQuotaRepo = deps.RepoLocator.GetSpaceQuotaRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *ListSpaceQuotas) Execute(c flags.FlagContext) error {
    62  	cmd.ui.Say(T("Getting space quotas as {{.Username}}...", map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())}))
    63  
    64  	quotas, err := cmd.spaceQuotaRepo.FindByOrg(cmd.config.OrganizationFields().GUID)
    65  
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	cmd.ui.Ok()
    71  	cmd.ui.Say("")
    72  
    73  	table := cmd.ui.Table([]string{
    74  		T("name"),
    75  		T("total memory"),
    76  		T("instance memory"),
    77  		T("routes"),
    78  		T("service instances"),
    79  		T("paid plans"),
    80  		T("app instances"),
    81  		T("route ports"),
    82  	})
    83  
    84  	var megabytes string
    85  
    86  	for _, quota := range quotas {
    87  		if quota.InstanceMemoryLimit == -1 {
    88  			megabytes = T("unlimited")
    89  		} else {
    90  			megabytes = formatters.ByteSize(quota.InstanceMemoryLimit * formatters.MEGABYTE)
    91  		}
    92  
    93  		servicesLimit := strconv.Itoa(quota.ServicesLimit)
    94  		if servicesLimit == "-1" {
    95  			servicesLimit = T("unlimited")
    96  		}
    97  
    98  		table.Add(
    99  			quota.Name,
   100  			formatters.ByteSize(quota.MemoryLimit*formatters.MEGABYTE),
   101  			megabytes,
   102  			fmt.Sprintf("%d", quota.RoutesLimit),
   103  			T(quota.FormattedServicesLimit()),
   104  			formatters.Allowed(quota.NonBasicServicesAllowed),
   105  			T(quota.FormattedAppInstanceLimit()),
   106  			T(quota.FormattedRoutePortsLimit()),
   107  		)
   108  	}
   109  
   110  	err = table.Print()
   111  	if err != nil {
   112  		return err
   113  	}
   114  	return nil
   115  }