github.com/Liam-Williams/i18n4go@v0.2.7-0.20201028180611-670cbaceaa6b/test_fixtures/extract_strings/d_option/input_files/quota/create_quota.go (about)

     1  package quota
     2  
     3  import ()
     4  
     5  type CreateQuota struct {
     6  	ui        terminal.UI
     7  	config    configuration.Reader
     8  	quotaRepo api.QuotaRepository
     9  }
    10  
    11  func NewCreateQuota(ui terminal.UI, config configuration.Reader, quotaRepo api.QuotaRepository) CreateQuota {
    12  	return CreateQuota{
    13  		ui:        ui,
    14  		config:    config,
    15  		quotaRepo: quotaRepo,
    16  	}
    17  }
    18  
    19  func (command CreateQuota) Metadata() command_metadata.CommandMetadata {
    20  	return command_metadata.CommandMetadata{
    21  		Name:        "create-quota",
    22  		Description: "Define a new resource quota",
    23  		Usage:       "CF_NAME create-quota QUOTA [-m MEMORY] [-r ROUTES] [-s SERVICE_INSTANCES] [--allow-paid-service-plans]",
    24  		Flags: []cli.Flag{
    25  			flag_helpers.NewStringFlag("m", "Total amount of memory (e.g. 1024M, 1G, 10G)"),
    26  			flag_helpers.NewIntFlag("r", "Total number of routes"),
    27  			flag_helpers.NewIntFlag("s", "Total number of service instances"),
    28  			cli.BoolFlag{Name: "allow-paid-service-plans", Usage: "Can provision instances of paid service plans"},
    29  		},
    30  	}
    31  }
    32  
    33  func (cmd CreateQuota) GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) ([]requirements.Requirement, error) {
    34  	if len(context.Args()) != 1 {
    35  		cmd.ui.FailWithUsage(context, "create-quota")
    36  	}
    37  
    38  	return []requirements.Requirement{
    39  		requirementsFactory.NewLoginRequirement(),
    40  	}, nil
    41  }
    42  
    43  func (cmd CreateQuota) Run(context *cli.Context) {
    44  	name := context.Args()[0]
    45  
    46  	cmd.ui.Say("Creating quota %s as %s...",
    47  		terminal.EntityNameColor(name),
    48  		terminal.EntityNameColor(cmd.config.Username()))
    49  
    50  	quota := models.QuotaFields{
    51  		Name: name,
    52  	}
    53  
    54  	memoryLimit := context.String("m")
    55  	if memoryLimit != "" {
    56  		parsedMemory, err := formatters.ToMegabytes(memoryLimit)
    57  		if err != nil {
    58  			cmd.ui.Failed("Invalid memory limit: %s\n%s", memoryLimit, err)
    59  		}
    60  
    61  		quota.MemoryLimit = parsedMemory
    62  	}
    63  
    64  	if context.IsSet("r") {
    65  		quota.RoutesLimit = context.Int("r")
    66  	}
    67  
    68  	if context.IsSet("s") {
    69  		quota.ServicesLimit = context.Int("s")
    70  	}
    71  
    72  	if context.IsSet("allow-paid-service-plans") {
    73  		quota.NonBasicServicesAllowed = true
    74  	}
    75  
    76  	err := cmd.quotaRepo.Create(quota)
    77  
    78  	httpErr, ok := err.(errors.HttpError)
    79  	if ok && httpErr.ErrorCode() == errors.QUOTA_EXISTS {
    80  		cmd.ui.Ok()
    81  		cmd.ui.Warn("Quota Definition %s already exists", quota.Name)
    82  		return
    83  	}
    84  
    85  	if err != nil {
    86  		cmd.ui.Failed(err.Error())
    87  	}
    88  
    89  	cmd.ui.Ok()
    90  }