Skip to content

Commit

Permalink
[FIX] Log fatal errors and migrate to urfave/cli
Browse files Browse the repository at this point in the history
Errors are properly logged to stderr and github.com/codegangsta/cli was replaced by github.com/urfave/cli.
This includes the open pull request at lukasmartinelli#67
  • Loading branch information
romnnn committed Nov 20, 2019
1 parent 3cc9f2f commit 1c63947
Showing 1 changed file with 36 additions and 39 deletions.
75 changes: 36 additions & 39 deletions pgfutter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ import (
"path/filepath"
"strings"

"github.com/codegangsta/cli"
"github.com/urfave/cli"
)

func exitOnError(err error) {
log.SetFlags(0)
if err != nil {
log.Fatalln(err)
}
}

//Parse table to copy to from given filename or passed flags
func parseTableName(c *cli.Context, filename string) string {
tableName := c.GlobalString("table")
tableName := c.String("table")
if tableName == "" {
if filename == "" {
// if no filename is not set, we reading stdin
Expand All @@ -33,7 +26,7 @@ func parseTableName(c *cli.Context, filename string) string {

func getDataType(c *cli.Context) string {
dataType := "json"
if c.GlobalBool("jsonb") {
if c.Bool("jsonb") {
dataType = "jsonb"
}

Expand All @@ -46,62 +39,62 @@ func main() {
app.Version = "1.2"
app.Usage = "Import JSON and CSV into PostgreSQL the easy way"
app.Flags = []cli.Flag{
cli.StringFlag{
&cli.StringFlag{
Name: "dbname, db",
Value: "postgres",
Usage: "database to connect to",
EnvVar: "DB_NAME",
EnvVars: []string{"DB_NAME"},
},
cli.StringFlag{
&cli.StringFlag{
Name: "host",
Value: "localhost",
Usage: "host name",
EnvVar: "DB_HOST",
EnvVars: []string{"DB_HOST"},
},
cli.StringFlag{
&cli.StringFlag{
Name: "port",
Value: "5432",
Usage: "port",
EnvVar: "DB_PORT",
EnvVars: []string{"DB_PORT"},
},
cli.StringFlag{
&cli.StringFlag{
Name: "username, user",
Value: "postgres",
Usage: "username",
EnvVar: "DB_USER",
EnvVars: []string{"DB_USER"},
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "ssl",
Usage: "require ssl mode",
},
cli.StringFlag{
&cli.StringFlag{
Name: "pass, pw",
Value: "",
Usage: "password",
EnvVar: "DB_PASS",
EnvVars: []string{"DB_PASS"},
},
cli.StringFlag{
&cli.StringFlag{
Name: "schema",
Value: "import",
Usage: "database schema",
EnvVar: "DB_SCHEMA",
EnvVars: []string{"DB_SCHEMA"},
},
cli.StringFlag{
&cli.StringFlag{
Name: "table",
Usage: "destination table",
EnvVar: "DB_TABLE",
EnvVars: []string{"DB_TABLE"},
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "jsonb",
Usage: "use JSONB data type",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "ignore-errors",
Usage: "halt transaction on inconsistencies",
},
}

app.Commands = []cli.Command{
app.Commands = []*cli.Command{
{
Name: "json",
Usage: "Import newline-delimited JSON objects into database",
Expand All @@ -110,8 +103,8 @@ func main() {

filename := c.Args().First()

ignoreErrors := c.GlobalBool("ignore-errors")
schema := c.GlobalString("schema")
ignoreErrors := c.Bool("ignore-errors")
schema := c.String("schema")
tableName := parseTableName(c, filename)
dataType := getDataType(c)

Expand All @@ -124,29 +117,29 @@ func main() {
Name: "csv",
Usage: "Import CSV into database",
Flags: []cli.Flag{
cli.BoolFlag{
&cli.BoolFlag{
Name: "excel",
Usage: "support problematic Excel 2008 and Excel 2011 csv line endings",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "skip-header",
Usage: "skip header row",
},
cli.StringFlag{
&cli.StringFlag{
Name: "fields",
Usage: "comma separated field names if no header row",
},
cli.StringFlag{
&cli.StringFlag{
Name: "delimiter, d",
Value: ",",
Usage: "field delimiter",
},
cli.StringFlag{
&cli.StringFlag{
Name: "null-delimiter, nd",
Value: "\\N",
Usage: "null delimiter",
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "skip-parse-delimiter",
Usage: "skip parsing escape sequences in the given delimiter",
},
Expand All @@ -156,8 +149,8 @@ func main() {

filename := c.Args().First()

ignoreErrors := c.GlobalBool("ignore-errors")
schema := c.GlobalString("schema")
ignoreErrors := c.Bool("ignore-errors")
schema := c.String("schema")
tableName := parseTableName(c, filename)

skipHeader := c.Bool("skip-header")
Expand All @@ -173,5 +166,9 @@ func main() {
},
}

app.Run(os.Args)
err := app.Run(os.Args)
if err != nil {
log.SetFlags(0)
log.Fatal(err)
}
}

0 comments on commit 1c63947

Please sign in to comment.