github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/rename.go (about) 1 package application 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/api/applications" 5 "github.com/cloudfoundry/cli/cf/command_metadata" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 . "github.com/cloudfoundry/cli/cf/i18n" 8 "github.com/cloudfoundry/cli/cf/models" 9 "github.com/cloudfoundry/cli/cf/requirements" 10 "github.com/cloudfoundry/cli/cf/terminal" 11 "github.com/codegangsta/cli" 12 ) 13 14 type RenameApp struct { 15 ui terminal.UI 16 config core_config.Reader 17 appRepo applications.ApplicationRepository 18 appReq requirements.ApplicationRequirement 19 } 20 21 func NewRenameApp(ui terminal.UI, config core_config.Reader, appRepo applications.ApplicationRepository) (cmd *RenameApp) { 22 cmd = new(RenameApp) 23 cmd.ui = ui 24 cmd.config = config 25 cmd.appRepo = appRepo 26 return 27 } 28 29 func (cmd *RenameApp) Metadata() command_metadata.CommandMetadata { 30 return command_metadata.CommandMetadata{ 31 Name: "rename", 32 Description: T("Rename an app"), 33 Usage: T("CF_NAME rename APP_NAME NEW_APP_NAME"), 34 } 35 } 36 37 func (cmd *RenameApp) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) { 38 if len(c.Args()) != 2 { 39 cmd.ui.FailWithUsage(c) 40 } 41 42 cmd.appReq = requirementsFactory.NewApplicationRequirement(c.Args()[0]) 43 reqs = []requirements.Requirement{ 44 requirementsFactory.NewLoginRequirement(), 45 cmd.appReq, 46 } 47 return 48 } 49 50 func (cmd *RenameApp) Run(c *cli.Context) { 51 app := cmd.appReq.GetApplication() 52 newName := c.Args()[1] 53 54 cmd.ui.Say(T("Renaming app {{.AppName}} to {{.NewName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", 55 map[string]interface{}{ 56 "AppName": terminal.EntityNameColor(app.Name), 57 "NewName": terminal.EntityNameColor(newName), 58 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 59 "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), 60 "Username": terminal.EntityNameColor(cmd.config.Username())})) 61 62 params := models.AppParams{Name: &newName} 63 64 _, apiErr := cmd.appRepo.Update(app.Guid, params) 65 if apiErr != nil { 66 cmd.ui.Failed(apiErr.Error()) 67 return 68 } 69 cmd.ui.Ok() 70 }