github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/copy_source.go (about) 1 package application 2 3 import ( 4 "errors" 5 "fmt" 6 7 "code.cloudfoundry.org/cli/cf/api/applications" 8 "code.cloudfoundry.org/cli/cf/api/authentication" 9 "code.cloudfoundry.org/cli/cf/api/copyapplicationsource" 10 "code.cloudfoundry.org/cli/cf/api/organizations" 11 "code.cloudfoundry.org/cli/cf/api/spaces" 12 "code.cloudfoundry.org/cli/cf/commandregistry" 13 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 14 "code.cloudfoundry.org/cli/cf/flags" 15 . "code.cloudfoundry.org/cli/cf/i18n" 16 "code.cloudfoundry.org/cli/cf/models" 17 "code.cloudfoundry.org/cli/cf/requirements" 18 "code.cloudfoundry.org/cli/cf/terminal" 19 ) 20 21 type CopySource struct { 22 ui terminal.UI 23 config coreconfig.Reader 24 authRepo authentication.Repository 25 appRepo applications.Repository 26 orgRepo organizations.OrganizationRepository 27 spaceRepo spaces.SpaceRepository 28 copyAppSourceRepo copyapplicationsource.Repository 29 appRestart Restarter 30 } 31 32 func init() { 33 commandregistry.Register(&CopySource{}) 34 } 35 36 func (cmd *CopySource) MetaData() commandregistry.CommandMetadata { 37 fs := make(map[string]flags.FlagSet) 38 fs["no-restart"] = &flags.BoolFlag{Name: "no-restart", Usage: T("Override restart of the application in target environment after copy-source completes")} 39 fs["o"] = &flags.StringFlag{ShortName: "o", Usage: T("Org that contains the target application")} 40 fs["s"] = &flags.StringFlag{ShortName: "s", Usage: T("Space that contains the target application")} 41 42 return commandregistry.CommandMetadata{ 43 Name: "copy-source", 44 Description: T("Copies the source code of an application to another existing application (and restarts that application)"), 45 Usage: []string{ 46 T(" CF_NAME copy-source SOURCE-APP TARGET-APP [-s TARGET-SPACE [-o TARGET-ORG]] [--no-restart]\n"), 47 }, 48 Flags: fs, 49 } 50 } 51 52 func (cmd *CopySource) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 53 usageReq := requirementsFactory.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), 54 T("Requires SOURCE-APP TARGET-APP as arguments"), 55 func() bool { 56 return len(fc.Args()) != 2 57 }, 58 ) 59 60 reqs := []requirements.Requirement{ 61 usageReq, 62 requirementsFactory.NewLoginRequirement(), 63 requirementsFactory.NewTargetedSpaceRequirement(), 64 } 65 66 return reqs, nil 67 } 68 69 func (cmd *CopySource) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 70 cmd.ui = deps.UI 71 cmd.config = deps.Config 72 cmd.authRepo = deps.RepoLocator.GetAuthenticationRepository() 73 cmd.appRepo = deps.RepoLocator.GetApplicationRepository() 74 cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository() 75 cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository() 76 cmd.copyAppSourceRepo = deps.RepoLocator.GetCopyApplicationSourceRepository() 77 78 //get command from registry for dependency 79 commandDep := commandregistry.Commands.FindCommand("restart") 80 commandDep = commandDep.SetDependency(deps, false) 81 cmd.appRestart = commandDep.(Restarter) 82 83 return cmd 84 } 85 86 func (cmd *CopySource) Execute(c flags.FlagContext) error { 87 sourceAppName := c.Args()[0] 88 targetAppName := c.Args()[1] 89 90 targetOrg := c.String("o") 91 targetSpace := c.String("s") 92 93 if targetOrg != "" && targetSpace == "" { 94 return errors.New(T("Please provide the space within the organization containing the target application")) 95 } 96 97 _, err := cmd.authRepo.RefreshAuthToken() 98 if err != nil { 99 return err 100 } 101 102 sourceApp, err := cmd.appRepo.Read(sourceAppName) 103 if err != nil { 104 return err 105 } 106 107 var targetOrgName, targetSpaceName, spaceGUID, copyStr string 108 if targetOrg != "" && targetSpace != "" { 109 spaceGUID, err = cmd.findSpaceGUID(targetOrg, targetSpace) 110 if err != nil { 111 return err 112 } 113 114 targetOrgName = targetOrg 115 targetSpaceName = targetSpace 116 } else if targetSpace != "" { 117 var space models.Space 118 space, err = cmd.spaceRepo.FindByName(targetSpace) 119 if err != nil { 120 return err 121 } 122 spaceGUID = space.GUID 123 targetOrgName = cmd.config.OrganizationFields().Name 124 targetSpaceName = targetSpace 125 } else { 126 spaceGUID = cmd.config.SpaceFields().GUID 127 targetOrgName = cmd.config.OrganizationFields().Name 128 targetSpaceName = cmd.config.SpaceFields().Name 129 } 130 131 copyStr = buildCopyString(sourceAppName, targetAppName, targetOrgName, targetSpaceName, cmd.config.Username()) 132 133 targetApp, err := cmd.appRepo.ReadFromSpace(targetAppName, spaceGUID) 134 if err != nil { 135 return err 136 } 137 138 cmd.ui.Say(copyStr) 139 cmd.ui.Say(T("Note: this may take some time")) 140 cmd.ui.Say("") 141 142 err = cmd.copyAppSourceRepo.CopyApplication(sourceApp.GUID, targetApp.GUID) 143 if err != nil { 144 return err 145 } 146 147 if !c.Bool("no-restart") { 148 cmd.appRestart.ApplicationRestart(targetApp, targetOrgName, targetSpaceName) 149 } 150 151 cmd.ui.Ok() 152 return nil 153 } 154 155 func (cmd *CopySource) findSpaceGUID(targetOrg, targetSpace string) (string, error) { 156 org, err := cmd.orgRepo.FindByName(targetOrg) 157 if err != nil { 158 return "", err 159 } 160 161 var space models.SpaceFields 162 var foundSpace bool 163 for _, s := range org.Spaces { 164 if s.Name == targetSpace { 165 space = s 166 foundSpace = true 167 } 168 } 169 170 if !foundSpace { 171 return "", fmt.Errorf(T("Could not find space {{.Space}} in organization {{.Org}}", 172 map[string]interface{}{ 173 "Space": terminal.EntityNameColor(targetSpace), 174 "Org": terminal.EntityNameColor(targetOrg), 175 }, 176 )) 177 } 178 179 return space.GUID, nil 180 } 181 182 func buildCopyString(sourceAppName, targetAppName, targetOrgName, targetSpaceName, username string) string { 183 return fmt.Sprintf(T("Copying source from app {{.SourceApp}} to target app {{.TargetApp}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", 184 map[string]interface{}{ 185 "SourceApp": terminal.EntityNameColor(sourceAppName), 186 "TargetApp": terminal.EntityNameColor(targetAppName), 187 "OrgName": terminal.EntityNameColor(targetOrgName), 188 "SpaceName": terminal.EntityNameColor(targetSpaceName), 189 "Username": terminal.EntityNameColor(username), 190 }, 191 )) 192 193 }