github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/soda/command.go (about)

     1  package soda
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io/fs"
     7  
     8  	"github.com/spf13/pflag"
     9  	"github.com/wawandco/ox/plugins/core"
    10  )
    11  
    12  var (
    13  	_ core.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     fs.FS
    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 "database"
    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.Usage = func() {}
    63  	m.flags.StringVarP(&m.connectionName, "conn", "", "development", "the name of the connection to use")
    64  	m.flags.IntVarP(&m.steps, "steps", "s", 0, "how many migrations to run")
    65  	m.flags.Parse(args) //nolint:errcheck,we don't care hence the flag
    66  }
    67  
    68  func (m *Command) Flags() *pflag.FlagSet {
    69  	return m.flags
    70  }
    71  
    72  func NewCommand(folder fs.FS) *Command {
    73  	return &Command{
    74  		migrations: folder,
    75  	}
    76  }