github.com/singlemusic/buffalo@v0.16.30/buffalo/cmd/plugins/add.go (about)

     1  package plugins
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/gobuffalo/buffalo/genny/add"
    10  	"github.com/gobuffalo/buffalo/internal/takeon/github.com/markbates/errx"
    11  	"github.com/gobuffalo/buffalo/plugins/plugdeps"
    12  	"github.com/gobuffalo/genny/v2"
    13  	"github.com/gobuffalo/meta"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var addOptions = struct {
    18  	dryRun    bool
    19  	buildTags []string
    20  }{}
    21  
    22  var addCmd = &cobra.Command{
    23  	Use:   "add",
    24  	Short: "adds plugins to config/buffalo-plugins.toml",
    25  	RunE: func(cmd *cobra.Command, args []string) error {
    26  		run := genny.WetRunner(context.Background())
    27  		if addOptions.dryRun {
    28  			run = genny.DryRunner(context.Background())
    29  		}
    30  
    31  		app := meta.New(".")
    32  		plugs, err := plugdeps.List(app)
    33  		if err != nil && (errx.Unwrap(err) != plugdeps.ErrMissingConfig) {
    34  			return err
    35  		}
    36  
    37  		tags := app.BuildTags("", addOptions.buildTags...)
    38  		for _, a := range args {
    39  			a = strings.TrimSpace(a)
    40  			bin := path.Base(a)
    41  			plug := plugdeps.Plugin{
    42  				Binary: bin,
    43  				GoGet:  a,
    44  				Tags:   tags,
    45  			}
    46  			if _, err := os.Stat(a); err == nil {
    47  				plug.Local = a
    48  				plug.GoGet = ""
    49  			}
    50  			plugs.Add(plug)
    51  		}
    52  		g, err := add.New(&add.Options{
    53  			App:     app,
    54  			Plugins: plugs.List(),
    55  		})
    56  		if err != nil {
    57  			return err
    58  		}
    59  		run.With(g)
    60  
    61  		return run.Run()
    62  	},
    63  }
    64  
    65  func init() {
    66  	addCmd.Flags().BoolVarP(&addOptions.dryRun, "dry-run", "d", false, "dry run")
    67  	addCmd.Flags().StringSliceVarP(&addOptions.buildTags, "tags", "t", []string{}, "build tags")
    68  }