github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/plugin/downloader.go (about)

     1  package plugin
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  
     7  	"github.com/kisexp/xdchain/common"
     8  	"github.com/kisexp/xdchain/log"
     9  )
    10  
    11  // get plugin zip file from local or remote
    12  type Downloader struct {
    13  	pm *PluginManager
    14  }
    15  
    16  func NewDownloader(pm *PluginManager) *Downloader {
    17  	return &Downloader{
    18  		pm: pm,
    19  	}
    20  }
    21  
    22  func (d *Downloader) Download(definition *PluginDefinition) (string, error) {
    23  	// check if plugin is already in the local
    24  	pluginFile := path.Join(d.pm.pluginBaseDir, definition.DistFileName())
    25  	exist := common.FileExist(pluginFile)
    26  	log.Debug("checking plugin zip file", "path", pluginFile, "exist", exist)
    27  	if exist {
    28  		return pluginFile, nil
    29  	}
    30  	if err := d.pm.centralClient.PluginDistribution(definition, pluginFile); err != nil {
    31  		return "", fmt.Errorf("can't download from Plugin Central due to: %s. Please download the plugin manually and copy it to %s", err, d.pm.pluginBaseDir)
    32  	}
    33  	return pluginFile, nil
    34  }