Skip to content

Commit

Permalink
feat: make output json
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jun 17, 2021
1 parent 0a2fa38 commit 4f11fef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ sqling.png
*.puml
_fixtures/platform.sql
output/
output.json
41 changes: 35 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
package cmd

import (
"encoding/json"
"fmt"
"github.com/inherd/sqling/model"
. "github.com/inherd/sqling/parser"
. "github.com/inherd/sqling/render"
"github.com/spf13/cobra"
"io/ioutil"
"os"
)

type SqlingJson struct {
Structs []model.CocoStruct
Refs []model.CocoRef
}

var (
path string
output_type string
rootCmd = &cobra.Command{
path string
outputType string
rootCmd = &cobra.Command{
Use: "sqling",
Short: "Sqling is a modeling tool to build from SQL file",
Long: `Sqling is a modeling tool to build from SQL file.`,
Run: func(cmd *cobra.Command, args []string) {
if len(path) < 0 {
fmt.Println("not input path")
return
}
dat, err := ioutil.ReadFile(path)
Check(err)

sql := string(dat)
parseSql, refs := ParseSql(sql)
structs, refs := ParseSql(sql)

if outputType == "json" {
output := &SqlingJson{
Structs: structs,
Refs: refs,
}

str, err := json.MarshalIndent(output, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(str))

err = ioutil.WriteFile("output.json", str, 0644)
return
}

Write(parseSql, refs)
Write(structs, refs)
},
}
)
Expand All @@ -38,7 +67,7 @@ func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVarP(&path, "input", "i", "", "input file")
rootCmd.PersistentFlags().StringVarP(&output_type, "output_type", "t", "puml", "output file type, support for puml, json")
rootCmd.PersistentFlags().StringVarP(&outputType, "output_type", "t", "puml", "output file type, support for puml, json")
}

func initConfig() {
Expand Down

0 comments on commit 4f11fef

Please sign in to comment.