github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/space/rename_space.go (about) 1 package space 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/api/spaces" 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/requirements" 12 "code.cloudfoundry.org/cli/cf/terminal" 13 ) 14 15 type RenameSpace struct { 16 ui terminal.UI 17 config coreconfig.ReadWriter 18 spaceRepo spaces.SpaceRepository 19 spaceReq requirements.SpaceRequirement 20 } 21 22 func init() { 23 commandregistry.Register(&RenameSpace{}) 24 } 25 26 func (cmd *RenameSpace) MetaData() commandregistry.CommandMetadata { 27 return commandregistry.CommandMetadata{ 28 Name: "rename-space", 29 Description: T("Rename a space"), 30 Usage: []string{ 31 T("CF_NAME rename-space SPACE NEW_SPACE"), 32 }, 33 } 34 } 35 36 func (cmd *RenameSpace) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 37 if len(fc.Args()) != 2 { 38 cmd.ui.Failed(T("Incorrect Usage. Requires SPACE_NAME NEW_SPACE_NAME as arguments\n\n") + commandregistry.Commands.CommandUsage("rename-space")) 39 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 40 } 41 42 cmd.spaceReq = requirementsFactory.NewSpaceRequirement(fc.Args()[0]) 43 44 reqs := []requirements.Requirement{ 45 requirementsFactory.NewLoginRequirement(), 46 requirementsFactory.NewTargetedOrgRequirement(), 47 cmd.spaceReq, 48 } 49 50 return reqs, nil 51 } 52 53 func (cmd *RenameSpace) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 54 cmd.ui = deps.UI 55 cmd.config = deps.Config 56 cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository() 57 return cmd 58 } 59 60 func (cmd *RenameSpace) Execute(c flags.FlagContext) error { 61 space := cmd.spaceReq.GetSpace() 62 newName := c.Args()[1] 63 cmd.ui.Say(T("Renaming space {{.OldSpaceName}} to {{.NewSpaceName}} in org {{.OrgName}} as {{.CurrentUser}}...", 64 map[string]interface{}{ 65 "OldSpaceName": terminal.EntityNameColor(space.Name), 66 "NewSpaceName": terminal.EntityNameColor(newName), 67 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 68 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 69 })) 70 71 err := cmd.spaceRepo.Rename(space.GUID, newName) 72 if err != nil { 73 return err 74 } 75 76 if cmd.config.SpaceFields().GUID == space.GUID { 77 space.Name = newName 78 cmd.config.SetSpaceFields(space.SpaceFields) 79 } 80 81 cmd.ui.Ok() 82 return err 83 }