github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/application/rename.go (about)

     1  package application
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/applications"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type RenameApp struct {
    17  	ui      terminal.UI
    18  	config  coreconfig.Reader
    19  	appRepo applications.Repository
    20  	appReq  requirements.ApplicationRequirement
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&RenameApp{})
    25  }
    26  
    27  func (cmd *RenameApp) MetaData() commandregistry.CommandMetadata {
    28  	return commandregistry.CommandMetadata{
    29  		Name:        "rename",
    30  		Description: T("Rename an app"),
    31  		Usage: []string{
    32  			T("CF_NAME rename APP_NAME NEW_APP_NAME"),
    33  		},
    34  	}
    35  }
    36  
    37  func (cmd *RenameApp) Requirements(requirementsFactory requirements.Factory, c flags.FlagContext) ([]requirements.Requirement, error) {
    38  	if len(c.Args()) != 2 {
    39  		cmd.ui.Failed(T("Incorrect Usage. Requires old app name and new app name as arguments\n\n") + commandregistry.Commands.CommandUsage("rename"))
    40  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(c.Args()), 2)
    41  	}
    42  
    43  	cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0])
    44  
    45  	reqs := []requirements.Requirement{
    46  		requirementsFactory.NewLoginRequirement(),
    47  		requirementsFactory.NewTargetedSpaceRequirement(),
    48  		cmd.appReq,
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *RenameApp) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.appRepo = deps.RepoLocator.GetApplicationRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *RenameApp) Execute(c flags.FlagContext) error {
    62  	app := cmd.appReq.GetApplication()
    63  	newName := c.Args()[1]
    64  
    65  	cmd.ui.Say(T("Renaming app {{.AppName}} to {{.NewName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
    66  		map[string]interface{}{
    67  			"AppName":   terminal.EntityNameColor(app.Name),
    68  			"NewName":   terminal.EntityNameColor(newName),
    69  			"OrgName":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    70  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    71  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    72  
    73  	params := models.AppParams{Name: &newName}
    74  
    75  	_, err := cmd.appRepo.Update(app.GUID, params)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	cmd.ui.Ok()
    80  	return err
    81  }