Skip to content

Commit

Permalink
Merge pull request #84 from k1LoW/fix-config
Browse files Browse the repository at this point in the history
Fix config.Config internal
  • Loading branch information
k1LoW authored Mar 10, 2019
2 parents 0f8bef3 + 6484c6a commit e9f6ba5
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 59 deletions.
31 changes: 28 additions & 3 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/k1LoW/tbls/config"
"github.com/k1LoW/tbls/datasource"
"github.com/k1LoW/tbls/output/md"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -47,7 +48,13 @@ var diffCmd = &cobra.Command{
configPath = additionalDataPath
}

err = c.Load(configPath, args)
options, err := loadDiffArgs(args)
if err != nil {
printError(err)
os.Exit(1)
}

err = c.Load(configPath, options...)
if err != nil {
printError(err)
os.Exit(1)
Expand All @@ -65,15 +72,15 @@ var diffCmd = &cobra.Command{
os.Exit(1)
}

if sort {
if c.Format.Sort {
err = s.Sort()
if err != nil {
printError(err)
os.Exit(1)
}
}

diff, err := md.Diff(s, c.DocPath, adjust, erFormat)
diff, err := md.Diff(s, c)
if err != nil {
printError(err)
os.Exit(2)
Expand All @@ -85,6 +92,24 @@ var diffCmd = &cobra.Command{
},
}

func loadDiffArgs(args []string) ([]config.Option, error) {
options := []config.Option{}
if len(args) > 2 {
return options, errors.WithStack(errors.New("too many arguments"))
}
options = append(options, config.Adjust(adjust))
options = append(options, config.Sort(sort))
options = append(options, config.ERFormat(erFormat))
if len(args) == 2 {
options = append(options, config.DSN(args[0]))
options = append(options, config.DocPath(args[1]))
}
if len(args) == 1 {
options = append(options, config.DSN(args[0]))
}
return options, nil
}

func init() {
rootCmd.AddCommand(diffCmd)
diffCmd.Flags().BoolVarP(&sort, "sort", "", false, "sort")
Expand Down
47 changes: 37 additions & 10 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ var docCmd = &cobra.Command{
configPath = additionalDataPath
}

err = c.Load(configPath, args)
options, err := loadDocArgs(args)
if err != nil {
printError(err)
os.Exit(1)
}

err = c.Load(configPath, options...)
if err != nil {
printError(err)
os.Exit(1)
Expand All @@ -75,26 +81,26 @@ var docCmd = &cobra.Command{
os.Exit(1)
}

if sort {
if c.Format.Sort {
err = s.Sort()
if err != nil {
printError(err)
os.Exit(1)
}
}

if !withoutER {
if !c.ER.Skip {
_, err = exec.Command("which", "dot").Output()
if err == nil {
err := withDot(s, c.DocPath, force)
err := withDot(s, c, force)
if err != nil {
printError(err)
os.Exit(1)
}
}
}

err = md.Output(s, c.DocPath, force, adjust, erFormat)
err = md.Output(s, c, force)

if err != nil {
printError(err)
Expand All @@ -103,7 +109,9 @@ var docCmd = &cobra.Command{
},
}

func withDot(s *schema.Schema, outputPath string, force bool) error {
func withDot(s *schema.Schema, c *config.Config, force bool) error {
erFormat := c.ER.Format
outputPath := c.DocPath
fullPath, err := filepath.Abs(outputPath)
if err != nil {
return errors.WithStack(err)
Expand All @@ -120,9 +128,9 @@ func withDot(s *schema.Schema, outputPath string, force bool) error {

fmt.Printf("%s\n", filepath.Join(outputPath, erFileName))
tmpfile, _ := ioutil.TempFile("", "tblstmp")
c := exec.Command("dot", dotFormatOption, "-o", filepath.Join(fullPath, erFileName), tmpfile.Name())
cmd := exec.Command("dot", dotFormatOption, "-o", filepath.Join(fullPath, erFileName), tmpfile.Name())
var stderr bytes.Buffer
c.Stderr = &stderr
cmd.Stderr = &stderr

dot := new(dot.Dot)

Expand All @@ -137,7 +145,7 @@ func withDot(s *schema.Schema, outputPath string, force bool) error {
os.Remove(tmpfile.Name())
return errors.WithStack(err)
}
err = c.Run()
err = cmd.Run()
if err != nil {
os.Remove(tmpfile.Name())
return errors.WithStack(errors.Wrap(err, stderr.String()))
Expand Down Expand Up @@ -176,6 +184,25 @@ func withDot(s *schema.Schema, outputPath string, force bool) error {
return nil
}

func loadDocArgs(args []string) ([]config.Option, error) {
options := []config.Option{}
if len(args) > 2 {
return options, errors.WithStack(errors.New("too many arguments"))
}
options = append(options, config.Adjust(adjust))
options = append(options, config.Sort(sort))
options = append(options, config.ERFormat(erFormat))
options = append(options, config.ERSkip(withoutER))
if len(args) == 2 {
options = append(options, config.DSN(args[0]))
options = append(options, config.DocPath(args[1]))
}
if len(args) == 1 {
options = append(options, config.DSN(args[0]))
}
return options, nil
}

func outputErExists(s *schema.Schema, path string) bool {
// schema.png
erFileName := fmt.Sprintf("schema.%s", erFormat)
Expand All @@ -197,7 +224,7 @@ func init() {
docCmd.Flags().BoolVarP(&force, "force", "f", false, "force")
docCmd.Flags().BoolVarP(&sort, "sort", "", false, "sort")
docCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
docCmd.Flags().StringVarP(&erFormat, "er-format", "t", "png", "ER diagrams output format [png, svg, jpg, ...]")
docCmd.Flags().StringVarP(&erFormat, "er-format", "t", "", fmt.Sprintf("ER diagrams output format [png, svg, jpg, ...]. default: %s", config.DefaultERFormat))
docCmd.Flags().BoolVarP(&withoutER, "without-er", "", false, "no generate ER diagrams")
docCmd.Flags().BoolVarP(&adjust, "adjust-table", "j", false, "adjust column width of table")
docCmd.Flags().StringVarP(&additionalDataPath, "add", "a", "", "additional schema data path (deprecated, use `config`)")
Expand Down
24 changes: 23 additions & 1 deletion cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/k1LoW/tbls/config"
"github.com/k1LoW/tbls/datasource"
"github.com/labstack/gommon/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -43,7 +44,13 @@ var lintCmd = &cobra.Command{
os.Exit(1)
}

err = c.Load(configPath, args)
options, err := loadLintArgs(args)
if err != nil {
printError(err)
os.Exit(1)
}

err = c.Load(configPath, options...)
if err != nil {
printError(err)
os.Exit(1)
Expand Down Expand Up @@ -83,6 +90,21 @@ var lintCmd = &cobra.Command{
},
}

func loadLintArgs(args []string) ([]config.Option, error) {
options := []config.Option{}
if len(args) > 2 {
return options, errors.WithStack(errors.New("too many arguments"))
}
if len(args) == 2 {
options = append(options, config.DSN(args[0]))
options = append(options, config.DocPath(args[1]))
}
if len(args) == 1 {
options = append(options, config.DSN(args[0]))
}
return options, nil
}

func init() {
rootCmd.AddCommand(lintCmd)
lintCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
Expand Down
5 changes: 3 additions & 2 deletions cmd/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var outCmd = &cobra.Command{
configPath = additionalDataPath
}

err = c.Load(configPath, args)
err = c.Load(configPath, config.DSN(args[0]), config.Sort(sort))
if err != nil {
printError(err)
os.Exit(1)
Expand All @@ -80,7 +80,7 @@ var outCmd = &cobra.Command{
os.Exit(1)
}

if sort {
if c.Format.Sort {
err = s.Sort()
if err != nil {
printError(err)
Expand Down Expand Up @@ -122,6 +122,7 @@ var outCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(outCmd)
outCmd.Flags().BoolVarP(&sort, "sort", "", false, "sort")
outCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
outCmd.Flags().StringVarP(&format, "format", "t", "json", "output format")
outCmd.Flags().StringVar(&tableName, "table", "", "table name")
Expand Down
Loading

0 comments on commit e9f6ba5

Please sign in to comment.