Remove tables flag from binary

This commit is contained in:
Patrick O'brien 2016-08-15 22:42:40 +10:00
parent 8f0abc95ee
commit a681624168
3 changed files with 15 additions and 33 deletions

View file

@ -26,12 +26,12 @@ type Interface interface {
// Tables returns the table metadata for the given tables, or all tables if // Tables returns the table metadata for the given tables, or all tables if
// no tables are provided. // no tables are provided.
func Tables(db Interface, names ...string) ([]Table, error) { func Tables(db Interface) ([]Table, error) {
var err error var err error
if len(names) == 0 {
if names, err = db.TableNames(); err != nil { names, err := db.TableNames()
return nil, errors.Wrap(err, "unable to get table names") if err != nil {
} return nil, errors.Wrap(err, "unable to get table names")
} }
var tables []Table var tables []Table

27
main.go
View file

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/kat-co/vala" "github.com/kat-co/vala"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -48,10 +47,10 @@ func main() {
// Set up the cobra root command // Set up the cobra root command
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "sqlboiler [flags] <driver>", Use: "sqlboiler [flags] <driver>",
Short: "SQL Boiler generates boilerplate structs and statements", Short: "SQL Boiler generates an ORM tailored to your database schema.",
Long: "SQL Boiler generates boilerplate structs and statements from template files.\n" + Long: "SQL Boiler generates a Go ORM from template files, tailored to your database schema.\n" +
`Complete documentation is available at http://github.com/vattle/sqlboiler`, `Complete documentation is available at http://github.com/vattle/sqlboiler`,
Example: `sqlboiler -o models -p models postgres`, Example: `sqlboiler postgres`,
PreRunE: preRun, PreRunE: preRun,
RunE: run, RunE: run,
PostRunE: postRun, PostRunE: postRun,
@ -60,7 +59,6 @@ func main() {
} }
// Set up the cobra root command flags // Set up the cobra root command flags
rootCmd.PersistentFlags().StringSliceP("tables", "t", nil, "Tables to generate models for, all tables if empty")
rootCmd.PersistentFlags().StringP("output", "o", "models", "The name of the folder to output to") rootCmd.PersistentFlags().StringP("output", "o", "models", "The name of the folder to output to")
rootCmd.PersistentFlags().StringP("pkgname", "p", "models", "The name you wish to assign to your generated package") rootCmd.PersistentFlags().StringP("pkgname", "p", "models", "The name you wish to assign to your generated package")
@ -71,8 +69,7 @@ func main() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
if e, ok := err.(commandFailure); ok { if e, ok := err.(commandFailure); ok {
rootCmd.HelpFunc() fmt.Printf("%s\n", string(e))
fmt.Printf("\n%s\n", string(e))
return return
} }
@ -91,7 +88,7 @@ func preRun(cmd *cobra.Command, args []string) error {
var err error var err error
if len(args) == 0 { if len(args) == 0 {
return commandFailure("must provide a driver name") return commandFailure("Must provide a driver name.")
} }
driverName := args[0] driverName := args[0]
@ -102,18 +99,6 @@ func preRun(cmd *cobra.Command, args []string) error {
PkgName: viper.GetString("pkgname"), PkgName: viper.GetString("pkgname"),
} }
// BUG: https://github.com/spf13/viper/issues/200
// Look up the value of TableNames directly from PFlags in Cobra if we
// detect a malformed value coming out of viper.
// Once the bug is fixed we'll be able to move this into the init above
cmdConfig.TableNames = viper.GetStringSlice("tables")
if len(cmdConfig.TableNames) == 1 && strings.HasPrefix(cmdConfig.TableNames[0], "[") {
cmdConfig.TableNames, err = cmd.PersistentFlags().GetStringSlice("tables")
if err != nil {
return err
}
}
if viper.IsSet("postgres.dbname") { if viper.IsSet("postgres.dbname") {
cmdConfig.Postgres = PostgresConfig{ cmdConfig.Postgres = PostgresConfig{
User: viper.GetString("postgres.user"), User: viper.GetString("postgres.user"),
@ -142,7 +127,7 @@ func preRun(cmd *cobra.Command, args []string) error {
return commandFailure(err.Error()) return commandFailure(err.Error())
} }
} else if driverName == "postgres" { } else if driverName == "postgres" {
return errors.New("postgres driver requires a postgres section in the config") return errors.New("Postgres driver requires a postgres section in your config file.")
} }
cmdState, err = New(cmdConfig) cmdState, err = New(cmdConfig)

View file

@ -53,7 +53,7 @@ func New(config *Config) (*State, error) {
return nil, errors.Wrap(err, "unable to connect to the database") return nil, errors.Wrap(err, "unable to connect to the database")
} }
err = s.initTables(config.TableNames) err = s.initTables()
if err != nil { if err != nil {
return nil, errors.Wrap(err, "unable to initialize tables") return nil, errors.Wrap(err, "unable to initialize tables")
} }
@ -189,13 +189,10 @@ func (s *State) initDriver(driverName string) error {
return nil return nil
} }
// initTables will create a string slice out of the passed in table flag value // initTables retrieves all "public" schema table names from the database.
// if one is provided. If no flag is provided, it will attempt to connect to the func (s *State) initTables() error {
// database to retrieve all "public" table names, and build a slice out of that
// result.
func (s *State) initTables(tableNames []string) error {
var err error var err error
s.Tables, err = bdb.Tables(s.Driver, tableNames...) s.Tables, err = bdb.Tables(s.Driver)
if err != nil { if err != nil {
return errors.Wrap(err, "unable to fetch table data") return errors.Wrap(err, "unable to fetch table data")
} }