Move main back to root folder.
- Change everything to package main. - Make main a little more user friendly.
This commit is contained in:
parent
0bb847c0da
commit
56234e37a1
10 changed files with 55 additions and 34 deletions
|
@ -1,4 +1,4 @@
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
// Config for the running of the commands
|
// Config for the running of the commands
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
|
@ -5,40 +5,58 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/nullbio/sqlboiler"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
cmdState *State
|
||||||
|
cmdConfig *Config
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
viper.SetConfigName("sqlboiler")
|
viper.SetConfigName("sqlboiler")
|
||||||
viper.AddConfigPath("$HOME/.sqlboiler")
|
|
||||||
viper.AddConfigPath(".")
|
|
||||||
|
|
||||||
err = viper.ReadInConfig()
|
configHome := os.Getenv("XDG_CONFIG_HOME")
|
||||||
|
homePath := os.Getenv("HOME")
|
||||||
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to load config file: %s\n", err)
|
wd = "./"
|
||||||
os.Exit(-1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configPaths := []string{wd}
|
||||||
|
if len(configHome) > 0 {
|
||||||
|
configPaths = append(configPaths, filepath.Join(configHome, "sqlboiler"))
|
||||||
|
} else {
|
||||||
|
configPaths = append(configPaths, filepath.Join(homePath, ".config/sqlboiler"))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range configPaths {
|
||||||
|
viper.AddConfigPath(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find and read config
|
||||||
|
err = viper.ReadInConfig()
|
||||||
|
|
||||||
// Set up the cobra root command
|
// Set up the cobra root command
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "sqlboiler",
|
Use: "sqlboiler [options] <driver>",
|
||||||
Short: "SQL Boiler generates boilerplate structs and statements",
|
Short: "SQL Boiler generates boilerplate structs and statements",
|
||||||
Long: "SQL Boiler generates boilerplate structs and statements from the template files.\n" +
|
Long: "SQL Boiler generates boilerplate structs and statements from template files.\n" +
|
||||||
`Complete documentation is available at http://github.com/nullbio/sqlboiler`,
|
`Complete documentation is available at http://github.com/nullbio/sqlboiler`,
|
||||||
|
Example: `sqlboiler -o mymodels -p mymodelpackage postgres`,
|
||||||
PreRunE: preRun,
|
PreRunE: preRun,
|
||||||
RunE: run,
|
RunE: run,
|
||||||
PostRunE: postRun,
|
PostRunE: postRun,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the cobra root command flags
|
// Set up the cobra root command flags
|
||||||
rootCmd.PersistentFlags().StringP("driver", "d", "", "The name of the driver in your config.toml (mandatory)")
|
rootCmd.PersistentFlags().StringSliceP("table", "t", nil, "Tables to generate models for, all tables if empty")
|
||||||
rootCmd.PersistentFlags().StringP("table", "t", "", "A comma seperated list of table names")
|
rootCmd.PersistentFlags().StringP("output", "o", "output", "The name of the folder to output to")
|
||||||
rootCmd.PersistentFlags().StringP("folder", "f", "output", "The name of the output folder")
|
|
||||||
rootCmd.PersistentFlags().StringP("pkgname", "p", "model", "The name you wish to assign to your generated package")
|
rootCmd.PersistentFlags().StringP("pkgname", "p", "model", "The name you wish to assign to your generated package")
|
||||||
|
|
||||||
viper.BindPFlags(rootCmd.PersistentFlags())
|
viper.BindPFlags(rootCmd.PersistentFlags())
|
||||||
|
@ -49,26 +67,29 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var state *sqlboiler.State
|
|
||||||
var config *sqlboiler.Config
|
|
||||||
|
|
||||||
func preRun(cmd *cobra.Command, args []string) error {
|
func preRun(cmd *cobra.Command, args []string) error {
|
||||||
config = new(sqlboiler.Config)
|
if len(args) == 0 {
|
||||||
|
_ = cmd.Help()
|
||||||
|
fmt.Println("\nmust provide a driver")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
config.DriverName = viper.GetString("driver")
|
cmdConfig = new(Config)
|
||||||
config.TableName = viper.GetString("table")
|
|
||||||
config.OutFolder = viper.GetString("folder")
|
|
||||||
config.PkgName = viper.GetString("pkgname")
|
|
||||||
|
|
||||||
if len(config.DriverName) == 0 {
|
cmdConfig.DriverName = args[0]
|
||||||
|
cmdConfig.TableName = viper.GetString("table")
|
||||||
|
cmdConfig.OutFolder = viper.GetString("folder")
|
||||||
|
cmdConfig.PkgName = viper.GetString("pkgname")
|
||||||
|
|
||||||
|
if len(cmdConfig.DriverName) == 0 {
|
||||||
return errors.New("Must supply a driver flag.")
|
return errors.New("Must supply a driver flag.")
|
||||||
}
|
}
|
||||||
if len(config.OutFolder) == 0 {
|
if len(cmdConfig.OutFolder) == 0 {
|
||||||
return fmt.Errorf("No output folder specified.")
|
return fmt.Errorf("No output folder specified.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if viper.IsSet("postgres.dbname") {
|
if viper.IsSet("postgres.dbname") {
|
||||||
config.Postgres = sqlboiler.PostgresConfig{
|
cmdConfig.Postgres = PostgresConfig{
|
||||||
User: viper.GetString("postgres.user"),
|
User: viper.GetString("postgres.user"),
|
||||||
Pass: viper.GetString("postgres.pass"),
|
Pass: viper.GetString("postgres.pass"),
|
||||||
Host: viper.GetString("postgres.host"),
|
Host: viper.GetString("postgres.host"),
|
||||||
|
@ -78,14 +99,14 @@ func preRun(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
state, err = sqlboiler.New(config)
|
cmdState, err = New(cmdConfig)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(cmd *cobra.Command, args []string) error {
|
func run(cmd *cobra.Command, args []string) error {
|
||||||
return state.Run(true)
|
return cmdState.Run(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func postRun(cmd *cobra.Command, args []string) error {
|
func postRun(cmd *cobra.Command, args []string) error {
|
||||||
return state.Cleanup()
|
return cmdState.Cleanup()
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Package sqlboiler has types and methods useful for generating code that
|
// Package sqlboiler has types and methods useful for generating code that
|
||||||
// acts as a fully dynamic ORM might.
|
// acts as a fully dynamic ORM might.
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package sqlboiler
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue