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

Allow windows users to pass \t to use as a tab delimiter #44

Merged
merged 1 commit into from
Jun 3, 2018
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
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 @@ -109,7 +127,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