github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/buffalo.go (about)

     1  package cli
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/gobuffalo/buffalo-cli/v2/cli/cmds"
     7  	"github.com/gobuffalo/buffalo-cli/v2/cli/internal/clifix"
     8  	"github.com/gobuffalo/buffalo-cli/v2/cli/internal/plugins/golang"
     9  	"github.com/gobuffalo/buffalo-cli/v2/meta"
    10  	"github.com/gobuffalo/plugins"
    11  	"github.com/gobuffalo/plugins/plugcmd"
    12  	"github.com/gobuffalo/plugins/plugprint"
    13  )
    14  
    15  var _ plugcmd.SubCommander = &Buffalo{}
    16  var _ plugins.Plugin = &Buffalo{}
    17  var _ plugins.Scoper = &Buffalo{}
    18  var _ plugprint.Describer = &Buffalo{}
    19  
    20  // Buffalo represents the `buffalo` cli.
    21  type Buffalo struct {
    22  	Plugins []plugins.Plugin
    23  	root    string
    24  }
    25  
    26  // TODO move to the generated application code
    27  // once packages are no longer internal
    28  func insidePlugins(root string) []plugins.Plugin {
    29  	var plugs []plugins.Plugin
    30  
    31  	plugs = append(plugs, golang.Plugins()...)
    32  	plugs = append(plugs, clifix.Plugins()...)
    33  	return plugs
    34  }
    35  
    36  func NewFromRoot(root string) (*Buffalo, error) {
    37  	b := &Buffalo{
    38  		root: root,
    39  	}
    40  
    41  	b.Plugins = append(b.Plugins, cmds.AvailablePlugins(root)...)
    42  
    43  	if meta.IsBuffalo(root) {
    44  		b.Plugins = append(b.Plugins, insidePlugins(root)...)
    45  	}
    46  
    47  	// pre scope the plugins to thin the initial set
    48  	plugs := b.ScopedPlugins()
    49  	plugins.Sort(plugs)
    50  	b.Plugins = plugs
    51  
    52  	pfn := b.ScopedPlugins
    53  
    54  	for _, p := range b.Plugins {
    55  		switch t := p.(type) {
    56  		case plugins.Needer:
    57  			t.WithPlugins(pfn)
    58  		}
    59  	}
    60  
    61  	return b, nil
    62  }
    63  
    64  func New() (*Buffalo, error) {
    65  	pwd, err := os.Getwd()
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	return NewFromRoot(pwd)
    70  }
    71  
    72  func (b Buffalo) ScopedPlugins() []plugins.Plugin {
    73  	root := b.root
    74  	plugs := make([]plugins.Plugin, 0, len(b.Plugins))
    75  	for _, p := range b.Plugins {
    76  		switch t := p.(type) {
    77  		case AvailabilityChecker:
    78  			if !t.PluginAvailable(root) {
    79  				continue
    80  			}
    81  		}
    82  		plugs = append(plugs, p)
    83  	}
    84  	return plugs
    85  }
    86  
    87  func (b Buffalo) SubCommands() []plugins.Plugin {
    88  	var plugs []plugins.Plugin
    89  	for _, p := range b.ScopedPlugins() {
    90  		if _, ok := p.(Commander); ok {
    91  			plugs = append(plugs, p)
    92  		}
    93  	}
    94  	return plugs
    95  }
    96  
    97  // Name ...
    98  func (Buffalo) PluginName() string {
    99  	return "buffalo"
   100  }
   101  
   102  func (Buffalo) String() string {
   103  	return "buffalo"
   104  }
   105  
   106  // Description ...
   107  func (Buffalo) Description() string {
   108  	return "Tools for working with Buffalo applications"
   109  }