github.com/jacobsoderblom/buffalo@v0.11.0/buffalo/cmd/plugins.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"runtime"
     8  	"sync"
     9  
    10  	"github.com/gobuffalo/buffalo/plugins"
    11  	"github.com/gobuffalo/envy"
    12  	"github.com/sirupsen/logrus"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var plugx = &sync.Mutex{}
    17  var _plugs plugins.List
    18  
    19  func plugs() plugins.List {
    20  	plugx.Lock()
    21  	defer plugx.Unlock()
    22  	if _plugs == nil {
    23  		var err error
    24  		_plugs, err = plugins.Available()
    25  		if err != nil {
    26  			_plugs = plugins.List{}
    27  			logrus.Errorf("error loading plugins %s\n", err)
    28  		}
    29  	}
    30  	return _plugs
    31  }
    32  
    33  func decorate(name string, cmd *cobra.Command) {
    34  	pugs := plugs()
    35  	for _, c := range pugs[name] {
    36  		func(c plugins.Command) {
    37  			anywhereCommands = append(anywhereCommands, c.Name)
    38  			cc := &cobra.Command{
    39  				Use:     c.Name,
    40  				Short:   fmt.Sprintf("[PLUGIN] %s", c.Description),
    41  				Aliases: c.Aliases,
    42  				RunE: func(cmd *cobra.Command, args []string) error {
    43  					plugCmd := c.Name
    44  					if c.UseCommand != "" {
    45  						plugCmd = c.UseCommand
    46  					}
    47  
    48  					ax := []string{plugCmd}
    49  					if plugCmd == "-" {
    50  						ax = []string{}
    51  					}
    52  
    53  					ax = append(ax, args...)
    54  					ex := exec.Command(c.Binary, ax...)
    55  					if runtime.GOOS != "windows" {
    56  						ex.Env = append(envy.Environ(), "BUFFALO_PLUGIN=1")
    57  					}
    58  					ex.Stdin = os.Stdin
    59  					ex.Stdout = os.Stdout
    60  					ex.Stderr = os.Stderr
    61  					return ex.Run()
    62  				},
    63  			}
    64  			cc.DisableFlagParsing = true
    65  			cmd.AddCommand(cc)
    66  		}(c)
    67  	}
    68  }