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

     1  package quota
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/quotas"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type DeleteQuota struct {
    17  	ui        terminal.UI
    18  	config    coreconfig.Reader
    19  	quotaRepo quotas.QuotaRepository
    20  	orgReq    requirements.OrganizationRequirement
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&DeleteQuota{})
    25  }
    26  
    27  func (cmd *DeleteQuota) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    30  
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "delete-quota",
    33  		Description: T("Delete a quota"),
    34  		Usage: []string{
    35  			T("CF_NAME delete-quota QUOTA [-f]"),
    36  		},
    37  		Flags: fs,
    38  	}
    39  }
    40  
    41  func (cmd *DeleteQuota) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    42  	if len(fc.Args()) != 1 {
    43  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-quota"))
    44  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    45  	}
    46  
    47  	reqs := []requirements.Requirement{
    48  		requirementsFactory.NewLoginRequirement(),
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *DeleteQuota) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.quotaRepo = deps.RepoLocator.GetQuotaRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *DeleteQuota) Execute(c flags.FlagContext) error {
    62  	quotaName := c.Args()[0]
    63  
    64  	if !c.Bool("f") {
    65  		response := cmd.ui.ConfirmDelete("quota", quotaName)
    66  		if !response {
    67  			return nil
    68  		}
    69  	}
    70  
    71  	cmd.ui.Say(T("Deleting quota {{.QuotaName}} as {{.Username}}...", map[string]interface{}{
    72  		"QuotaName": terminal.EntityNameColor(quotaName),
    73  		"Username":  terminal.EntityNameColor(cmd.config.Username()),
    74  	}))
    75  
    76  	quota, err := cmd.quotaRepo.FindByName(quotaName)
    77  
    78  	switch (err).(type) {
    79  	case nil: // no error
    80  	case *errors.ModelNotFoundError:
    81  		cmd.ui.Ok()
    82  		cmd.ui.Warn(T("Quota {{.QuotaName}} does not exist", map[string]interface{}{"QuotaName": quotaName}))
    83  		return nil
    84  	default:
    85  		return err
    86  	}
    87  
    88  	err = cmd.quotaRepo.Delete(quota.GUID)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	cmd.ui.Ok()
    94  	return err
    95  }