Skip to content
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

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,3 @@ func queryExec(fileName string, query string, caption bool) error {

return mdtsql.MarkdownQuery(fileName, query, caption, w)
}

func analyzeDump(fileName string, caption bool) error {
im, err := mdtsql.Analyze(fileName, caption)
if err != nil {
return err
}
im.Dump(os.Stdout)
return nil
}
43 changes: 43 additions & 0 deletions cmd/list.go
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
Comment on lines +32 to +38
Copy link
Contributor

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 where fileName is an empty string before proceeding with the analysis.

}

func init() {
rootCmd.AddCommand(listCmd)
}
45 changes: 45 additions & 0 deletions cmd/query.go
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Short and Long descriptions for queryCmd are placeholders and should be updated to accurately describe the command's functionality.

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)
}
15 changes: 1 addition & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"io"
"log"
"os"
"strings"

Expand All @@ -25,19 +24,7 @@ The result can be output to CSV, JSON, LTSV, YAML, Markdown, etc.`,
fmt.Printf("mdtsql version %s rev:%s\n", Version, Revision)
return
}
fileName := ""
if len(args) >= 1 {
fileName = args[0]
}
var err error
if Query != "" {
err = queryExec(fileName, Query, Caption)
} else {
err = analyzeDump(fileName, Caption)
}
if err != nil {
log.Fatal(err)
}
cmd.Help()
},
}

Expand Down
202 changes: 202 additions & 0 deletions reader.go
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be cautious of potential stack overflow due to deep recursion in parseNode. Consider using an iterative approach if the input document can be deeply nested.

}

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())
Copy link
Contributor

Choose a reason for hiding this comment

The 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 tableNode when encountering an unknown node.

}
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that toText correctly handles different text encodings. Consider using a library that can detect and convert character encodings to UTF-8.

}

func init() {
trdsql.RegisterReaderFunc("MD", NewMDTReader)
Comment on lines +200 to +201
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid potential conflicts by ensuring the key "MD" used in trdsql.RegisterReaderFunc is unique or configurable.

}
7 changes: 0 additions & 7 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ func (t table) ReadRow(row []interface{}) ([]interface{}, error) {
return nil, io.EOF
}

func toText(buf []byte) string {
if len(buf) > 0 {
return string(buf)
}
return ""
}

func (im *Importer) tableNode(node ast.Node) table {
t := table{}
for n := node.FirstChild(); n != nil; n = n.NextSibling() {
Expand Down