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

     1  package buildpack
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  )
    14  
    15  type RenameBuildpack struct {
    16  	ui            terminal.UI
    17  	buildpackRepo api.BuildpackRepository
    18  }
    19  
    20  func init() {
    21  	commandregistry.Register(&RenameBuildpack{})
    22  }
    23  
    24  func (cmd *RenameBuildpack) MetaData() commandregistry.CommandMetadata {
    25  	return commandregistry.CommandMetadata{
    26  		Name:        "rename-buildpack",
    27  		Description: T("Rename a buildpack"),
    28  		Usage: []string{
    29  			T("CF_NAME rename-buildpack BUILDPACK_NAME NEW_BUILDPACK_NAME"),
    30  		},
    31  	}
    32  }
    33  
    34  func (cmd *RenameBuildpack) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    35  	if len(fc.Args()) != 2 {
    36  		cmd.ui.Failed(T("Incorrect Usage. Requires BUILDPACK_NAME, NEW_BUILDPACK_NAME as arguments\n\n") + commandregistry.Commands.CommandUsage("rename-buildpack"))
    37  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    38  	}
    39  
    40  	reqs := []requirements.Requirement{
    41  		requirementsFactory.NewLoginRequirement(),
    42  	}
    43  
    44  	return reqs, nil
    45  }
    46  
    47  func (cmd *RenameBuildpack) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    48  	cmd.ui = deps.UI
    49  	cmd.buildpackRepo = deps.RepoLocator.GetBuildpackRepository()
    50  	return cmd
    51  }
    52  
    53  func (cmd *RenameBuildpack) Execute(c flags.FlagContext) error {
    54  	buildpackName := c.Args()[0]
    55  	newBuildpackName := c.Args()[1]
    56  
    57  	cmd.ui.Say(T("Renaming buildpack {{.OldBuildpackName}} to {{.NewBuildpackName}}...", map[string]interface{}{"OldBuildpackName": terminal.EntityNameColor(buildpackName), "NewBuildpackName": terminal.EntityNameColor(newBuildpackName)}))
    58  
    59  	buildpack, err := cmd.buildpackRepo.FindByName(buildpackName)
    60  
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	buildpack.Name = newBuildpackName
    66  	buildpack, err = cmd.buildpackRepo.Update(buildpack)
    67  	if err != nil {
    68  		return errors.New(T("Error renaming buildpack {{.Name}}\n{{.Error}}", map[string]interface{}{
    69  			"Name":  terminal.EntityNameColor(buildpack.Name),
    70  			"Error": err.Error(),
    71  		}))
    72  	}
    73  
    74  	cmd.ui.Ok()
    75  	return nil
    76  }