-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changed the execution method to subcommand method #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/noborus/mdtsql" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// listCmd represents the list command | ||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
|
||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fileName := "" | ||
if len(args) >= 1 { | ||
fileName = args[0] | ||
} | ||
if err := analyzeDump(fileName, Caption); err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func analyzeDump(fileName string, caption bool) error { | ||
im, err := mdtsql.Analyze(fileName, caption) | ||
if err != nil { | ||
return err | ||
} | ||
im.Dump(os.Stdout) | ||
return nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(listCmd) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/noborus/trdsql" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// queryCmd represents the query command | ||
var queryCmd = &cobra.Command{ | ||
Use: "query", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
|
||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
Comment on lines
+14
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return exec(args) | ||
}, | ||
} | ||
|
||
func exec(args []string) error { | ||
if Debug { | ||
trdsql.EnableDebug() | ||
} | ||
query := strings.Join(args, " ") | ||
|
||
writer := newWriter(os.Stdout, os.Stderr) | ||
trd := trdsql.NewTRDSQL( | ||
trdsql.NewImporter( | ||
trdsql.InHeader(Header), | ||
trdsql.InPreRead(100), | ||
), | ||
trdsql.NewExporter(writer), | ||
) | ||
return trd.Exec(query) | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(queryCmd) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package mdtsql | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/noborus/trdsql" | ||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/extension" | ||
gast "github.com/yuin/goldmark/extension/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/renderer/html" | ||
"github.com/yuin/goldmark/text" | ||
) | ||
|
||
type MDTReader struct { | ||
tableName string | ||
caption bool | ||
names []string | ||
types []string | ||
tables []ast.Node | ||
body [][]interface{} | ||
source []byte | ||
} | ||
|
||
func NewMDTReader(reader io.Reader, opts *trdsql.ReadOpts) (trdsql.Reader, error) { | ||
opt := opts.InJQuery | ||
target := 0 | ||
if opt != "" { | ||
n, err := strconv.Atoi(opt) | ||
if err == nil { | ||
target = n | ||
} | ||
} | ||
gmd := goldmark.New( | ||
goldmark.WithExtensions(extension.GFM), | ||
goldmark.WithParserOptions( | ||
parser.WithAutoHeadingID(), | ||
), | ||
goldmark.WithRendererOptions( | ||
html.WithHardWraps(), | ||
html.WithXHTML(), | ||
), | ||
) | ||
source, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, err | ||
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider streaming the input or processing it in chunks to handle large files more efficiently and avoid potential memory issues. |
||
} | ||
|
||
parser := gmd.Parser() | ||
node := parser.Parse(text.NewReader(source)) | ||
|
||
r := MDTReader{} | ||
for n := node.FirstChild(); n != nil; n = n.NextSibling() { | ||
switch n.Kind() { | ||
case gast.KindTableHeader: | ||
i := 0 | ||
for c := n.FirstChild(); c != nil; c = c.NextSibling() { | ||
col := toText(c.Text(source)) | ||
if col == "" { | ||
col = fmt.Sprintf("c%d", i+1) | ||
} | ||
r.names = append(r.names, col) | ||
i++ | ||
} | ||
case gast.KindTableRow: | ||
row := []string{} | ||
for c := n.FirstChild(); c != nil; c = c.NextSibling() { | ||
rawText := []byte{} | ||
for i := 0; i < c.Lines().Len(); i++ { | ||
line := c.Lines().At(i) | ||
rawText = append(rawText, line.Value(source)...) | ||
} | ||
row = append(row, string(rawText)) | ||
} | ||
data := make([]interface{}, len(row)) | ||
for i, col := range row { | ||
data[i] = col | ||
} | ||
default: | ||
// fmt.Fprintf(os.Stderr, "unknown node:") | ||
// fmt.Fprintf(os.Stderr, "%v:%v\n", n.Kind(), n.Type()) | ||
} | ||
} | ||
r.source = source | ||
if err := r.parseNode(node, target); err != nil { | ||
return nil, err | ||
} | ||
|
||
for i, node := range r.tables { | ||
if i != target { | ||
continue | ||
} | ||
table, err := tableNode(r.source, node) | ||
if err != nil { | ||
return nil, err | ||
} | ||
r.names = table.names | ||
r.types = table.types | ||
r.body = table.body | ||
} | ||
|
||
return &r, nil | ||
} | ||
|
||
func (r *MDTReader) parseNode(node ast.Node, target int) error { | ||
switch node.Type() { | ||
case ast.TypeDocument: | ||
for n := node.FirstChild(); n != nil; n = n.NextSibling() { | ||
if err := r.parseNode(n, target); err != nil { | ||
return err | ||
} | ||
} | ||
case ast.TypeBlock: | ||
if node.Kind() == gast.KindTable { | ||
r.tables = append(r.tables, node) | ||
} | ||
|
||
switch node.Kind() { | ||
case ast.KindHeading, ast.KindParagraph: | ||
if r.caption { | ||
r.tableName = string(node.Text(r.source)) | ||
} | ||
} | ||
default: | ||
fmt.Fprintf(os.Stderr, "unknown node:") | ||
fmt.Fprintf(os.Stderr, "%v:%v\n", node.Kind(), node.Type()) | ||
} | ||
|
||
return nil | ||
Comment on lines
+109
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be cautious of potential stack overflow due to deep recursion in |
||
} | ||
|
||
func tableNode(source []byte, node ast.Node) (table, error) { | ||
t := table{} | ||
for n := node.FirstChild(); n != nil; n = n.NextSibling() { | ||
switch n.Kind() { | ||
case gast.KindTableHeader: | ||
i := 0 | ||
for c := n.FirstChild(); c != nil; c = c.NextSibling() { | ||
col := toText(c.Text(source)) | ||
if col == "" { | ||
col = fmt.Sprintf("c%d", i+1) | ||
} | ||
t.names = append(t.names, col) | ||
i++ | ||
} | ||
case gast.KindTableRow: | ||
row := []string{} | ||
for c := n.FirstChild(); c != nil; c = c.NextSibling() { | ||
rawText := []byte{} | ||
for i := 0; i < c.Lines().Len(); i++ { | ||
line := c.Lines().At(i) | ||
rawText = append(rawText, line.Value(source)...) | ||
} | ||
row = append(row, string(rawText)) | ||
} | ||
data := make([]interface{}, len(row)) | ||
for i, col := range row { | ||
data[i] = col | ||
} | ||
t.body = append(t.body, data) | ||
default: | ||
return t, fmt.Errorf("unknown node %v:%v", n.Kind(), n.Type()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve error handling by providing more context in the error message returned from |
||
} | ||
} | ||
t.types = make([]string, len(t.names)) | ||
for i := 0; i < len(t.names); i++ { | ||
t.types[i] = trdsql.DefaultDBType | ||
} | ||
return t, nil | ||
} | ||
|
||
func (t MDTReader) Names() ([]string, error) { | ||
return t.names, nil | ||
} | ||
|
||
func (t MDTReader) Types() ([]string, error) { | ||
return t.types, nil | ||
} | ||
|
||
func (t MDTReader) PreReadRow() [][]interface{} { | ||
return t.body | ||
} | ||
|
||
// ReadRow only returns EOF. | ||
func (t MDTReader) ReadRow(row []interface{}) ([]interface{}, error) { | ||
return nil, io.EOF | ||
} | ||
|
||
func toText(buf []byte) string { | ||
if len(buf) > 0 { | ||
return string(buf) | ||
} | ||
return "" | ||
Comment on lines
+193
to
+197
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
} | ||
|
||
func init() { | ||
trdsql.RegisterReaderFunc("MD", NewMDTReader) | ||
Comment on lines
+200
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid potential conflicts by ensuring the key "MD" used in |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
analyzeDump
function should handle the case wherefileName
is an empty string before proceeding with the analysis.