2016-02-23 09:27:32 +01:00
|
|
|
package cmds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-02-23 13:38:24 +01:00
|
|
|
|
|
|
|
"github.com/pobri19/sqlboiler/dbdrivers"
|
2016-02-29 10:39:49 +01:00
|
|
|
"github.com/spf13/cobra"
|
2016-02-23 09:27:32 +01:00
|
|
|
)
|
|
|
|
|
2016-02-29 10:39:49 +01:00
|
|
|
// CobraRunFunc declares the cobra.Command.Run function definition
|
|
|
|
type CobraRunFunc func(cmd *cobra.Command, args []string)
|
|
|
|
|
|
|
|
// CmdData holds the table schema a slice of (column name, column type) slices.
|
|
|
|
// It also holds a slice of all of the table names sqlboiler is generating against,
|
|
|
|
// the database driver chosen by the driver flag at runtime, and a pointer to the
|
|
|
|
// output file, if one is specified with a flag.
|
|
|
|
type CmdData struct {
|
2016-03-01 16:53:56 +01:00
|
|
|
TablesInfo [][]dbdrivers.DBColumn
|
2016-02-29 10:39:49 +01:00
|
|
|
TableNames []string
|
2016-03-01 15:20:13 +01:00
|
|
|
PkgName string
|
|
|
|
OutFolder string
|
2016-02-29 10:39:49 +01:00
|
|
|
DBDriver dbdrivers.DBDriver
|
|
|
|
}
|
|
|
|
|
2016-02-24 09:53:34 +01:00
|
|
|
// tplData is used to pass data to the template
|
2016-02-23 13:38:24 +01:00
|
|
|
type tplData struct {
|
|
|
|
TableName string
|
2016-03-01 16:53:56 +01:00
|
|
|
TableData []dbdrivers.DBColumn
|
2016-02-23 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
2016-02-24 09:53:34 +01:00
|
|
|
// errorQuit displays an error message and then exits the application.
|
2016-02-23 09:27:32 +01:00
|
|
|
func errorQuit(err error) {
|
2016-02-24 07:34:19 +01:00
|
|
|
fmt.Println(fmt.Sprintf("Error: %s\n---\n\nRun 'sqlboiler --help' for usage.", err))
|
2016-02-23 09:27:32 +01:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
2016-02-29 10:39:49 +01:00
|
|
|
// defaultRun is the default function passed to the commands cobra.Command.Run.
|
|
|
|
// It will generate the specific commands template and send it to outHandler for output.
|
|
|
|
func defaultRun(cmd *cobra.Command, args []string) {
|
2016-03-01 15:20:13 +01:00
|
|
|
// Generate the template for every table
|
|
|
|
for i := 0; i < len(cmdData.TablesInfo); i++ {
|
|
|
|
data := tplData{
|
|
|
|
TableName: cmdData.TableNames[i],
|
|
|
|
TableData: cmdData.TablesInfo[i],
|
|
|
|
}
|
|
|
|
|
|
|
|
// outHandler takes a slice of byte slices, so append the Template
|
|
|
|
// execution output to a [][]byte before sending it to outHandler.
|
2016-03-01 16:15:32 +01:00
|
|
|
out := [][]byte{generateTemplate(cmd.Name(), &data)}
|
2016-03-01 15:20:13 +01:00
|
|
|
|
|
|
|
err := outHandler(out, &data)
|
|
|
|
if err != nil {
|
|
|
|
errorQuit(fmt.Errorf("Unable to generate the template for command %s: %s", cmd.Name(), err))
|
|
|
|
}
|
2016-02-24 06:40:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 09:53:34 +01:00
|
|
|
// outHandler loops over the slice of byte slices, outputting them to either
|
|
|
|
// the OutFile if it is specified with a flag, or to Stdout if no flag is specified.
|
2016-03-01 15:20:13 +01:00
|
|
|
func outHandler(output [][]byte, data *tplData) error {
|
2016-02-24 06:40:07 +01:00
|
|
|
nl := []byte{'\n'}
|
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
if cmdData.OutFolder == "" {
|
|
|
|
for _, v := range output {
|
|
|
|
if _, err := os.Stdout.Write(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-24 06:40:07 +01:00
|
|
|
|
2016-03-01 15:20:13 +01:00
|
|
|
if _, err := os.Stdout.Write(nl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // If not using stdout, attempt to create the model file.
|
|
|
|
path := cmdData.OutFolder + "/" + data.TableName + ".go"
|
|
|
|
out, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
errorQuit(fmt.Errorf("Unable to create output file %s: %s", path, err))
|
2016-02-24 06:40:07 +01:00
|
|
|
}
|
2016-03-01 15:20:13 +01:00
|
|
|
|
|
|
|
// Combine the slice of slice into a single byte slice.
|
|
|
|
var newOutput []byte
|
|
|
|
for _, v := range output {
|
|
|
|
newOutput = append(newOutput, v...)
|
|
|
|
newOutput = append(newOutput, nl...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := out.Write(newOutput); err != nil {
|
2016-02-24 06:40:07 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|