github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/space/rename_space.go (about)

     1  package space
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/spaces"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	. "github.com/cloudfoundry/cli/cf/i18n"
     8  	"github.com/cloudfoundry/cli/cf/requirements"
     9  	"github.com/cloudfoundry/cli/cf/terminal"
    10  	"github.com/cloudfoundry/cli/flags"
    11  )
    12  
    13  type RenameSpace struct {
    14  	ui        terminal.UI
    15  	config    core_config.ReadWriter
    16  	spaceRepo spaces.SpaceRepository
    17  	spaceReq  requirements.SpaceRequirement
    18  }
    19  
    20  func init() {
    21  	command_registry.Register(&RenameSpace{})
    22  }
    23  
    24  func (cmd *RenameSpace) MetaData() command_registry.CommandMetadata {
    25  	return command_registry.CommandMetadata{
    26  		Name:        "rename-space",
    27  		Description: T("Rename a space"),
    28  		Usage:       T("CF_NAME rename-space SPACE NEW_SPACE"),
    29  	}
    30  }
    31  
    32  func (cmd *RenameSpace) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    33  	if len(fc.Args()) != 2 {
    34  		cmd.ui.Failed(T("Incorrect Usage. Requires SPACE_NAME NEW_SPACE_NAME as arguments\n\n") + command_registry.Commands.CommandUsage("rename-space"))
    35  	}
    36  
    37  	cmd.spaceReq = requirementsFactory.NewSpaceRequirement(fc.Args()[0])
    38  	reqs = []requirements.Requirement{
    39  		requirementsFactory.NewLoginRequirement(),
    40  		requirementsFactory.NewTargetedOrgRequirement(),
    41  		cmd.spaceReq,
    42  	}
    43  	return
    44  }
    45  
    46  func (cmd *RenameSpace) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    47  	cmd.ui = deps.Ui
    48  	cmd.config = deps.Config
    49  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    50  	return cmd
    51  }
    52  
    53  func (cmd *RenameSpace) Execute(c flags.FlagContext) {
    54  	space := cmd.spaceReq.GetSpace()
    55  	newName := c.Args()[1]
    56  	cmd.ui.Say(T("Renaming space {{.OldSpaceName}} to {{.NewSpaceName}} in org {{.OrgName}} as {{.CurrentUser}}...",
    57  		map[string]interface{}{
    58  			"OldSpaceName": terminal.EntityNameColor(space.Name),
    59  			"NewSpaceName": terminal.EntityNameColor(newName),
    60  			"OrgName":      terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    61  			"CurrentUser":  terminal.EntityNameColor(cmd.config.Username()),
    62  		}))
    63  
    64  	apiErr := cmd.spaceRepo.Rename(space.Guid, newName)
    65  	if apiErr != nil {
    66  		cmd.ui.Failed(apiErr.Error())
    67  		return
    68  	}
    69  
    70  	if cmd.config.SpaceFields().Guid == space.Guid {
    71  		space.Name = newName
    72  		cmd.config.SetSpaceFields(space.SpaceFields)
    73  	}
    74  
    75  	cmd.ui.Ok()
    76  }