Add WithoutHeader and Silent options

This commit is contained in:
nxshock 2023-02-11 22:07:58 +05:00
parent 3606f38890
commit 3f2a2e2b44

47
main.go
View File

@ -73,6 +73,12 @@ var app = &cli.App{
Name: "unknowncolumnnames", Name: "unknowncolumnnames",
Usage: "insert to table with unknown column names", Usage: "insert to table with unknown column names",
}, },
&cli.BoolFlag{
Name: "withoutheader",
Usage: "file does not contains a header"},
&cli.BoolFlag{
Name: "silent",
Usage: "silens progress output"},
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
if len(strings.Split(c.String("table"), ".")) != 2 { if len(strings.Split(c.String("table"), ".")) != 2 {
@ -146,23 +152,26 @@ func processReader(c *cli.Context, r io.Reader) error {
reader.Comma = []rune(c.String("comma"))[0] reader.Comma = []rune(c.String("comma"))[0]
} }
header, err := reader.Read() var header []string
if err != nil { if !c.Bool("withoutheader") {
return fmt.Errorf("read header: %v", err) header, err = reader.Read()
} if err != nil {
return fmt.Errorf("read header: %v", err)
headerList := `"`
for i, v := range header {
if c.String("fields")[i] == ' ' {
continue
} }
headerList += v headerList := `"`
for i, v := range header {
if c.String("fields")[i] == ' ' {
continue
}
if i+1 < len(header) { headerList += v
headerList += `", "`
} else { if i+1 < len(header) {
headerList += `"` headerList += `", "`
} else {
headerList += `"`
}
} }
} }
@ -207,7 +216,7 @@ func processReader(c *cli.Context, r io.Reader) error {
} }
if c.Bool("create") { if c.Bool("create") {
err = createTable(tx, c.String("table"), header, c.String("fields"), c.Bool("overwrite")) err = createTable(tx, c.String("table"), neededHeader, c.String("fields"), c.Bool("overwrite"))
if err != nil { if err != nil {
_ = tx.Rollback() _ = tx.Rollback()
return fmt.Errorf("create table: %v", err) return fmt.Errorf("create table: %v", err)
@ -225,7 +234,9 @@ func processReader(c *cli.Context, r io.Reader) error {
n := 0 n := 0
for { for {
if n%100000 == 0 { if n%100000 == 0 {
fmt.Fprintf(os.Stderr, "Processed %d records...\r", n) if !c.Bool("silent") {
fmt.Fprintf(os.Stderr, "Processed %d records...\r", n)
}
} }
record, err := reader.Read() record, err := reader.Read()
@ -274,7 +285,9 @@ func processReader(c *cli.Context, r io.Reader) error {
_ = tx.Rollback() _ = tx.Rollback()
return fmt.Errorf("calc rows affected: %v", err) return fmt.Errorf("calc rows affected: %v", err)
} }
fmt.Fprintf(os.Stderr, "Processed %d records. \n", rowsAffected) if !c.Bool("silent") {
fmt.Fprintf(os.Stderr, "Processed %d records. \n", rowsAffected)
}
err = stmt.Close() err = stmt.Close()
if err != nil { if err != nil {