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
// no tables are provided.
func Tables(db Interface, names ...string) ([]Table, error) {
func Tables(db Interface) ([]Table, error) {
var err error
if len(names) == 0 {
if names, err = db.TableNames(); err != nil {
return nil, errors.Wrap(err, "unable to get table names")
}
names, err := db.TableNames()
if err != nil {
return nil, errors.Wrap(err, "unable to get table names")
}
var tables []Table

27
main.go
View file

@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/kat-co/vala"
"github.com/spf13/cobra"
@ -48,10 +47,10 @@ func main() {
// Set up the cobra root command
var rootCmd = &cobra.Command{
Use: "sqlboiler [flags] <driver>",
Short: "SQL Boiler generates boilerplate structs and statements",
Long: "SQL Boiler generates boilerplate structs and statements from template files.\n" +
Short: "SQL Boiler generates an ORM tailored to your database schema.",
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`,
Example: `sqlboiler -o models -p models postgres`,
Example: `sqlboiler postgres`,
PreRunE: preRun,
RunE: run,
PostRunE: postRun,
@ -60,7 +59,6 @@ func main() {
}
// 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("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 e, ok := err.(commandFailure); ok {
rootCmd.HelpFunc()
fmt.Printf("\n%s\n", string(e))
fmt.Printf("%s\n", string(e))
return
}
@ -91,7 +88,7 @@ func preRun(cmd *cobra.Command, args []string) error {
var err error
if len(args) == 0 {
return commandFailure("must provide a driver name")
return commandFailure("Must provide a driver name.")
}
driverName := args[0]
@ -102,18 +99,6 @@ func preRun(cmd *cobra.Command, args []string) error {
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") {
cmdConfig.Postgres = PostgresConfig{
User: viper.GetString("postgres.user"),
@ -142,7 +127,7 @@ func preRun(cmd *cobra.Command, args []string) error {
return commandFailure(err.Error())
}
} 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)

View file

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