github.com/wawandco/oxplugins@v0.7.11/tools/db/create.go (about)

     1  package db
     2  
     3  import (
     4  	"context"
     5  
     6  	pop4 "github.com/gobuffalo/pop"
     7  	pop5 "github.com/gobuffalo/pop/v5"
     8  	"github.com/spf13/pflag"
     9  )
    10  
    11  type CreateCommand struct {
    12  	connections    map[string]URLProvider
    13  	connectionName string
    14  	flags          *pflag.FlagSet
    15  }
    16  
    17  func (d CreateCommand) Name() string {
    18  	return "create"
    19  }
    20  
    21  func (d CreateCommand) HelpText() string {
    22  	return "creates database in GO_ENV or --conn flag"
    23  }
    24  
    25  func (d CreateCommand) ParentName() string {
    26  	return "db"
    27  }
    28  
    29  func (d *CreateCommand) Run(ctx context.Context, root string, args []string) error {
    30  	conn := d.connections[d.connectionName]
    31  	if conn == nil {
    32  		return ErrConnectionNotFound
    33  	}
    34  
    35  	if c, ok := conn.(*pop4.Connection); ok {
    36  		return c.Dialect.CreateDB()
    37  	}
    38  
    39  	if c, ok := conn.(*pop5.Connection); ok {
    40  		return c.Dialect.CreateDB()
    41  	}
    42  
    43  	return nil
    44  }
    45  
    46  func (d *CreateCommand) ParseFlags(args []string) {
    47  	d.flags = pflag.NewFlagSet(d.Name(), pflag.ContinueOnError)
    48  	d.flags.StringVarP(&d.connectionName, "conn", "", "development", "the name of the connection to use")
    49  	d.flags.Parse(args) //nolint:errcheck,we don't care hence the flag
    50  }
    51  
    52  func (d *CreateCommand) Flags() *pflag.FlagSet {
    53  	return d.flags
    54  }