get.porter.sh/porter@v1.3.0/pkg/plugins/plugins.go (about)

     1  package plugins
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/go-plugin"
     9  )
    10  
    11  // HandshakeConfig is common handshake config between Porter and its plugins.
    12  var HandshakeConfig = plugin.HandshakeConfig{
    13  	MagicCookieKey:   "PORTER",
    14  	MagicCookieValue: "bbc2dd71-def4-4311-906e-e98dc27208ce",
    15  }
    16  
    17  type PluginKey struct {
    18  	Binary         string
    19  	Interface      string
    20  	Implementation string
    21  	IsInternal     bool
    22  }
    23  
    24  func (k PluginKey) String() string {
    25  	return fmt.Sprintf("%s.%s.%s", k.Interface, k.Binary, k.Implementation)
    26  }
    27  
    28  func ParsePluginKey(value string) (PluginKey, error) {
    29  	var key PluginKey
    30  
    31  	parts := strings.Split(value, ".")
    32  
    33  	switch len(parts) {
    34  	case 1:
    35  		key.Binary = "porter"
    36  		key.Implementation = parts[0]
    37  	case 2:
    38  		key.Binary = parts[0]
    39  		key.Implementation = parts[1]
    40  	case 3:
    41  		key.Interface = parts[0]
    42  		key.Binary = parts[1]
    43  		key.Implementation = parts[2]
    44  	default:
    45  		return PluginKey{}, errors.New("invalid plugin key '%s', allowed format is [INTERFACE].BINARY.IMPLEMENTATION")
    46  	}
    47  
    48  	if key.Binary == "porter" {
    49  		key.IsInternal = true
    50  	}
    51  
    52  	return key, nil
    53  }