github.com/singlemusic/buffalo@v0.16.30/buffalo/cmd/generate/action.go (about)

     1  package generate
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/gobuffalo/buffalo/genny/actions"
     8  	"github.com/gobuffalo/genny/v2"
     9  	"github.com/gobuffalo/logger"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var actionOptions = struct {
    14  	*actions.Options
    15  	dryRun  bool
    16  	verbose bool
    17  }{
    18  	Options: &actions.Options{},
    19  }
    20  
    21  //ActionCmd is the cmd that generates actions.
    22  var ActionCmd = &cobra.Command{
    23  	Use:     "action [name] [handler name...]",
    24  	Aliases: []string{"a", "actions"},
    25  	Short:   "Generate new action(s)",
    26  	RunE: func(cmd *cobra.Command, args []string) error {
    27  		if len(args) == 0 {
    28  			return fmt.Errorf("you must provide a name")
    29  		}
    30  		actionOptions.Name = args[0]
    31  		if len(args) == 1 {
    32  			return fmt.Errorf("you must provide at least one action name")
    33  		}
    34  		actionOptions.Actions = args[1:]
    35  
    36  		ctx := context.Background()
    37  		run := genny.WetRunner(ctx)
    38  
    39  		if actionOptions.dryRun {
    40  			run = genny.DryRunner(ctx)
    41  		}
    42  
    43  		if actionOptions.verbose {
    44  			run.Logger = logger.New(logger.DebugLevel)
    45  		}
    46  
    47  		opts := actionOptions.Options
    48  		run.WithNew(actions.New(opts))
    49  		return run.Run()
    50  	},
    51  }
    52  
    53  func init() {
    54  	ActionCmd.Flags().BoolVarP(&actionOptions.SkipTemplates, "skip-template", "", false, "skip generation of templates for action(s)")
    55  	ActionCmd.Flags().BoolVarP(&actionOptions.dryRun, "dry-run", "d", false, "dry run")
    56  	ActionCmd.Flags().BoolVarP(&actionOptions.verbose, "verbose", "v", false, "verbosely run the generator")
    57  	ActionCmd.Flags().StringVarP(&actionOptions.Method, "method", "m", "GET", "change the HTTP method for the generate action(s)")
    58  }