Skip to content

Commit

Permalink
Merge pull request #44 from toneill818/master
Browse files Browse the repository at this point in the history
Allow windows users to pass \t to use as a tab delimiter
  • Loading branch information
lukasmartinelli authored Jun 3, 2018
2 parents ce73ea0 + abe6c18 commit 9840b30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 19 additions & 1 deletion csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ func containsDelimiter(col string) bool {
strings.Contains(col, "^") || strings.Contains(col, "~")
}

// Parse the delimiter for an escape sequence. This allows windows users to pass
// in \t since they cannot pass "`t" or "$Tab" to the program.
func parseDelimiter(delim string, skip bool) string {
if !strings.HasPrefix(delim, "\\") || skip {
return delim
}
switch delim {
case "\\t":
{
return "\t"
}
default:
{
return delim
}
}
}

// Parse columns from first header row or from flags
func parseColumns(reader *csv.Reader, skipHeader bool, fields string) ([]string, error) {
var err error
Expand Down Expand Up @@ -111,7 +129,7 @@ func importCSV(filename string, connStr string, schema string, tableName string,
defer db.Close()

var reader *csv.Reader
var bar *pb.ProgressBar
var bar *pb.ProgressBar
if filename != "" {
file, err := os.Open(filename)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions pgfutter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -154,6 +155,10 @@ func main() {
Value: ",",
Usage: "field delimiter",
},
cli.BoolFlag{
Name: "skip-parse-delimiter",
Usage: "skip parsing escape sequences in the given delimiter",
},
},
Action: func(c *cli.Context) {
cli.CommandHelpTemplate = strings.Replace(cli.CommandHelpTemplate, "[arguments...]", "<csv-file>", -1)
Expand All @@ -166,8 +171,9 @@ func main() {

skipHeader := c.Bool("skip-header")
fields := c.String("fields")
delimiter := c.String("delimiter")

skipParseheader := c.Bool("skip-parse-delimiter")
delimiter := parseDelimiter(c.String("delimiter"), skipParseheader)
fmt.Println(delimiter)
connStr := parseConnStr(c)
err := importCSV(filename, connStr, schema, tableName, ignoreErrors, skipHeader, fields, delimiter)
exitOnError(err)
Expand Down

0 comments on commit 9840b30

Please sign in to comment.