github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/actors/plugin_installer/plugin_downloader.go (about) 1 package plugin_installer 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "runtime" 8 9 clipr "github.com/cloudfoundry-incubator/cli-plugin-repo/models" 10 . "github.com/cloudfoundry/cli/cf/i18n" 11 "github.com/cloudfoundry/cli/cf/terminal" 12 "github.com/cloudfoundry/cli/fileutils" 13 ) 14 15 type PluginDownloader struct { 16 Ui terminal.UI 17 FileDownloader fileutils.Downloader 18 } 19 type downloadFromPath func(pluginSourceFilepath string, downloader fileutils.Downloader) string 20 21 func (downloader *PluginDownloader) downloadFromPath(pluginSourceFilepath string) string { 22 size, filename, err := downloader.FileDownloader.DownloadFile(pluginSourceFilepath) 23 24 if err != nil { 25 downloader.Ui.Failed(fmt.Sprintf(T("Download attempt failed: {{.Error}}\n\nUnable to install, plugin is not available from the given url.", map[string]interface{}{"Error": err.Error()}))) 26 } 27 28 downloader.Ui.Say(fmt.Sprintf("%d "+T("bytes downloaded")+"...", size)) 29 30 executablePath := filepath.Join(downloader.FileDownloader.SavePath(), filename) 31 os.Chmod(executablePath, 0700) 32 33 return executablePath 34 } 35 36 func (downloader *PluginDownloader) downloadFromPlugin(plugin clipr.Plugin) (string, string) { 37 arch := runtime.GOARCH 38 39 switch runtime.GOOS { 40 case "darwin": 41 return downloader.downloadFromPath(downloader.getBinaryUrl(plugin, "osx")), downloader.getBinaryChecksum(plugin, "osx") 42 case "linux": 43 if arch == "386" { 44 return downloader.downloadFromPath(downloader.getBinaryUrl(plugin, "linux32")), downloader.getBinaryChecksum(plugin, "linux32") 45 } else { 46 return downloader.downloadFromPath(downloader.getBinaryUrl(plugin, "linux64")), downloader.getBinaryChecksum(plugin, "linux64") 47 } 48 case "windows": 49 if arch == "386" { 50 return downloader.downloadFromPath(downloader.getBinaryUrl(plugin, "win32")), downloader.getBinaryChecksum(plugin, "win32") 51 } else { 52 return downloader.downloadFromPath(downloader.getBinaryUrl(plugin, "win64")), downloader.getBinaryChecksum(plugin, "win64") 53 } 54 default: 55 downloader.binaryNotAvailable() 56 } 57 return "", "" 58 } 59 60 func (downloader *PluginDownloader) getBinaryUrl(plugin clipr.Plugin, os string) string { 61 for _, binary := range plugin.Binaries { 62 if binary.Platform == os { 63 return binary.Url 64 } 65 } 66 downloader.binaryNotAvailable() 67 return "" 68 } 69 70 func (downloader *PluginDownloader) getBinaryChecksum(plugin clipr.Plugin, os string) string { 71 for _, binary := range plugin.Binaries { 72 if binary.Platform == os { 73 return binary.Checksum 74 } 75 } 76 return "" 77 } 78 79 func (downloader *PluginDownloader) binaryNotAvailable() { 80 downloader.Ui.Failed(T("Plugin requested has no binary available for your OS: ") + runtime.GOOS + ", " + runtime.GOARCH) 81 }