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

     1  package bzr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	"github.com/gobuffalo/buffalo-cli/v2/cli/cmds/build"
    10  	"github.com/gobuffalo/plugins"
    11  	"github.com/gobuffalo/plugins/plugio"
    12  	"github.com/gobuffalo/plugins/plugprint"
    13  )
    14  
    15  var _ build.Versioner = &Versioner{}
    16  var _ plugins.Plugin = Versioner{}
    17  var _ plugins.Needer = &Versioner{}
    18  var _ plugprint.Describer = Versioner{}
    19  
    20  // Versioner ...
    21  type Versioner struct {
    22  	pluginsFn plugins.Feeder
    23  }
    24  
    25  // WithPlugins ...
    26  func (b *Versioner) WithPlugins(f plugins.Feeder) {
    27  	b.pluginsFn = f
    28  }
    29  
    30  // ScopedPlugins ...
    31  func (b *Versioner) ScopedPlugins() []plugins.Plugin {
    32  	if b.pluginsFn == nil {
    33  		return []plugins.Plugin{}
    34  	}
    35  
    36  	var scoped []plugins.Plugin
    37  	for _, p := range b.pluginsFn() {
    38  		switch p.(type) {
    39  		case Runner:
    40  			scoped = append(scoped, p)
    41  		}
    42  	}
    43  
    44  	return scoped
    45  }
    46  
    47  // BuildVersion is used by other commands to get the build
    48  // version of the current source and use it for the build.
    49  func (b *Versioner) BuildVersion(ctx context.Context, root string) (string, error) {
    50  	plugs := b.ScopedPlugins()
    51  
    52  	cmd := exec.CommandContext(ctx, "bzr", "revno")
    53  
    54  	bb := &bytes.Buffer{}
    55  	cmd.Stdout = bb
    56  	cmd.Stderr = plugio.Stderr(plugs...)
    57  
    58  	fn := func(ctx context.Context, root string, cmd *exec.Cmd) error {
    59  		return cmd.Run()
    60  	}
    61  
    62  	for _, p := range plugs {
    63  		if vr, ok := p.(Runner); ok {
    64  			fn = vr.RunBzr
    65  			break
    66  		}
    67  	}
    68  
    69  	if err := fn(ctx, root, cmd); err != nil {
    70  		return "", err
    71  	}
    72  
    73  	return strings.TrimSpace(bb.String()), nil
    74  }
    75  
    76  // Name is the name of the plugin.
    77  // This will also be used for the cli sub-command
    78  // 	"pop" | "heroku" | "auth" | etc...
    79  func (b Versioner) PluginName() string {
    80  	return "bzr"
    81  }
    82  
    83  //Description is a general description of the plugin and its functionalities.
    84  func (b Versioner) Description() string {
    85  	return "Provides bzr related hooks to Buffalo applications."
    86  }