2016-06-12 03:37:36 +02:00
|
|
|
// Package main defines a command line interface for the sqlboiler package
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-08-01 04:04:48 +02:00
|
|
|
"errors"
|
2016-06-12 03:37:36 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-06-12 20:19:23 +02:00
|
|
|
"path/filepath"
|
2016-08-16 12:38:31 +02:00
|
|
|
"strings"
|
2016-06-12 03:37:36 +02:00
|
|
|
|
2016-06-19 23:47:50 +02:00
|
|
|
"github.com/kat-co/vala"
|
2016-06-12 03:37:36 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2016-06-12 20:19:23 +02:00
|
|
|
var (
|
|
|
|
cmdState *State
|
|
|
|
cmdConfig *Config
|
|
|
|
)
|
|
|
|
|
2016-06-12 03:37:36 +02:00
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
viper.SetConfigName("sqlboiler")
|
|
|
|
|
2016-06-12 20:19:23 +02:00
|
|
|
configHome := os.Getenv("XDG_CONFIG_HOME")
|
|
|
|
homePath := os.Getenv("HOME")
|
|
|
|
wd, err := os.Getwd()
|
2016-06-12 03:37:36 +02:00
|
|
|
if err != nil {
|
2016-06-12 20:19:23 +02:00
|
|
|
wd = "./"
|
2016-06-12 03:37:36 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:19:23 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:38:31 +02:00
|
|
|
// Ignore errors here, fallback to other validation methods.
|
|
|
|
// Users can use environment variables if a config is not found.
|
|
|
|
_ = viper.ReadInConfig()
|
2016-06-12 20:19:23 +02:00
|
|
|
|
2016-06-12 03:37:36 +02:00
|
|
|
// Set up the cobra root command
|
|
|
|
var rootCmd = &cobra.Command{
|
2016-06-14 16:52:58 +02:00
|
|
|
Use: "sqlboiler [flags] <driver>",
|
2016-08-15 14:42:40 +02:00
|
|
|
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" +
|
2016-08-09 09:59:30 +02:00
|
|
|
`Complete documentation is available at http://github.com/vattle/sqlboiler`,
|
2016-08-15 14:42:40 +02:00
|
|
|
Example: `sqlboiler postgres`,
|
2016-06-19 23:47:50 +02:00
|
|
|
PreRunE: preRun,
|
|
|
|
RunE: run,
|
|
|
|
PostRunE: postRun,
|
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
2016-06-12 03:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the cobra root command flags
|
2016-06-14 14:58:46 +02:00
|
|
|
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")
|
2016-08-30 08:00:54 +02:00
|
|
|
rootCmd.PersistentFlags().StringP("basedir", "b", "", "The base directory has the templates and templates_test folders")
|
2016-08-16 15:25:31 +02:00
|
|
|
rootCmd.PersistentFlags().StringSliceP("exclude", "x", nil, "Tables to be excluded from the generated package")
|
2016-08-16 15:54:26 +02:00
|
|
|
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug mode prints stack traces on error")
|
2016-09-02 03:22:56 +02:00
|
|
|
rootCmd.PersistentFlags().BoolP("no-tests", "", false, "Disable generated go test files")
|
2016-08-28 12:48:50 +02:00
|
|
|
rootCmd.PersistentFlags().BoolP("no-hooks", "", false, "Disable hooks feature for your models")
|
2016-08-28 16:12:37 +02:00
|
|
|
rootCmd.PersistentFlags().BoolP("no-auto-timestamps", "", false, "Disable automatic timestamps for created_at/updated_at")
|
2016-06-12 03:37:36 +02:00
|
|
|
|
2016-08-06 07:14:28 +02:00
|
|
|
viper.SetDefault("postgres.sslmode", "require")
|
|
|
|
viper.SetDefault("postgres.port", "5432")
|
2016-06-12 03:37:36 +02:00
|
|
|
viper.BindPFlags(rootCmd.PersistentFlags())
|
2016-08-06 07:14:28 +02:00
|
|
|
viper.AutomaticEnv()
|
2016-06-12 03:37:36 +02:00
|
|
|
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2016-06-19 23:47:50 +02:00
|
|
|
if e, ok := err.(commandFailure); ok {
|
2016-08-16 16:22:41 +02:00
|
|
|
fmt.Printf("Error: %v\n\n", string(e))
|
|
|
|
rootCmd.Help()
|
2016-08-16 15:54:26 +02:00
|
|
|
} else if !viper.GetBool("debug") {
|
2016-08-16 16:22:41 +02:00
|
|
|
fmt.Printf("Error: %v\n", err)
|
2016-08-16 15:54:26 +02:00
|
|
|
} else {
|
2016-08-16 16:22:41 +02:00
|
|
|
fmt.Printf("Error: %+v\n", err)
|
2016-06-19 23:47:50 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 00:34:57 +02:00
|
|
|
os.Exit(1)
|
2016-06-12 03:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 23:47:50 +02:00
|
|
|
type commandFailure string
|
|
|
|
|
|
|
|
func (c commandFailure) Error() string {
|
|
|
|
return string(c)
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:37:36 +02:00
|
|
|
func preRun(cmd *cobra.Command, args []string) error {
|
2016-06-13 00:34:57 +02:00
|
|
|
var err error
|
|
|
|
|
2016-06-12 20:19:23 +02:00
|
|
|
if len(args) == 0 {
|
2016-08-16 16:22:41 +02:00
|
|
|
return commandFailure("must provide a driver name")
|
2016-06-12 20:19:23 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 04:04:48 +02:00
|
|
|
driverName := args[0]
|
|
|
|
|
2016-06-13 00:34:57 +02:00
|
|
|
cmdConfig = &Config{
|
2016-08-28 16:12:37 +02:00
|
|
|
DriverName: driverName,
|
|
|
|
OutFolder: viper.GetString("output"),
|
|
|
|
PkgName: viper.GetString("pkgname"),
|
2016-09-02 03:22:56 +02:00
|
|
|
NoTests: viper.GetBool("no-tests"),
|
2016-08-28 16:12:37 +02:00
|
|
|
NoHooks: viper.GetBool("no-hooks"),
|
|
|
|
NoAutoTimestamps: viper.GetBool("no-auto-timestamps"),
|
2016-06-13 00:34:57 +02:00
|
|
|
}
|
2016-06-12 03:37:36 +02:00
|
|
|
|
2016-08-16 12:38:31 +02:00
|
|
|
// BUG: https://github.com/spf13/viper/issues/200
|
|
|
|
// Look up the value of ExcludeTables 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.ExcludeTables = viper.GetStringSlice("exclude")
|
|
|
|
if len(cmdConfig.ExcludeTables) == 1 && strings.HasPrefix(cmdConfig.ExcludeTables[0], "[") {
|
|
|
|
cmdConfig.ExcludeTables, err = cmd.PersistentFlags().GetStringSlice("exclude")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:37:36 +02:00
|
|
|
if viper.IsSet("postgres.dbname") {
|
2016-06-12 20:19:23 +02:00
|
|
|
cmdConfig.Postgres = PostgresConfig{
|
2016-07-12 00:17:49 +02:00
|
|
|
User: viper.GetString("postgres.user"),
|
|
|
|
Pass: viper.GetString("postgres.pass"),
|
|
|
|
Host: viper.GetString("postgres.host"),
|
|
|
|
Port: viper.GetInt("postgres.port"),
|
|
|
|
DBName: viper.GetString("postgres.dbname"),
|
|
|
|
SSLMode: viper.GetString("postgres.sslmode"),
|
2016-06-12 03:37:36 +02:00
|
|
|
}
|
2016-06-19 23:47:50 +02:00
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
// Set the default SSLMode value
|
|
|
|
if cmdConfig.Postgres.SSLMode == "" {
|
|
|
|
viper.Set("postgres.sslmode", "require")
|
|
|
|
cmdConfig.Postgres.SSLMode = viper.GetString("postgres.sslmode")
|
|
|
|
}
|
|
|
|
|
2016-06-19 23:47:50 +02:00
|
|
|
err = vala.BeginValidation().Validate(
|
|
|
|
vala.StringNotEmpty(cmdConfig.Postgres.User, "postgres.user"),
|
|
|
|
vala.StringNotEmpty(cmdConfig.Postgres.Host, "postgres.host"),
|
|
|
|
vala.Not(vala.Equals(cmdConfig.Postgres.Port, 0, "postgres.port")),
|
|
|
|
vala.StringNotEmpty(cmdConfig.Postgres.DBName, "postgres.dbname"),
|
2016-07-12 00:17:49 +02:00
|
|
|
vala.StringNotEmpty(cmdConfig.Postgres.SSLMode, "postgres.sslmode"),
|
2016-06-19 23:47:50 +02:00
|
|
|
).Check()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return commandFailure(err.Error())
|
|
|
|
}
|
2016-08-01 04:04:48 +02:00
|
|
|
} else if driverName == "postgres" {
|
2016-08-16 16:22:41 +02:00
|
|
|
return errors.New("postgres driver requires a postgres section in your config file")
|
2016-06-12 03:37:36 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 20:19:23 +02:00
|
|
|
cmdState, err = New(cmdConfig)
|
2016-06-12 03:37:36 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(cmd *cobra.Command, args []string) error {
|
2016-06-12 20:19:23 +02:00
|
|
|
return cmdState.Run(true)
|
2016-06-12 03:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func postRun(cmd *cobra.Command, args []string) error {
|
2016-06-12 20:19:23 +02:00
|
|
|
return cmdState.Cleanup()
|
2016-06-12 03:37:36 +02:00
|
|
|
}
|