Skip to content

Commit

Permalink
Merge pull request #11 from rrbarbosa/stdin
Browse files Browse the repository at this point in the history
Read input from stdin
  • Loading branch information
lukasmartinelli committed Mar 21, 2016
2 parents b2c9199 + a977143 commit cfe524c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
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

0 comments on commit cfe524c

Please sign in to comment.