github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/plugins/plugdeps/plugin.go (about)

     1  package plugdeps
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/gobuffalo/meta"
    10  )
    11  
    12  // bin, module version, and tag
    13  var re = regexp.MustCompile(`.*(buffalo-[^/@]+)/?(v[0-9]+)?@?(.*)?`)
    14  
    15  // Plugin represents a Go plugin for Buffalo applications
    16  type Plugin struct {
    17  	Binary   string         `toml:"binary" json:"binary"`
    18  	GoGet    string         `toml:"go_get,omitempty" json:"go_get,omitempty"`
    19  	Local    string         `toml:"local,omitempty" json:"local,omitempty"`
    20  	Commands []Command      `toml:"command,omitempty" json:"commands,omitempty"`
    21  	Tags     meta.BuildTags `toml:"tags,omitempty" json:"tags,omitempty"`
    22  }
    23  
    24  // String implementation of fmt.Stringer
    25  func (p Plugin) String() string {
    26  	b, _ := json.Marshal(p)
    27  	return string(b)
    28  }
    29  
    30  func (p Plugin) key() string {
    31  	// p.Binary should be uniq and it can be used as the key
    32  	return p.Binary
    33  }
    34  
    35  func NewPlugin(mod string, tags ...meta.BuildTags) Plugin {
    36  	mod = strings.TrimSpace(mod)
    37  	match := re.FindStringSubmatch(mod)
    38  	bin := match[1]
    39  	tag := match[3]
    40  	plug := Plugin{
    41  		Binary: bin,
    42  		GoGet:  mod,
    43  	}
    44  	if len(tags) > 0 {
    45  		plug.Tags = tags[0]
    46  	}
    47  	if _, err := os.Stat(mod); err == nil {
    48  		plug.Local = mod
    49  		plug.GoGet = ""
    50  	}
    51  	if plug.GoGet != "" && tag == "" {
    52  		plug.GoGet = mod + "@latest"
    53  	}
    54  	return plug
    55  }