github.com/sleungcy-sap/cli@v7.1.0+incompatible/cf/actors/plugininstaller/plugin_downloader.go (about)

     1  package plugininstaller
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  
     9  	clipr "code.cloudfoundry.org/cli-plugin-repo/web"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  	"code.cloudfoundry.org/cli/cf/terminal"
    12  	"code.cloudfoundry.org/cli/cf/util/downloader"
    13  )
    14  
    15  type PluginDownloader struct {
    16  	UI             terminal.UI
    17  	FileDownloader downloader.Downloader
    18  }
    19  type downloadFromPath func(string, downloader.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  	err = os.Chmod(executablePath, 0700)
    32  	if err != nil {
    33  		downloader.UI.Failed(fmt.Sprintf(T("Failed to make plugin executable: {{.Error}}", map[string]interface{}{"Error": err.Error()})))
    34  	}
    35  
    36  	return executablePath
    37  }
    38  
    39  func (downloader *PluginDownloader) downloadFromPlugin(plugin clipr.Plugin) (string, string) {
    40  	arch := runtime.GOARCH
    41  
    42  	switch runtime.GOOS {
    43  	case "darwin":
    44  		return downloader.downloadFromPath(downloader.getBinaryURL(plugin, "osx")), downloader.getBinaryChecksum(plugin, "osx")
    45  	case "linux":
    46  		if arch == "386" {
    47  			return downloader.downloadFromPath(downloader.getBinaryURL(plugin, "linux32")), downloader.getBinaryChecksum(plugin, "linux32")
    48  		}
    49  		return downloader.downloadFromPath(downloader.getBinaryURL(plugin, "linux64")), downloader.getBinaryChecksum(plugin, "linux64")
    50  	case "windows":
    51  		if arch == "386" {
    52  			return downloader.downloadFromPath(downloader.getBinaryURL(plugin, "win32")), downloader.getBinaryChecksum(plugin, "win32")
    53  		}
    54  		return downloader.downloadFromPath(downloader.getBinaryURL(plugin, "win64")), downloader.getBinaryChecksum(plugin, "win64")
    55  	default:
    56  		downloader.binaryNotAvailable()
    57  	}
    58  	return "", ""
    59  }
    60  
    61  func (downloader *PluginDownloader) getBinaryURL(plugin clipr.Plugin, os string) string {
    62  	for _, binary := range plugin.Binaries {
    63  		if binary.Platform == os {
    64  			return binary.Url
    65  		}
    66  	}
    67  	downloader.binaryNotAvailable()
    68  	return ""
    69  }
    70  
    71  func (downloader *PluginDownloader) getBinaryChecksum(plugin clipr.Plugin, os string) string {
    72  	for _, binary := range plugin.Binaries {
    73  		if binary.Platform == os {
    74  			return binary.Checksum
    75  		}
    76  	}
    77  	return ""
    78  }
    79  
    80  func (downloader *PluginDownloader) binaryNotAvailable() {
    81  	downloader.UI.Failed(T("Plugin requested has no binary available for your OS: ") + runtime.GOOS + ", " + runtime.GOARCH)
    82  }