diff --git a/args.go b/args.go index 5cc87bf4ba7..070e8b1c945 100644 --- a/args.go +++ b/args.go @@ -6,7 +6,6 @@ import ( "os" "github.com/alecthomas/kingpin" - "github.com/xo/usql/text" ) @@ -19,24 +18,21 @@ type CommandOrFile struct { // Args are the command line arguments. type Args struct { - DSN string - + DSN string CommandOrFiles []CommandOrFile Out string ForcePassword bool NoPassword bool NoRC bool SingleTransaction bool - - Variables []string - PVariables []string + Variables []string + PVariables []string } func (args *Args) Next() (string, bool, error) { if len(args.CommandOrFiles) == 0 { return "", false, io.EOF } - cmd := args.CommandOrFiles[0] args.CommandOrFiles = args.CommandOrFiles[1:] return cmd.Value, cmd.Command, nil @@ -85,16 +81,12 @@ func (p pset) IsCumulative() bool { func NewArgs() *Args { args := &Args{} - // set usage template kingpin.UsageTemplate(text.UsageTemplate()) - kingpin.Arg("dsn", "database url").StringVar(&args.DSN) - // command / file flags kingpin.Flag("command", "run only single command (SQL or internal) and exit").Short('c').SetValue(commandOrFile{args, true}) kingpin.Flag("file", "execute commands from file and exit").Short('f').SetValue(commandOrFile{args, false}) - // general flags kingpin.Flag("no-password", "never prompt for password").Short('w').BoolVar(&args.NoPassword) kingpin.Flag("no-rc", "do not read start up file").Short('X').BoolVar(&args.NoRC) @@ -102,15 +94,12 @@ func NewArgs() *Args { kingpin.Flag("password", "force password prompt (should happen automatically)").Short('W').BoolVar(&args.ForcePassword) kingpin.Flag("single-transaction", "execute as a single transaction (if non-interactive)").Short('1').BoolVar(&args.SingleTransaction) kingpin.Flag("set", "set variable NAME to VALUE").Short('v').PlaceHolder(", --variable=NAME=VALUE").StringsVar(&args.Variables) - // pset kingpin.Flag("pset", `set printing option VAR to ARG (see \pset command)`).Short('P').PlaceHolder("VAR[=ARG]").StringsVar(&args.PVariables) - // pset flags kingpin.Flag("field-separator", `field separator for unaligned output (default, "|")`).Short('F').SetValue(pset{args, []string{"fieldsep=%q", "fieldsep_zero=off"}}) kingpin.Flag("record-separator", `record separator for unaligned output (default, \n)`).Short('R').SetValue(pset{args, []string{"recordsep=%q", "recordsep_zero=off"}}) kingpin.Flag("table-attr", "set HTML table tag attributes (e.g., width, border)").Short('T').SetValue(pset{args, []string{"tableattr=%q"}}) - type psetconfig struct { long string short rune @@ -138,22 +127,17 @@ func NewArgs() *Args { return nil }).Bool() } - // add --set as a hidden alias for --variable kingpin.Flag("variable", "set variable NAME to VALUE").Hidden().StringsVar(&args.Variables) - // add --version flag kingpin.Flag("version", "display version and exit").PreAction(func(*kingpin.ParseContext) error { fmt.Fprintln(os.Stdout, text.CommandName, text.CommandVersion) os.Exit(0) return nil }).Short('V').Bool() - // hide help flag kingpin.HelpFlag.Short('h').Hidden() - // parse kingpin.Parse() - return args } diff --git a/drivers/drivers.go b/drivers/drivers.go index dd4e931cc45..47a5775b921 100644 --- a/drivers/drivers.go +++ b/drivers/drivers.go @@ -124,15 +124,14 @@ func ForceParams(u *dburl.URL) { // Open opens a sql.DB connection for the registered driver. func Open(u *dburl.URL) (*sql.DB, error) { - var err error d, ok := drivers[u.Driver] if !ok { return nil, WrapErr(u.Driver, text.ErrDriverNotAvailable) } f := sql.Open if d.Open != nil { - f, err = d.Open(u) - if err != nil { + var err error + if f, err = d.Open(u); err != nil { return nil, WrapErr(u.Driver, err) } } @@ -200,7 +199,7 @@ func User(u *dburl.URL, db DB) (string, error) { return user, WrapErr(u.Driver, err) } var user string - db.QueryRow(`select current_user`).Scan(&user) + _ = db.QueryRow(`select current_user`).Scan(&user) return user, nil } @@ -250,10 +249,9 @@ func CanChangePassword(u *dburl.URL) error { // from User. func ChangePassword(u *dburl.URL, db DB, user, new, old string) (string, error) { if d, ok := drivers[u.Driver]; ok && d.ChangePassword != nil { - var err error if user == "" { - user, err = User(u, db) - if err != nil { + var err error + if user, err = User(u, db); err != nil { return "", err } } diff --git a/env/env.go b/env/env.go index b48749b61fd..e1e3130c669 100644 --- a/env/env.go +++ b/env/env.go @@ -32,9 +32,10 @@ func Getenv(keys ...string) string { // expand expands the tilde (~) in the front of a path to a the supplied // directory. func expand(u *user.User, path string) string { - if path == "~" { + switch { + case path == "~": return u.HomeDir - } else if strings.HasPrefix(path, "~/") { + case strings.HasPrefix(path, "~/"): return filepath.Join(u.HomeDir, strings.TrimPrefix(path, "~/")) } return path @@ -53,8 +54,7 @@ func Getvar(s string) (bool, string, error) { q, n := "", s if c := rune(s[0]); c == '\'' || c == '"' { var err error - n, err = unquote(s, c) - if err != nil { + if n, err = unquote(s, c); err != nil { return false, "", err } q = string(c) @@ -68,8 +68,7 @@ func Getvar(s string) (bool, string, error) { // OpenFile opens a file for reading, returning the full, expanded path of the // file. All callers are responsible for closing the returned file. func OpenFile(u *user.User, path string, relative bool) (string, *os.File, error) { - var err error - path, err = filepath.EvalSymlinks(expand(u, path)) + path, err := filepath.EvalSymlinks(expand(u, path)) switch { case err != nil && os.IsNotExist(err): return "", nil, text.ErrNoSuchFileOrDirectory @@ -94,7 +93,6 @@ func OpenFile(u *user.User, path string, relative bool) (string, *os.File, error // EditFile edits a file. If path is empty, then a temporary file will be created. func EditFile(u *user.User, path, line, s string) ([]rune, error) { - var err error ed := Getenv(text.CommandUpper()+"_EDITOR", "EDITOR", "VISUAL") if ed == "" { return nil, text.ErrNoEditorDefined @@ -102,8 +100,7 @@ func EditFile(u *user.User, path, line, s string) ([]rune, error) { if path != "" { path = expand(u, path) } else { - var f *os.File - f, err = temp.File("", text.CommandLower(), "sql") + f, err := temp.File("", text.CommandLower(), "sql") if err != nil { return nil, err } @@ -132,8 +129,7 @@ func EditFile(u *user.User, path, line, s string) ([]rune, error) { c.Stdout = os.Stdout c.Stderr = os.Stderr // run - err = c.Run() - if err != nil { + if err := c.Run(); err != nil { return nil, err } // read @@ -355,8 +351,7 @@ func Unquote(u *user.User, s string, exec bool) (string, error) { return unquote(s, c) case exec && c == '`': var err error - s, err = unquote(s, c) - if err != nil { + if s, err = unquote(s, c); err != nil { return "", err } if strings.TrimSpace(s) == "" { diff --git a/handler/handler.go b/handler/handler.go index 31f053d6ea3..184f7a858f4 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -69,7 +69,7 @@ func New(l rline.IO, user *user.User, wd string, nopw bool) *Handler { return nil, nil } // save history - l.Save(string(r)) + _ = l.Save(string(r)) return r, nil } } @@ -142,12 +142,14 @@ func (h *Handler) outputHighlighter(s string) string { // EOF or when a \ cmd has been encountered var err error var cmd, final string +loop: for { cmd, _, err = st.Next() - if err != nil && err != io.EOF { + switch { + case err != nil && err != io.EOF: return s + endl - } else if err == io.EOF { - break + case err == io.EOF: + break loop } if st.Ready() || cmd != "" { final += st.RawString() @@ -197,7 +199,6 @@ func (h *Handler) Run() error { fmt.Fprintln(h.l.Stdout()) } for { - var err error var execute bool // set prompt if iactive { @@ -448,8 +449,7 @@ func (h *Handler) Open(params ...string) error { switch { case err == dburl.ErrInvalidDatabaseScheme: var fi os.FileInfo - fi, err = os.Stat(urlstr) - if err != nil { + if fi, err = os.Stat(urlstr); err != nil { return err } switch { @@ -512,11 +512,12 @@ func (h *Handler) forceParams(u *dburl.URL) { drivers.ForceParams(u) // see if password entry is present user, err := env.PassFileEntry(h.user, u) - if err != nil { + switch { + case err != nil: errout := h.l.Stderr() fmt.Fprintf(errout, "error: %v", err) fmt.Fprintln(errout) - } else if user != nil { + case user != nil: u.User = user } // copy back to u @@ -527,7 +528,6 @@ func (h *Handler) forceParams(u *dburl.URL) { // Password collects a password from input, and returns a modified DSN // including the collected password. func (h *Handler) Password(dsn string) (string, error) { - var err error if dsn == "" { return "", text.ErrMissingDSN } @@ -720,7 +720,7 @@ func (h *Handler) execSet(w io.Writer, prefix, qstr string, _ bool, namePrefix s if err = env.ValidIdentifier(n); err != nil { return fmt.Errorf(text.CouldNotSetVariable, n) } - env.Set(n, row[i]) + _ = env.Set(n, row[i]) } return nil } @@ -750,7 +750,6 @@ func (h *Handler) execExec(w io.Writer, prefix, qstr string, qtyp bool, _ string // query executes a query against the database. func (h *Handler) query(w io.Writer, _, qstr string) error { - var err error // run query q, err := h.DB().Query(qstr) if err != nil { @@ -763,7 +762,6 @@ func (h *Handler) query(w io.Writer, _, qstr string) error { // execRows executes all the columns in the row. func (h *Handler) execRows(w io.Writer, q *sql.Rows) error { - var err error // get columns cols, err := drivers.Columns(h.u, q) if err != nil { @@ -801,8 +799,7 @@ func (h *Handler) scan(q *sql.Rows, clen int, tfmt string) ([]string, error) { return nil, err } // get conversion funcs - cb, cm, cs, cd := drivers.ConvertBytes(h.u), drivers.ConvertMap(h.u), - drivers.ConvertSlice(h.u), drivers.ConvertDefault(h.u) + cb, cm, cs, cd := drivers.ConvertBytes(h.u), drivers.ConvertMap(h.u), drivers.ConvertSlice(h.u), drivers.ConvertDefault(h.u) row := make([]string, clen) for n, z := range r { j := z.(*interface{}) @@ -848,7 +845,6 @@ func (h *Handler) scan(q *sql.Rows, clen int, tfmt string) ([]string, error) { // exec does a database exec. func (h *Handler) exec(w io.Writer, typ, qstr string) error { - var err error res, err := h.DB().Exec(qstr) if err != nil { return err @@ -920,7 +916,6 @@ func (h *Handler) Rollback() error { // Include includes the specified path. func (h *Handler) Include(path string, relative bool) error { - var err error if relative && !filepath.IsAbs(path) { path = filepath.Join(h.wd, path) } diff --git a/internal/adodb.go b/internal/adodb.go index 9c8f27e314f..2522447af98 100644 --- a/internal/adodb.go +++ b/internal/adodb.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // adodb driver - _ "github.com/xo/usql/drivers/adodb" + _ "github.com/xo/usql/drivers/adodb" // adodb driver ) diff --git a/internal/athena.go b/internal/athena.go index 06e04fb7f50..162feb87fe9 100644 --- a/internal/athena.go +++ b/internal/athena.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // athena driver - _ "github.com/xo/usql/drivers/athena" + _ "github.com/xo/usql/drivers/athena" // athena driver ) diff --git a/internal/avatica.go b/internal/avatica.go index a9aaf2b5a19..7fa1eba69a0 100644 --- a/internal/avatica.go +++ b/internal/avatica.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // avatica driver - _ "github.com/xo/usql/drivers/avatica" + _ "github.com/xo/usql/drivers/avatica" // avatica driver ) diff --git a/internal/bigquery.go b/internal/bigquery.go index e9c0afc7950..39079cbffdc 100644 --- a/internal/bigquery.go +++ b/internal/bigquery.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // bigquery driver - _ "github.com/xo/usql/drivers/bigquery" + _ "github.com/xo/usql/drivers/bigquery" // bigquery driver ) diff --git a/internal/cassandra.go b/internal/cassandra.go index d3898829aa0..0cae9f01c20 100644 --- a/internal/cassandra.go +++ b/internal/cassandra.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // cassandra driver - _ "github.com/xo/usql/drivers/cassandra" + _ "github.com/xo/usql/drivers/cassandra" // cassandra driver ) diff --git a/internal/clickhouse.go b/internal/clickhouse.go index 55c417dac81..66a9f537c35 100644 --- a/internal/clickhouse.go +++ b/internal/clickhouse.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // clickhouse driver - _ "github.com/xo/usql/drivers/clickhouse" + _ "github.com/xo/usql/drivers/clickhouse" // clickhouse driver ) diff --git a/internal/cosmos.go b/internal/cosmos.go index d93dfef2706..6a7fd04098c 100644 --- a/internal/cosmos.go +++ b/internal/cosmos.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // cosmos driver - _ "github.com/xo/usql/drivers/cosmos" + _ "github.com/xo/usql/drivers/cosmos" // cosmos driver ) diff --git a/internal/couchbase.go b/internal/couchbase.go index 7db13b6f4cc..c7574d88b1c 100644 --- a/internal/couchbase.go +++ b/internal/couchbase.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // couchbase driver - _ "github.com/xo/usql/drivers/couchbase" + _ "github.com/xo/usql/drivers/couchbase" // couchbase driver ) diff --git a/internal/firebird.go b/internal/firebird.go index 741c598fcee..5da51001a73 100644 --- a/internal/firebird.go +++ b/internal/firebird.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // firebird driver - _ "github.com/xo/usql/drivers/firebird" + _ "github.com/xo/usql/drivers/firebird" // firebird driver ) diff --git a/internal/gen.sh b/internal/gen.sh index 43ebe9eebee..a4f55f5b086 100755 --- a/internal/gen.sh +++ b/internal/gen.sh @@ -27,8 +27,7 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // $NAME driver - _ "github.com/xo/usql/drivers/$NAME" + _ "github.com/xo/usql/drivers/$NAME" // $NAME driver ) ENDSTR ) diff --git a/internal/genji.go b/internal/genji.go index ce0a06034f6..bdd33974b85 100644 --- a/internal/genji.go +++ b/internal/genji.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // genji driver - _ "github.com/xo/usql/drivers/genji" + _ "github.com/xo/usql/drivers/genji" // genji driver ) diff --git a/internal/godror.go b/internal/godror.go index 9873ab598ee..e50526e5d47 100644 --- a/internal/godror.go +++ b/internal/godror.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // godror driver - _ "github.com/xo/usql/drivers/godror" + _ "github.com/xo/usql/drivers/godror" // godror driver ) diff --git a/internal/h2.go b/internal/h2.go index f4a9bce59cb..205c42cf427 100644 --- a/internal/h2.go +++ b/internal/h2.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // h2 driver - _ "github.com/xo/usql/drivers/h2" + _ "github.com/xo/usql/drivers/h2" // h2 driver ) diff --git a/internal/hive.go b/internal/hive.go index 826205084fd..4bd3aebbbf0 100644 --- a/internal/hive.go +++ b/internal/hive.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // hive driver - _ "github.com/xo/usql/drivers/hive" + _ "github.com/xo/usql/drivers/hive" // hive driver ) diff --git a/internal/ignite.go b/internal/ignite.go index 276271c484c..6bb53f864e8 100644 --- a/internal/ignite.go +++ b/internal/ignite.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // ignite driver - _ "github.com/xo/usql/drivers/ignite" + _ "github.com/xo/usql/drivers/ignite" // ignite driver ) diff --git a/internal/impala.go b/internal/impala.go index b88004ff9e7..b4c9ca121fb 100644 --- a/internal/impala.go +++ b/internal/impala.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // impala driver - _ "github.com/xo/usql/drivers/impala" + _ "github.com/xo/usql/drivers/impala" // impala driver ) diff --git a/internal/maxcompute.go b/internal/maxcompute.go index 5ac004c5c20..9e881ca320d 100644 --- a/internal/maxcompute.go +++ b/internal/maxcompute.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // maxcompute driver - _ "github.com/xo/usql/drivers/maxcompute" + _ "github.com/xo/usql/drivers/maxcompute" // maxcompute driver ) diff --git a/internal/moderncsqlite.go b/internal/moderncsqlite.go index 0c7803d8a2e..9b3ae3e0b3c 100644 --- a/internal/moderncsqlite.go +++ b/internal/moderncsqlite.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // moderncsqlite driver - _ "github.com/xo/usql/drivers/moderncsqlite" + _ "github.com/xo/usql/drivers/moderncsqlite" // moderncsqlite driver ) diff --git a/internal/mssql.go b/internal/mssql.go index 4dc292698d2..63c16fabaab 100644 --- a/internal/mssql.go +++ b/internal/mssql.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // mssql driver - _ "github.com/xo/usql/drivers/mssql" + _ "github.com/xo/usql/drivers/mssql" // mssql driver ) diff --git a/internal/mymysql.go b/internal/mymysql.go index 9dde1a7d284..a1d82042cf0 100644 --- a/internal/mymysql.go +++ b/internal/mymysql.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // mymysql driver - _ "github.com/xo/usql/drivers/mymysql" + _ "github.com/xo/usql/drivers/mymysql" // mymysql driver ) diff --git a/internal/mysql.go b/internal/mysql.go index db2071e9ee9..5eca50929a9 100644 --- a/internal/mysql.go +++ b/internal/mysql.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // mysql driver - _ "github.com/xo/usql/drivers/mysql" + _ "github.com/xo/usql/drivers/mysql" // mysql driver ) diff --git a/internal/odbc.go b/internal/odbc.go index 06571f5f979..4d6bbb4376f 100644 --- a/internal/odbc.go +++ b/internal/odbc.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // odbc driver - _ "github.com/xo/usql/drivers/odbc" + _ "github.com/xo/usql/drivers/odbc" // odbc driver ) diff --git a/internal/oracle.go b/internal/oracle.go index 19e131a8bab..1ac38b4d88f 100644 --- a/internal/oracle.go +++ b/internal/oracle.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // oracle driver - _ "github.com/xo/usql/drivers/oracle" + _ "github.com/xo/usql/drivers/oracle" // oracle driver ) diff --git a/internal/pgx.go b/internal/pgx.go index 01a41da6a9b..a498713cde9 100644 --- a/internal/pgx.go +++ b/internal/pgx.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // pgx driver - _ "github.com/xo/usql/drivers/pgx" + _ "github.com/xo/usql/drivers/pgx" // pgx driver ) diff --git a/internal/postgres.go b/internal/postgres.go index abb4e00ef30..07f2b3171ba 100644 --- a/internal/postgres.go +++ b/internal/postgres.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // postgres driver - _ "github.com/xo/usql/drivers/postgres" + _ "github.com/xo/usql/drivers/postgres" // postgres driver ) diff --git a/internal/presto.go b/internal/presto.go index e2eacaf78ed..17cbefb7989 100644 --- a/internal/presto.go +++ b/internal/presto.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // presto driver - _ "github.com/xo/usql/drivers/presto" + _ "github.com/xo/usql/drivers/presto" // presto driver ) diff --git a/internal/ql.go b/internal/ql.go index f6a71ba6030..a6734e17469 100644 --- a/internal/ql.go +++ b/internal/ql.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // ql driver - _ "github.com/xo/usql/drivers/ql" + _ "github.com/xo/usql/drivers/ql" // ql driver ) diff --git a/internal/sapase.go b/internal/sapase.go index 4143be0c498..7ec02adea96 100644 --- a/internal/sapase.go +++ b/internal/sapase.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // sapase driver - _ "github.com/xo/usql/drivers/sapase" + _ "github.com/xo/usql/drivers/sapase" // sapase driver ) diff --git a/internal/saphana.go b/internal/saphana.go index 69fdef79da7..3ee5271fe3c 100644 --- a/internal/saphana.go +++ b/internal/saphana.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // saphana driver - _ "github.com/xo/usql/drivers/saphana" + _ "github.com/xo/usql/drivers/saphana" // saphana driver ) diff --git a/internal/snowflake.go b/internal/snowflake.go index 4b9f04fcb0c..84de93aeafe 100644 --- a/internal/snowflake.go +++ b/internal/snowflake.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // snowflake driver - _ "github.com/xo/usql/drivers/snowflake" + _ "github.com/xo/usql/drivers/snowflake" // snowflake driver ) diff --git a/internal/spanner.go b/internal/spanner.go index d7bdb6d293e..1ac503a8d39 100644 --- a/internal/spanner.go +++ b/internal/spanner.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // spanner driver - _ "github.com/xo/usql/drivers/spanner" + _ "github.com/xo/usql/drivers/spanner" // spanner driver ) diff --git a/internal/sqlite3.go b/internal/sqlite3.go index 7bebc93d2bc..cbd76a19d0d 100644 --- a/internal/sqlite3.go +++ b/internal/sqlite3.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // sqlite3 driver - _ "github.com/xo/usql/drivers/sqlite3" + _ "github.com/xo/usql/drivers/sqlite3" // sqlite3 driver ) diff --git a/internal/trino.go b/internal/trino.go index f1e49fb90b0..37d343a6e43 100644 --- a/internal/trino.go +++ b/internal/trino.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // trino driver - _ "github.com/xo/usql/drivers/trino" + _ "github.com/xo/usql/drivers/trino" // trino driver ) diff --git a/internal/vertica.go b/internal/vertica.go index 5c6afcb3174..efa3f37a747 100644 --- a/internal/vertica.go +++ b/internal/vertica.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // vertica driver - _ "github.com/xo/usql/drivers/vertica" + _ "github.com/xo/usql/drivers/vertica" // vertica driver ) diff --git a/internal/voltdb.go b/internal/voltdb.go index eda7e3a13f7..ada533bed2a 100644 --- a/internal/voltdb.go +++ b/internal/voltdb.go @@ -5,6 +5,5 @@ package internal // Code generated by gen.sh. DO NOT EDIT. import ( - // voltdb driver - _ "github.com/xo/usql/drivers/voltdb" + _ "github.com/xo/usql/drivers/voltdb" // voltdb driver ) diff --git a/internal/z.go b/internal/z.go index 65ab745e6cb..b68e5d3067c 100644 --- a/internal/z.go +++ b/internal/z.go @@ -4,7 +4,6 @@ import ( "runtime" "github.com/xo/dburl" - "github.com/xo/usql/drivers" ) diff --git a/main.go b/main.go index 6293edbccb2..22c2224e857 100644 --- a/main.go +++ b/main.go @@ -20,17 +20,14 @@ import ( func main() { // get available drivers and known build tags available, known := drivers.Available(), internal.KnownBuildTags() - // report if database is supported if len(os.Args) == 2 && strings.HasPrefix(os.Args[1], "--has-") && strings.HasSuffix(os.Args[1], "-support") { - n := os.Args[1][6 : len(os.Args[1])-8] if v, ok := known[n]; ok { n = v } - var out int if _, ok := available[n]; ok { out = 1 @@ -38,36 +35,28 @@ func main() { fmt.Fprintf(os.Stdout, "%d", out) return } - - var err error - // load current user cur, err := user.Current() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } - args := NewArgs() - // run err = run(args, cur) if err != nil && err != io.EOF && err != rline.ErrInterrupt { fmt.Fprintf(os.Stderr, "error: %v\n", err) - if e, ok := err.(*drivers.Error); ok && e.Err == text.ErrDriverNotAvailable { m := make(map[string]string, len(known)) for k, v := range known { m[v] = k } - tag := e.Driver if t, ok := m[tag]; ok { tag = t } fmt.Fprintf(os.Stderr, "\ntry:\n\n go get -u -tags %s github.com/xo/usql\n\n", tag) } - os.Exit(1) } } @@ -75,20 +64,17 @@ func main() { // run processes args, processing args.CommandOrFiles if non-empty, if // specified, otherwise launch an interactive readline from stdin. func run(args *Args, u *user.User) error { - var err error - // get working directory wd, err := os.Getwd() if err != nil { return err } - // handle variables for _, v := range args.Variables { if i := strings.Index(v, "="); i != -1 { - env.Set(v[:i], v[i+1:]) + _ = env.Set(v[:i], v[i+1:]) } else { - env.Unset(v) + _ = env.Unset(v) } } for _, v := range args.PVariables { @@ -102,17 +88,14 @@ func run(args *Args, u *user.User) error { } } } - // create input/output l, err := rline.New(len(args.CommandOrFiles) != 0, args.Out, env.HistoryFile(u)) if err != nil { return err } defer l.Close() - // create handler h := handler.New(l, u, wd, args.NoPassword) - // force a password ... dsn := args.DSN if args.ForcePassword { @@ -121,12 +104,10 @@ func run(args *Args, u *user.User) error { return err } } - // open dsn if err = h.Open(dsn); err != nil { return err } - // start transaction if args.SingleTransaction { if h.IO().Interactive() { @@ -136,30 +117,25 @@ func run(args *Args, u *user.User) error { return err } } - // rc file if rc := env.RCFile(u); !args.NoRC && rc != "" { if err = h.Include(rc, false); err != nil && err != text.ErrNoSuchFileOrDirectory { return err } } - // setup runner f := h.Run if len(args.CommandOrFiles) != 0 { f = runCommandOrFiles(h, args.CommandOrFiles) } - // run if err = f(); err != nil { return err } - // commit if args.SingleTransaction { return h.Commit() } - return nil } diff --git a/metacmd/cmds.go b/metacmd/cmds.go index 3250fd69b03..3c67f47fddf 100644 --- a/metacmd/cmds.go +++ b/metacmd/cmds.go @@ -220,9 +220,10 @@ func init() { case buf.Len != 0: s = buf.String() } - if s == "" { + switch { + case s == "": s = text.QueryBufferEmpty - } else if p.Handler.IO().Interactive() && env.All()["SYNTAX_HL"] == "true" { + case p.Handler.IO().Interactive() && env.All()["SYNTAX_HL"] == "true": b := new(bytes.Buffer) if p.Handler.Highlight(b, s) == nil { s = b.String() @@ -281,14 +282,11 @@ func init() { Min: 1, Desc: "set or unset environment variable,NAME [VALUE]", Process: func(p *Params) error { - var err error n := p.Get() if len(p.Params) == 1 { - err = os.Unsetenv(n) - } else { - err = os.Setenv(n, strings.Join(p.GetAll(), "")) + return os.Unsetenv(n) } - return err + return os.Setenv(n, strings.Join(p.GetAll(), "")) }, }, ShellExec: { diff --git a/rline/rline.go b/rline/rline.go index 83687f48753..0de390200d8 100644 --- a/rline/rline.go +++ b/rline/rline.go @@ -120,32 +120,34 @@ func (l *Rline) SetOutput(f func(string) string) { // New creates a new readline input/output handler. func New(forceNonInteractive bool, out, histfile string) (IO, error) { - var err error // determine if interactive interactive := isatty.IsTerminal(os.Stdout.Fd()) && isatty.IsTerminal(os.Stdin.Fd()) cygwin := isatty.IsCygwinTerminal(os.Stdout.Fd()) && isatty.IsCygwinTerminal(os.Stdin.Fd()) var closers []func() error // configure stdin var stdin io.ReadCloser - if forceNonInteractive { + switch { + case forceNonInteractive: interactive, cygwin = false, false - } else if cygwin { + case cygwin: stdin = os.Stdin - } else { + default: stdin = readline.Stdin } // configure stdout var stdout io.WriteCloser - if out != "" { + switch { + case out != "": + var err error stdout, err = os.OpenFile(out, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644) if err != nil { return nil, err } closers = append(closers, stdout.Close) interactive = false - } else if cygwin { + case cygwin: stdout = os.Stdout - } else { + default: stdout = readline.Stdout } // configure stderr @@ -189,7 +191,7 @@ func New(forceNonInteractive bool, out, histfile string) (IO, error) { N: n, C: func() error { for _, f := range closers { - f() + _ = f() } return nil }, diff --git a/stmt/parse_test.go b/stmt/parse_test.go index 4ebf25bbc8e..65868f9097d 100644 --- a/stmt/parse_test.go +++ b/stmt/parse_test.go @@ -175,11 +175,10 @@ func TestTrimSplit(t *testing.T) { sp = "|" } exp := strings.Split(test.exp, sp) - if test.exp == "" { - if len(y) != 0 { - t.Errorf("test %d expected result to have length 0, has length: %d", i, len(y)) - } - } else if !reflect.DeepEqual(y, exp) { + switch { + case test.exp == "" && len(y) != 0: + t.Errorf("test %d expected result to have length 0, has length: %d", i, len(y)) + case test.exp != "" && !reflect.DeepEqual(y, exp): t.Errorf("test %d expected %v, got: %v", i, exp, y) } } diff --git a/stmt/stmt_test.go b/stmt/stmt_test.go index 77ab93dd276..0969ccd9334 100644 --- a/stmt/stmt_test.go +++ b/stmt/stmt_test.go @@ -174,11 +174,13 @@ func TestNextResetState(t *testing.T) { var stmts, cmds []string var aparams [][]string var vars []*Var + loop: for { cmd, params, err := b.Next() - if err == io.EOF { - break - } else if err != nil { + switch { + case err == io.EOF: + break loop + case err != nil: t.Fatalf("test %d did not expect error, got: %v", i, err) } vars = append(vars, b.Vars...)