Skip to content

Commit

Permalink
Adding \warn command and -n option for \echo, \qecho, and \warn
Browse files Browse the repository at this point in the history
Refactors \qecho into the same command as \echo, adds a \warn alias that
outputs to stderr, and adds the -n option for no newline output to match
psql's behavior.
  • Loading branch information
kenshaw committed Apr 3, 2021
1 parent cd4a318 commit 7a8e7e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
46 changes: 26 additions & 20 deletions metacmd/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ func init() {
Exec: {
Section: SectionGeneral,
Name: "g",
Desc: "execute query (and send results to file or |pipe),[FILE] or ;",
Desc: "execute query (and send results to file or |pipe),[(OPTIONS)] [FILE] or ;",
Aliases: map[string]string{
"gexec": "execute query and execute each value of the result",
"gset": "execute query and store results in " + text.CommandName + " variables,[PREFIX]",
"gx": `as \g, but forces expanded output mode,`,
"crosstabview": "execute query and display results in crosstab,[COLUMNS]",
"watch": "execute query every specified interval,[DURATION]",
"gx": `as \g, but forces expanded output mode,[(OPTIONS)] [FILE]`,
"crosstabview": "execute query and display results in crosstab,[(OPTIONS)] [COLUMNS]",
"watch": "execute query every specified interval,[(OPTIONS)] [DURATION]",
},
Process: func(p *Params) error {
p.Option.Exec = ExecOnly
Expand Down Expand Up @@ -314,30 +314,36 @@ func init() {
Echo: {
Section: SectionInputOutput,
Name: "echo",
Desc: "write string to standard output,[STRING]",
Desc: "write string to standard output (-n for no newline),[-n] [STRING]",
Aliases: map[string]string{
"qecho": "write string to \\o output stream (-n for no newline),[-n] [STRING]",
"warn": "write string to standard error (-n for no newline),[-n] [STRING]",
},
Process: func(p *Params) error {
vals, err := p.GetAll(true)
nl := "\n"
var vals []string
ok, n, err := p.GetOptional(true)
if err != nil {
return err
}
fmt.Fprintln(p.Handler.IO().Stdout(), strings.Join(vals, " "))
return nil
},
},
Qecho: {
Section: SectionInputOutput,
Name: "qecho",
Desc: "write string to \\o output stream,[STRING]",
Process: func(p *Params) error {
vals, err := p.GetAll(true)
if ok && n == "n" {
nl = ""
} else if ok {
vals = append(vals, "-"+n)
} else {
vals = append(vals, n)
}
v, err := p.GetAll(true)
if err != nil {
return err
}
var out io.Writer = p.Handler.GetOutput()
if out == nil {
out = p.Handler.IO().Stdout()
out := io.Writer(p.Handler.IO().Stdout())
if o := p.Handler.GetOutput(); p.Name == "qecho" && o != nil {
out = o
} else if p.Name == "warn" {
out = p.Handler.IO().Stderr()
}
fmt.Fprintln(out, strings.Join(vals, " "))
fmt.Fprint(out, strings.Join(append(vals, v...), " ")+nl)
return nil
},
},
Expand Down
4 changes: 1 addition & 3 deletions metacmd/metacmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ const (
Print
// Reset is the reset query buffer meta command (\r, \reset).
Reset
// Echo is the echo meta command (\echo).
// Echo is the echo meta command (\echo, \warn, \qecho).
Echo
// Qecho is the qecho meta command (\qecho).
Qecho
// Write is the write meta command (\w).
Write
// ChangeDir is the system change directory meta command (\cd).
Expand Down
2 changes: 2 additions & 0 deletions text/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ var (
ErrInvalidFormatOption = errors.New("invalid format option")
// ErrInvalidWatchDuration is the invalid watch duration error.
ErrInvalidWatchDuration = errors.New("invalid watch duration")
// ErrUnableToNormalizeURL is the unable to normalize URL error.
ErrUnableToNormalizeURL = errors.New("unable to normalize URL")
)

0 comments on commit 7a8e7e7

Please sign in to comment.