github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/buildpack/delete_buildpack.go (about)

     1  package buildpack
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/errors"
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	. "code.cloudfoundry.org/cli/cf/i18n"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/terminal"
    11  )
    12  
    13  type DeleteBuildpack struct {
    14  	ui            terminal.UI
    15  	buildpackRepo api.BuildpackRepository
    16  }
    17  
    18  func init() {
    19  	commandregistry.Register(&DeleteBuildpack{})
    20  }
    21  
    22  func (cmd *DeleteBuildpack) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    23  	cmd.ui = deps.UI
    24  	cmd.buildpackRepo = deps.RepoLocator.GetBuildpackRepository()
    25  	return cmd
    26  }
    27  
    28  func (cmd *DeleteBuildpack) MetaData() commandregistry.CommandMetadata {
    29  	fs := make(map[string]flags.FlagSet)
    30  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    31  
    32  	return commandregistry.CommandMetadata{
    33  		Name:        "delete-buildpack",
    34  		Description: T("Delete a buildpack"),
    35  		Usage: []string{
    36  			T("CF_NAME delete-buildpack BUILDPACK [-f]"),
    37  		},
    38  		Flags: fs,
    39  	}
    40  }
    41  
    42  func (cmd *DeleteBuildpack) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    43  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), "",
    44  		func() bool {
    45  			return len(fc.Args()) != 1
    46  		},
    47  	)
    48  
    49  	loginReq := requirementsFactory.NewLoginRequirement()
    50  
    51  	reqs := []requirements.Requirement{
    52  		usageReq,
    53  		loginReq,
    54  	}
    55  
    56  	return reqs, nil
    57  }
    58  
    59  func (cmd *DeleteBuildpack) Execute(c flags.FlagContext) error {
    60  	buildpackName := c.Args()[0]
    61  
    62  	force := c.Bool("f")
    63  
    64  	if !force {
    65  		answer := cmd.ui.ConfirmDelete("buildpack", buildpackName)
    66  		if !answer {
    67  			return nil
    68  		}
    69  	}
    70  
    71  	cmd.ui.Say(T("Deleting buildpack {{.BuildpackName}}...", map[string]interface{}{"BuildpackName": terminal.EntityNameColor(buildpackName)}))
    72  	buildpack, err := cmd.buildpackRepo.FindByName(buildpackName)
    73  
    74  	switch err.(type) {
    75  	case nil: //do nothing
    76  	case *errors.ModelNotFoundError:
    77  		cmd.ui.Ok()
    78  		cmd.ui.Warn(T("Buildpack {{.BuildpackName}} does not exist.", map[string]interface{}{"BuildpackName": buildpackName}))
    79  		return nil
    80  	default:
    81  		return err
    82  
    83  	}
    84  
    85  	err = cmd.buildpackRepo.Delete(buildpack.GUID)
    86  	if err != nil {
    87  		return errors.New(T("Error deleting buildpack {{.Name}}\n{{.Error}}", map[string]interface{}{
    88  			"Name":  terminal.EntityNameColor(buildpack.Name),
    89  			"Error": err.Error(),
    90  		}))
    91  	}
    92  
    93  	cmd.ui.Ok()
    94  	return nil
    95  }