Skip to content

Commit 0d2eb10

Browse files
committed
Fixes QUIET issues
Cleans up various output statements around the code base, better matching psql's output. Corrects --quiet behavior. Fixes #437.
1 parent 4d66d0d commit 0d2eb10

File tree

5 files changed

+46
-41
lines changed

5 files changed

+46
-41
lines changed

env/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ func init() {
258258
"SHOW_HOST_INFORMATION": enableHostInformation,
259259
"PAGER": pagerCmd,
260260
"EDITOR": editorCmd,
261+
"QUIET": "off",
261262
"ON_ERROR_STOP": "off",
262263
// prompts
263264
"PROMPT1": "%S%N%m%/%R%# ",

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ require (
5858
github.com/uber/athenadriver v1.1.15
5959
github.com/vertica/vertica-sql-go v1.3.3
6060
github.com/xo/dburl v0.22.0
61-
github.com/xo/tblfmt v0.12.1
61+
github.com/xo/tblfmt v0.13.0
6262
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e
6363
github.com/ydb-platform/ydb-go-sdk/v3 v3.61.2
6464
github.com/yookoala/realpath v1.0.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1039,8 +1039,8 @@ github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQ
10391039
github.com/xo/dburl v0.22.0 h1:sO5WLm2ywMzyiLxEcLBlw5AyKvdR5hirq9U7s3fCoeM=
10401040
github.com/xo/dburl v0.22.0/go.mod h1:B7/G9FGungw6ighV8xJNwWYQPMfn3gsi2sn5SE8Bzco=
10411041
github.com/xo/tblfmt v0.0.0-20190609041254-28c54ec42ce8/go.mod h1:3U5kKQdIhwACye7ml3acccHmjGExY9WmUGU7rnDWgv0=
1042-
github.com/xo/tblfmt v0.12.1 h1:kohkoLtIuuM7FiRkoYDsURPBz4viVr9wj13XUJvVSxs=
1043-
github.com/xo/tblfmt v0.12.1/go.mod h1:AKivZTttxEXXrztqGl0dGVh4hsEOSGIfSZNbRhnKZuo=
1042+
github.com/xo/tblfmt v0.13.0 h1:mh0ilx9rKZNoMqGwvyMpQv3MED+X6te6ZqB+BzSypck=
1043+
github.com/xo/tblfmt v0.13.0/go.mod h1:BLPC+dRy68cgSK/mPgQRfFQ/xLg231Fyic178ybjB34=
10441044
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
10451045
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
10461046
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=

handler/handler.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ var helpQuitExitRE = regexp.MustCompile(fmt.Sprintf(`(?im)^(%s|%s|%s)\s*$`, text
228228
func (h *Handler) Run() error {
229229
stdout, stderr, iactive := h.l.Stdout(), h.l.Stderr(), h.l.Interactive()
230230
// display welcome info
231-
if iactive {
232-
// graphics logo
231+
if iactive && env.Get("QUIET") == "off" {
232+
// logo
233233
if typ := env.TermGraphics(); typ.Available() {
234234
if err := typ.Encode(stdout, text.Logo); err != nil {
235235
return err
@@ -756,7 +756,7 @@ func (h *Handler) Open(ctx context.Context, params ...string) error {
756756
}
757757
// open connection
758758
var err error
759-
h.db, err = drivers.Open(ctx, h.u, h.GetOutput, h.IO().Stderr)
759+
h.db, err = drivers.Open(ctx, h.u, h.GetOutput, h.l.Stderr)
760760
if err != nil && !drivers.IsPasswordErr(h.u, err) {
761761
defer h.Close()
762762
return err
@@ -971,19 +971,20 @@ func (h *Handler) ChangePassword(user string) (string, error) {
971971

972972
// Version prints the database version information after a successful connection.
973973
func (h *Handler) Version(ctx context.Context) error {
974-
if env.Get("SHOW_HOST_INFORMATION") != "true" || !h.IO().Interactive() {
974+
if env.Get("SHOW_HOST_INFORMATION") != "true" || !h.l.Interactive() {
975975
return nil
976976
}
977977
if h.db == nil {
978978
return text.ErrNotConnected
979979
}
980980
ver, err := drivers.Version(ctx, h.u, h.DB())
981-
if err != nil {
981+
switch {
982+
case err != nil:
982983
ver = fmt.Sprintf("<unknown, error: %v>", err)
984+
case ver == "":
985+
ver = "<unknown>"
983986
}
984-
if ver != "" {
985-
h.Print(text.ConnInfo, h.u.Driver, ver)
986-
}
987+
h.Print(text.ConnInfo, h.u.Driver, ver)
987988
return nil
988989
}
989990

@@ -1030,13 +1031,13 @@ func (h *Handler) doExecSingle(ctx context.Context, w io.Writer, opt metacmd.Opt
10301031
}
10311032
if h.timing {
10321033
d := time.Since(start)
1033-
format := text.TimingDesc
1034+
s := text.TimingDesc
10341035
v := []interface{}{float64(d.Microseconds()) / 1000}
10351036
if d > 1*time.Second {
1036-
format += " (%v)"
1037+
s += " (%v)"
10371038
v = append(v, d.Round(1*time.Millisecond))
10381039
}
1039-
h.Print(format, v...)
1040+
fmt.Fprintln(h.l.Stdout(), fmt.Sprintf(s, v...))
10401041
}
10411042
return nil
10421043
}
@@ -1123,7 +1124,7 @@ func (h *Handler) doQuery(ctx context.Context, w io.Writer, opt metacmd.Option,
11231124
}
11241125
if pipeName != "" {
11251126
if pipeName[0] == '|' {
1126-
pipe, cmd, err = env.Pipe(h.IO().Stdout(), h.IO().Stderr(), pipeName[1:])
1127+
pipe, cmd, err = env.Pipe(h.l.Stdout(), h.l.Stderr(), pipeName[1:])
11271128
} else {
11281129
pipe, err = os.OpenFile(pipeName, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644)
11291130
}
@@ -1275,12 +1276,14 @@ func (h *Handler) doExec(ctx context.Context, w io.Writer, _ metacmd.Option, typ
12751276
return err
12761277
}
12771278
// print name
1278-
fmt.Fprint(w, typ)
1279-
// print count
1280-
if count > 0 {
1281-
fmt.Fprint(w, " ", count)
1279+
if env.Get("QUIET") == "off" {
1280+
fmt.Fprint(w, typ)
1281+
// print count
1282+
if count > 0 {
1283+
fmt.Fprint(w, " ", count)
1284+
}
1285+
fmt.Fprintln(w)
12821286
}
1283-
fmt.Fprintln(w)
12841287
return env.Set("ROW_COUNT", strconv.FormatInt(count, 10))
12851288
}
12861289

metacmd/cmds.go

+22-21
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ func init() {
9494
Name: "copyright",
9595
Desc: Desc{"show " + text.CommandName + " usage and distribution terms", ""},
9696
Process: func(p *Params) error {
97+
stdout := p.Handler.IO().Stdout()
9798
if typ := env.TermGraphics(); typ.Available() {
98-
typ.Encode(p.Handler.IO().Stdout(), text.Logo)
99+
typ.Encode(stdout, text.Logo)
99100
}
100-
p.Handler.Print(text.Copyright)
101+
fmt.Fprintln(stdout, text.Copyright)
101102
return nil
102103
},
103104
},
@@ -106,11 +107,11 @@ func init() {
106107
Name: "conninfo",
107108
Desc: Desc{"display information about the current database connection", ""},
108109
Process: func(p *Params) error {
110+
s := text.NotConnected
109111
if db, u := p.Handler.DB(), p.Handler.URL(); db != nil && u != nil {
110-
p.Handler.Print(text.ConnInfo, u.Driver, u.DSN)
111-
} else {
112-
p.Handler.Print(text.NotConnected)
112+
s = fmt.Sprintf(text.ConnInfo, u.Driver, u.DSN)
113113
}
114+
fmt.Fprintln(p.Handler.IO().Stdout(), s)
114115
return nil
115116
},
116117
},
@@ -119,7 +120,7 @@ func init() {
119120
Name: "drivers",
120121
Desc: Desc{"display information about available database drivers", ""},
121122
Process: func(p *Params) error {
122-
out := p.Handler.IO().Stdout()
123+
stdout := p.Handler.IO().Stdout()
123124
available := drivers.Available()
124125
names := make([]string, len(available))
125126
var z int
@@ -128,7 +129,7 @@ func init() {
128129
z++
129130
}
130131
sort.Strings(names)
131-
fmt.Fprintln(out, text.AvailableDrivers)
132+
fmt.Fprintln(stdout, text.AvailableDrivers)
132133
for _, n := range names {
133134
s := " " + n
134135
driver, aliases := dburl.SchemeDriverAndAliases(n)
@@ -140,7 +141,7 @@ func init() {
140141
s += " [" + strings.Join(aliases, ", ") + "]"
141142
}
142143
}
143-
fmt.Fprintln(out, s)
144+
fmt.Fprintln(stdout, s)
144145
}
145146
return nil
146147
},
@@ -366,7 +367,7 @@ func init() {
366367
Aliases: map[string]Desc{"reset": {}},
367368
Process: func(p *Params) error {
368369
p.Handler.Reset(nil)
369-
fmt.Fprintln(p.Handler.IO().Stdout(), text.QueryBufferReset)
370+
p.Handler.Print(text.QueryBufferReset)
370371
return nil
371372
},
372373
},
@@ -379,30 +380,32 @@ func init() {
379380
"warn": {"write string to standard error (-n for no newline)", "[-n] [STRING]"},
380381
},
381382
Process: func(p *Params) error {
382-
nl := "\n"
383-
var vals []string
384383
ok, n, err := p.GetOptional(true)
385384
if err != nil {
386385
return err
387386
}
388-
if ok && n == "n" {
389-
nl = ""
390-
} else if ok {
387+
f := fmt.Fprintln
388+
var vals []string
389+
switch {
390+
case ok && n == "n":
391+
f = fmt.Fprint
392+
case ok:
391393
vals = append(vals, "-"+n)
392-
} else {
394+
default:
393395
vals = append(vals, n)
394396
}
395397
v, err := p.GetAll(true)
396398
if err != nil {
397399
return err
398400
}
399401
out := p.Handler.IO().Stdout()
400-
if o := p.Handler.GetOutput(); p.Name == "qecho" && o != nil {
402+
switch o := p.Handler.GetOutput(); {
403+
case p.Name == "qecho" && o != nil:
401404
out = o
402-
} else if p.Name == "warn" {
405+
case p.Name == "warn":
403406
out = p.Handler.IO().Stderr()
404407
}
405-
fmt.Fprint(out, strings.Join(append(vals, v...), " ")+nl)
408+
f(out, strings.Join(append(vals, v...), " "))
406409
return nil
407410
},
408411
},
@@ -521,9 +524,7 @@ func init() {
521524
Desc: Desc{"send all query results to file or |pipe", "[FILE]"},
522525
Aliases: map[string]Desc{"out": {}},
523526
Process: func(p *Params) error {
524-
if p.Handler.GetOutput() != nil {
525-
p.Handler.SetOutput(nil)
526-
}
527+
p.Handler.SetOutput(nil)
527528
params, err := p.GetAll(true)
528529
if err != nil {
529530
return err

0 commit comments

Comments
 (0)