github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/plugin/discovery/meta.go (about)

     1  package discovery
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"io"
     6  	"os"
     7  )
     8  
     9  // PluginMeta is metadata about a plugin, useful for launching the plugin
    10  // and for understanding which plugins are available.
    11  type PluginMeta struct {
    12  	// Name is the name of the plugin, e.g. as inferred from the plugin
    13  	// binary's filename, or by explicit configuration.
    14  	Name string
    15  
    16  	// Version is the semver version of the plugin, expressed as a string
    17  	// that might not be semver-valid.
    18  	Version VersionStr
    19  
    20  	// Path is the absolute path of the executable that can be launched
    21  	// to provide the RPC server for this plugin.
    22  	Path string
    23  }
    24  
    25  // SHA256 returns a SHA256 hash of the content of the referenced executable
    26  // file, or an error if the file's contents cannot be read.
    27  func (m PluginMeta) SHA256() ([]byte, error) {
    28  	f, err := os.Open(m.Path)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	defer f.Close()
    33  
    34  	h := sha256.New()
    35  	_, err = io.Copy(h, f)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	return h.Sum(nil), nil
    41  }