Added all command, added output file flag

This commit is contained in:
Patrick O'brien 2016-02-24 15:40:07 +10:00
parent 69adb1a552
commit 2ea3239ad1
7 changed files with 128 additions and 104 deletions

35
cmds/all.go Normal file
View file

@ -0,0 +1,35 @@
package cmds
import "github.com/spf13/cobra"
func init() {
SQLBoiler.AddCommand(allCmd)
allCmd.Run = allRun
}
var allCmd = &cobra.Command{
Use: "all",
Short: "Generate all templates from table definitions",
}
func allRun(cmd *cobra.Command, args []string) {
err := outHandler(generateStructs())
if err != nil {
errorQuit(err)
}
err = outHandler(generateDeletes())
if err != nil {
errorQuit(err)
}
err = outHandler(generateInserts())
if err != nil {
errorQuit(err)
}
err = outHandler(generateSelects())
if err != nil {
errorQuit(err)
}
}

View file

@ -1,9 +1,6 @@
package cmds
import (
"bytes"
"go/format"
"os"
"text/template"
"github.com/spf13/cobra"
@ -20,10 +17,9 @@ var deleteCmd = &cobra.Command{
}
func deleteRun(cmd *cobra.Command, args []string) {
out := generateDeletes()
for _, v := range out {
os.Stdout.Write(v)
err := outHandler(generateDeletes())
if err != nil {
errorQuit(err)
}
}
@ -36,25 +32,9 @@ func generateDeletes() [][]byte {
errorQuit(err)
}
var outputs [][]byte
for i := 0; i < len(cmdData.TablesInfo); i++ {
data := tplData{
TableName: cmdData.TableNames[i],
TableData: cmdData.TablesInfo[i],
}
var buf bytes.Buffer
if err = t.Execute(&buf, data); err != nil {
errorQuit(err)
}
out, err := format.Source(buf.Bytes())
if err != nil {
errorQuit(err)
}
outputs = append(outputs, out)
outputs, err := processTemplate(t)
if err != nil {
errorQuit(err)
}
return outputs

View file

@ -1,10 +1,7 @@
package cmds
import (
"bytes"
"fmt"
"go/format"
"os"
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
@ -22,10 +19,9 @@ var insertCmd = &cobra.Command{
}
func insertRun(cmd *cobra.Command, args []string) {
out := generateInserts()
for _, v := range out {
os.Stdout.Write(v)
err := outHandler(generateInserts())
if err != nil {
errorQuit(err)
}
}
@ -41,25 +37,9 @@ func generateInserts() [][]byte {
errorQuit(err)
}
var outputs [][]byte
for i := 0; i < len(cmdData.TablesInfo); i++ {
data := tplData{
TableName: cmdData.TableNames[i],
TableData: cmdData.TablesInfo[i],
}
var buf bytes.Buffer
if err = t.Execute(&buf, data); err != nil {
errorQuit(err)
}
out, err := format.Source(buf.Bytes())
if err != nil {
errorQuit(err)
}
outputs = append(outputs, out)
outputs, err := processTemplate(t)
if err != nil {
errorQuit(err)
}
return outputs

View file

@ -1,10 +1,7 @@
package cmds
import (
"bytes"
"fmt"
"go/format"
"os"
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
@ -22,10 +19,9 @@ var selectCmd = &cobra.Command{
}
func selectRun(cmd *cobra.Command, args []string) {
out := generateSelects()
for _, v := range out {
os.Stdout.Write(v)
err := outHandler(generateSelects())
if err != nil {
errorQuit(err)
}
}
@ -40,25 +36,9 @@ func generateSelects() [][]byte {
errorQuit(err)
}
var outputs [][]byte
for i := 0; i < len(cmdData.TablesInfo); i++ {
data := tplData{
TableName: cmdData.TableNames[i],
TableData: cmdData.TablesInfo[i],
}
var buf bytes.Buffer
if err = t.Execute(&buf, data); err != nil {
errorQuit(err)
}
out, err := format.Source(buf.Bytes())
if err != nil {
errorQuit(err)
}
outputs = append(outputs, out)
outputs, err := processTemplate(t)
if err != nil {
errorQuit(err)
}
return outputs

View file

@ -1,9 +1,12 @@
package cmds
import (
"bytes"
"fmt"
"go/format"
"os"
"strings"
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
)
@ -19,6 +22,53 @@ func errorQuit(err error) {
os.Exit(-1)
}
func processTemplate(t *template.Template) ([][]byte, error) {
var outputs [][]byte
for i := 0; i < len(cmdData.TablesInfo); i++ {
data := tplData{
TableName: cmdData.TableNames[i],
TableData: cmdData.TablesInfo[i],
}
var buf bytes.Buffer
if err := t.Execute(&buf, data); err != nil {
return nil, err
}
out, err := format.Source(buf.Bytes())
if err != nil {
return nil, err
}
outputs = append(outputs, out)
}
return outputs, nil
}
func outHandler(data [][]byte) error {
nl := []byte{'\n'}
var out *os.File
if cmdData.OutFile == nil {
out = os.Stdout
} else {
out = cmdData.OutFile
}
for _, v := range data {
if _, err := out.Write(v); err != nil {
return err
}
if _, err := out.Write(nl); err != nil {
return err
}
}
return nil
}
func makeGoColName(name string) string {
s := strings.Split(name, "_")

View file

@ -2,6 +2,7 @@ package cmds
import (
"errors"
"os"
"strings"
"github.com/pobri19/sqlboiler/dbdrivers"
@ -12,6 +13,7 @@ type CmdData struct {
TablesInfo [][]dbdrivers.DBTable
TableNames []string
DBDriver dbdrivers.DBDriver
OutFile *os.File
}
var cmdData *CmdData
@ -19,7 +21,9 @@ var cmdData *CmdData
func init() {
SQLBoiler.PersistentFlags().StringP("driver", "d", "", "The name of the driver in your config.toml")
SQLBoiler.PersistentFlags().StringP("table", "t", "", "A comma seperated list of table names")
SQLBoiler.PersistentFlags().StringP("out", "o", "", "The name of the output file")
SQLBoiler.PersistentPreRun = sqlBoilerPreRun
SQLBoiler.PersistentPostRun = sqlBoilerPostRun
}
var SQLBoiler = &cobra.Command{
@ -29,6 +33,11 @@ var SQLBoiler = &cobra.Command{
`Complete documentation is available at http://github.com/pobri19/sqlboiler`,
}
func sqlBoilerPostRun(cmd *cobra.Command, args []string) {
cmdData.OutFile.Close()
cmdData.DBDriver.Close()
}
func sqlBoilerPreRun(cmd *cobra.Command, args []string) {
var err error
cmdData = &CmdData{}
@ -88,4 +97,14 @@ func sqlBoilerPreRun(cmd *cobra.Command, args []string) {
cmdData.TablesInfo = append(cmdData.TablesInfo, tInfo)
}
// open the out file filehandle
outf := SQLBoiler.PersistentFlags().Lookup("out").Value.String()
if outf != "" {
var err error
cmdData.OutFile, err = os.Create(outf)
if err != nil {
errorQuit(err)
}
}
}

View file

@ -1,9 +1,6 @@
package cmds
import (
"bytes"
"go/format"
"os"
"text/template"
"github.com/spf13/cobra"
@ -20,10 +17,9 @@ var structCmd = &cobra.Command{
}
func structRun(cmd *cobra.Command, args []string) {
out := generateStructs()
for _, v := range out {
os.Stdout.Write(v)
err := outHandler(generateStructs())
if err != nil {
errorQuit(err)
}
}
@ -37,25 +33,9 @@ func generateStructs() [][]byte {
errorQuit(err)
}
var outputs [][]byte
for i := 0; i < len(cmdData.TablesInfo); i++ {
data := tplData{
TableName: cmdData.TableNames[i],
TableData: cmdData.TablesInfo[i],
}
var buf bytes.Buffer
if err = t.Execute(&buf, data); err != nil {
errorQuit(err)
}
out, err := format.Source(buf.Bytes())
if err != nil {
errorQuit(err)
}
outputs = append(outputs, out)
outputs, err := processTemplate(t)
if err != nil {
errorQuit(err)
}
return outputs