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