github.com/wawandco/oxplugins@v0.7.11/tools/pop/migrate/command.go (about)

     1  package migrate
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/gobuffalo/packd"
     8  	"github.com/spf13/pflag"
     9  	"github.com/wawandco/oxplugins/plugins"
    10  )
    11  
    12  var (
    13  	_ plugins.Command = (*Command)(nil)
    14  
    15  	ErrCouldNotFindConnection = errors.New("could not find connection by name")
    16  	ErrNotEnoughArgs          = errors.New("not enough args, please specify direction p.e ox pop migrate up")
    17  	ErrInvalidInstruction     = errors.New("invalid instruction for migrate command")
    18  )
    19  
    20  type Command struct {
    21  	*Logger
    22  
    23  	steps          int
    24  	connectionName string
    25  	migrations     packd.Box
    26  
    27  	flags *pflag.FlagSet
    28  }
    29  
    30  //HelpText returns the help Text of build function
    31  func (m Command) HelpText() string {
    32  	return "Uses soda to run pop migrations"
    33  }
    34  
    35  func (m *Command) Name() string {
    36  	return "migrate"
    37  }
    38  
    39  func (m *Command) ParentName() string {
    40  	return "db"
    41  }
    42  
    43  func (m *Command) Run(ctx context.Context, root string, args []string) error {
    44  	if len(args) < 3 {
    45  		return m.RunUp()
    46  	}
    47  
    48  	direction := args[2]
    49  	if direction == "up" {
    50  		return m.RunUp()
    51  	}
    52  
    53  	if direction == "down" {
    54  		return m.RunDown()
    55  	}
    56  
    57  	return ErrInvalidInstruction
    58  }
    59  
    60  func (m *Command) ParseFlags(args []string) {
    61  	m.flags = pflag.NewFlagSet(m.Name(), pflag.ContinueOnError)
    62  	m.flags.StringVarP(&m.connectionName, "conn", "", "development", "the name of the connection to use")
    63  	m.flags.IntVarP(&m.steps, "steps", "s", 0, "how many migrations to run")
    64  	m.flags.Parse(args) //nolint:errcheck,we don't care hence the flag
    65  }
    66  
    67  func (m *Command) Flags() *pflag.FlagSet {
    68  	return m.flags
    69  }