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

hacking stdin #11

Merged
merged 3 commits into from
Mar 21, 2016
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
36 changes: 25 additions & 11 deletions csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"strings"
"unicode/utf8"

"github.com/cheggaaa/pb"
)

func containsDelimiter(col string) bool {
Expand Down Expand Up @@ -100,20 +102,27 @@ func copyCSVRows(i *Import, reader *csv.Reader, ignoreErrors bool, delimiter str

func importCSV(filename string, connStr string, schema string, tableName string, ignoreErrors bool, skipHeader bool, fields string, delimiter string) error {

file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()

db, err := connect(connStr, schema)
if err != nil {
return err
}
defer db.Close()

bar := NewProgressBar(file)
reader := csv.NewReader(io.TeeReader(file, bar))
var reader *csv.Reader
var bar *pb.ProgressBar
if filename != "" {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()

bar = NewProgressBar(file)
reader = csv.NewReader(io.TeeReader(file, bar))
} else {
reader = csv.NewReader(os.Stdin)
}

reader.Comma, _ = utf8.DecodeRuneInString(delimiter)
reader.LazyQuotes = true

Expand All @@ -129,9 +138,14 @@ func importCSV(filename string, connStr string, schema string, tableName string,
return err
}

bar.Start()
err, success, failed := copyCSVRows(i, reader, ignoreErrors, delimiter, columns)
bar.Finish()
var success, failed int
if filename != "" {
bar.Start()
err, success, failed = copyCSVRows(i, reader, ignoreErrors, delimiter, columns)
bar.Finish()
} else {
err, success, failed = copyCSVRows(i, reader, ignoreErrors, delimiter, columns)
}

if err != nil {
lineNumber := success + failed
Expand Down
35 changes: 23 additions & 12 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ func importJSONObject(filename string, connStr string, schema string, tableName
// The entire file is read into memory because we need to add
// it into the PostgreSQL transaction, this will hit memory limits
// for big JSON objects
bytes, err := ioutil.ReadFile(filename)
var bytes []byte
if filename == "" {
bytes, err = ioutil.ReadAll(os.Stdin)
} else {
bytes, err = ioutil.ReadFile(filename)
}
if err != nil {
return err
}
Expand All @@ -100,12 +105,6 @@ func importJSONObject(filename string, connStr string, schema string, tableName

func importJSON(filename string, connStr string, schema string, tableName string, ignoreErrors bool) error {

file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()

db, err := connect(connStr, schema)
if err != nil {
return err
Expand All @@ -117,12 +116,24 @@ func importJSON(filename string, connStr string, schema string, tableName string
return err
}

bar := NewProgressBar(file)
reader := bufio.NewReader(io.TeeReader(file, bar))
var success, failed int
if filename == "" {
reader := bufio.NewReader(os.Stdin)
err, success, failed = copyJSONRows(i, reader, ignoreErrors)
} else {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()

bar := NewProgressBar(file)
reader := bufio.NewReader(io.TeeReader(file, bar))
bar.Start()
err, success, failed = copyJSONRows(i, reader, ignoreErrors)
bar.Finish()
}

bar.Start()
err, success, failed := copyJSONRows(i, reader, ignoreErrors)
bar.Finish()

if err != nil {
lineNumber := success + failed
Expand Down
16 changes: 4 additions & 12 deletions pgfutter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func exitOnError(err error) {
func parseTableName(c *cli.Context, filename string) string {
tableName := c.GlobalString("table")
if tableName == "" {
if filename == "" {
// if no filename is not set, we reading stdin
filename = "stdin"
}
base := filepath.Base(filename)
ext := filepath.Ext(filename)
tableName = strings.TrimSuffix(base, ext)
Expand Down Expand Up @@ -92,10 +96,6 @@ func main() {
cli.CommandHelpTemplate = strings.Replace(cli.CommandHelpTemplate, "[arguments...]", "<json-file>", -1)

filename := c.Args().First()
if filename == "" {
cli.ShowCommandHelp(c, "json")
os.Exit(1)
}

ignoreErrors := c.GlobalBool("ignore-errors")
schema := c.GlobalString("schema")
Expand All @@ -113,10 +113,6 @@ func main() {
cli.CommandHelpTemplate = strings.Replace(cli.CommandHelpTemplate, "[arguments...]", "<json-file>", -1)

filename := c.Args().First()
if filename == "" {
cli.ShowCommandHelp(c, "jsonobj")
os.Exit(1)
}

schema := c.GlobalString("schema")
tableName := parseTableName(c, filename)
Expand Down Expand Up @@ -148,10 +144,6 @@ func main() {
cli.CommandHelpTemplate = strings.Replace(cli.CommandHelpTemplate, "[arguments...]", "<csv-file>", -1)

filename := c.Args().First()
if filename == "" {
cli.ShowCommandHelp(c, "csv")
os.Exit(1)
}

ignoreErrors := c.GlobalBool("ignore-errors")
schema := c.GlobalString("schema")
Expand Down