pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feed[PATCH v1] Add \echo_stderr to psql
11+ messages / 5 participants
[nested] [flat]
* [PATCH v1] Add \echo_stderr to psql
@ 2019-04-21 18:31 David Fetter <david@fetter.org>
0 siblings, 2 replies; 11+ messages in thread
From: David Fetter @ 2019-04-21 18:31 UTC (permalink / raw)
To: pgsql-hackers
Folks,
Any interest in this?
Best,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
Attachments:
[text/x-diff] v1-0001-Add-echo_stderr-to-psql.patch (4.5K, ../../20190421183115.GA4311@fetter.org/2-v1-0001-Add-echo_stderr-to-psql.patch)
download | inline diff:
From 089d48c00ffa6dfd0a6a443e83f6d618c9847deb Mon Sep 17 00:00:00 2001
From: David Fetter <david@fetter.org>
Date: Sun, 21 Apr 2019 11:08:40 -0700
Subject: [PATCH v1] Add \echo_stderr to psql
To: hackers
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------2.20.1"
This is a multi-part message in MIME format.
--------------2.20.1
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit
- Does what it says on the label
- Do we have any way to test this?
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index b86764003d..f8481f2e1b 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1879,6 +1879,26 @@ Tue Oct 26 21:40:57 CEST 1999
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><literal>\echo_stderr <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
+ <listitem>
+ <para>
+ Prints the arguments to the standard error, separated by one
+ space and followed by a newline. This can be useful to
+ intersperse information in the output of scripts when not polluting
+ standard output is desired. For example:
+
+<programlisting>
+=> <userinput>\echo :variable</userinput>
+=> <userinput>\echo_stderr `date`</userinput>
+Sun Apr 21 10:48:11 PDT 2019
+</programlisting>
+ If the first argument is an unquoted <literal>-n</literal> the trailing
+ newline is not written.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><literal>\ef <optional> <replaceable class="parameter">function_description</replaceable> <optional> <replaceable class="parameter">line_number</replaceable> </optional> </optional> </literal></term>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 8254d61099..6134c43938 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -319,7 +319,7 @@ exec_command(const char *cmd,
status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
else if (strcmp(cmd, "ev") == 0)
status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
- else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0)
+ else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "echo_stderr") == 0 || strcmp(cmd, "qecho") == 0)
status = exec_command_echo(scan_state, active_branch, cmd);
else if (strcmp(cmd, "elif") == 0)
status = exec_command_elif(scan_state, cstack, query_buf);
@@ -1114,7 +1114,7 @@ exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
}
/*
- * \echo and \qecho -- echo arguments to stdout or query output
+ * \echo, \echo_stderr and \qecho -- echo arguments to stdout or query output
*/
static backslashResult
exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
@@ -1129,6 +1129,8 @@ exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
if (strcmp(cmd, "qecho") == 0)
fout = pset.queryFout;
+ else if (strcmp(cmd, "echo_stderr") == 0)
+ fout = stderr;
else
fout = stdout;
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index d6d41b51d5..b6ef98d00c 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -207,6 +207,7 @@ slashUsage(unsigned short int pager)
fprintf(output, _("Input/Output\n"));
fprintf(output, _(" \\copy ... perform SQL COPY with data stream to the client host\n"));
fprintf(output, _(" \\echo [STRING] write string to standard output\n"));
+ fprintf(output, _(" \\echo_stderr [STRING] write string to standard error\n"));
fprintf(output, _(" \\i FILE execute commands from file\n"));
fprintf(output, _(" \\ir FILE as \\i, but relative to location of current script\n"));
fprintf(output, _(" \\o [FILE] send all query results to file or |pipe\n"));
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index bcddc7601e..a76be5f068 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1406,7 +1406,7 @@ psql_completion(const char *text, int start, int end)
"\\dm", "\\dn", "\\do", "\\dO", "\\dp", "\\dP", "\\dPi", "\\dPt",
"\\drds", "\\dRs", "\\dRp", "\\ds", "\\dS",
"\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dy",
- "\\e", "\\echo", "\\ef", "\\elif", "\\else", "\\encoding",
+ "\\e", "\\echo", "\\echo_stderr", "\\ef", "\\elif", "\\else", "\\encoding",
"\\endif", "\\errverbose", "\\ev",
"\\f",
"\\g", "\\gdesc", "\\gexec", "\\gset", "\\gx",
--------------2.20.1--
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-21 18:42 Pavel Stehule <pavel.stehule@gmail.com>
parent: David Fetter <david@fetter.org>
1 sibling, 0 replies; 11+ messages in thread
From: Pavel Stehule @ 2019-04-21 18:42 UTC (permalink / raw)
To: David Fetter <david@fetter.org>; +Cc: pgsql-hackers
ne 21. 4. 2019 v 20:31 odesÃlatel David Fetter <david@fetter.org> napsal:
> Folks,
>
> Any interest in this?
>
has sense
Pavel
> Best,
> David.
> --
> David Fetter <david(at)fetter(dot)org> http://fetter.org/
> Phone: +1 415 235 3778
>
> Remember to vote!
> Consider donating to Postgres: http://www.postgresql.org/about/donate
>
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-21 19:31 Fabien COELHO <coelho@cri.ensmp.fr>
parent: David Fetter <david@fetter.org>
1 sibling, 2 replies; 11+ messages in thread
From: Fabien COELHO @ 2019-04-21 19:31 UTC (permalink / raw)
To: David Fetter <david@fetter.org>; +Cc: pgsql-hackers
> Any interest in this?
Yep, although I'm not sure of the suggested command name. More
suggestions:
\stderr ...
\err ...
\error ...
\warn ...
\warning ...
--
Fabien.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-21 21:38 David Fetter <david@fetter.org>
parent: Fabien COELHO <coelho@cri.ensmp.fr>
1 sibling, 0 replies; 11+ messages in thread
From: David Fetter @ 2019-04-21 21:38 UTC (permalink / raw)
To: Fabien COELHO <coelho@cri.ensmp.fr>; +Cc: pgsql-hackers
On Sun, Apr 21, 2019 at 09:31:16PM +0200, Fabien COELHO wrote:
> > Any interest in this?
>
> Yep, although I'm not sure of the suggested command name. More suggestions:
> \stderr ...
> \err ...
> \error ...
> \warn ...
> \warning ...
Naming Things is one of the two[1] hard problems in CS.
I'm happy with whatever the community consensus comes out to be.
Best,
David.
[1] The others are cache coherency and off-by-one
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-22 03:52 Corey Huinker <corey.huinker@gmail.com>
parent: Fabien COELHO <coelho@cri.ensmp.fr>
1 sibling, 1 reply; 11+ messages in thread
From: Corey Huinker @ 2019-04-22 03:52 UTC (permalink / raw)
To: Fabien COELHO <coelho@cri.ensmp.fr>; +Cc: David Fetter <david@fetter.org>; pgsql-hackers
>
>
> \warn ...
> \warning ...
>
These two seem about the best to me, drawing from the perl warn command.
I suppose we could go the bash &2 route here, but I don't want to.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-22 07:04 Fabien COELHO <coelho@cri.ensmp.fr>
parent: Corey Huinker <corey.huinker@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Fabien COELHO @ 2019-04-22 07:04 UTC (permalink / raw)
To: Corey Huinker <corey.huinker@gmail.com>; +Cc: David Fetter <david@fetter.org>; pgsql-hackers
Hello Corey,
>> \warn ...
>> \warning ...
>
> These two seem about the best to me, drawing from the perl warn command.
Yep, I was thinking of perl & gmake. Maybe the 4 letter option is better
because its the same length as "echo".
> I suppose we could go the bash &2 route here, but I don't want to.
I agree on this one.
--
Fabien.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-22 13:45 David Fetter <david@fetter.org>
parent: Fabien COELHO <coelho@cri.ensmp.fr>
0 siblings, 2 replies; 11+ messages in thread
From: David Fetter @ 2019-04-22 13:45 UTC (permalink / raw)
To: Fabien COELHO <coelho@cri.ensmp.fr>; +Cc: Corey Huinker <corey.huinker@gmail.com>; pgsql-hackers
On Mon, Apr 22, 2019 at 09:04:08AM +0200, Fabien COELHO wrote:
>
> Hello Corey,
>
> > > \warn ...
> > > \warning ...
> >
> > These two seem about the best to me, drawing from the perl warn command.
>
> Yep, I was thinking of perl & gmake. Maybe the 4 letter option is better
> because its the same length as "echo".
>
> > I suppose we could go the bash &2 route here, but I don't want to.
>
> I agree on this one.
Please find attached v2, name is now \warn.
How might we test this portably?
Best,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
Attachments:
[text/x-diff] v2-0001-Add-warn-to-psql.patch (4.2K, ../../20190422134532.GV28936@fetter.org/2-v2-0001-Add-warn-to-psql.patch)
download | inline diff:
From db987e21d0e25ce306d5482fa78d71de8454f1aa Mon Sep 17 00:00:00 2001
From: David Fetter <david@fetter.org>
Date: Sun, 21 Apr 2019 11:08:40 -0700
Subject: [PATCH v2] Add \warn to psql
To: hackers
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------2.20.1"
This is a multi-part message in MIME format.
--------------2.20.1
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit
- Does what it says on the label
- Do we have any way to test this?
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index b86764003d..4edf8e67a1 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1879,6 +1879,26 @@ Tue Oct 26 21:40:57 CEST 1999
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><literal>\warn <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
+ <listitem>
+ <para>
+ Prints the arguments to the standard error, separated by one
+ space and followed by a newline. This can be useful to
+ intersperse information in the output of scripts when not polluting
+ standard output is desired. For example:
+
+<programlisting>
+=> <userinput>\echo :variable</userinput>
+=> <userinput>\warn `date`</userinput>
+Sun Apr 21 10:48:11 PDT 2019
+</programlisting>
+ If the first argument is an unquoted <literal>-n</literal> the trailing
+ newline is not written.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><literal>\ef <optional> <replaceable class="parameter">function_description</replaceable> <optional> <replaceable class="parameter">line_number</replaceable> </optional> </optional> </literal></term>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 8254d61099..9425f02005 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -319,7 +319,7 @@ exec_command(const char *cmd,
status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
else if (strcmp(cmd, "ev") == 0)
status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
- else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0)
+ else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "warn") == 0 || strcmp(cmd, "qecho") == 0)
status = exec_command_echo(scan_state, active_branch, cmd);
else if (strcmp(cmd, "elif") == 0)
status = exec_command_elif(scan_state, cstack, query_buf);
@@ -1114,7 +1114,7 @@ exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
}
/*
- * \echo and \qecho -- echo arguments to stdout or query output
+ * \echo, \warn and \qecho -- echo arguments to stdout or query output
*/
static backslashResult
exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
@@ -1129,6 +1129,8 @@ exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
if (strcmp(cmd, "qecho") == 0)
fout = pset.queryFout;
+ else if (strcmp(cmd, "warn") == 0)
+ fout = stderr;
else
fout = stdout;
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index d6d41b51d5..2ecb8b73c1 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -207,6 +207,7 @@ slashUsage(unsigned short int pager)
fprintf(output, _("Input/Output\n"));
fprintf(output, _(" \\copy ... perform SQL COPY with data stream to the client host\n"));
fprintf(output, _(" \\echo [STRING] write string to standard output\n"));
+ fprintf(output, _(" \\warn [STRING] write string to standard error\n"));
fprintf(output, _(" \\i FILE execute commands from file\n"));
fprintf(output, _(" \\ir FILE as \\i, but relative to location of current script\n"));
fprintf(output, _(" \\o [FILE] send all query results to file or |pipe\n"));
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index bcddc7601e..383a17a7f4 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1421,7 +1421,7 @@ psql_completion(const char *text, int start, int end)
"\\t", "\\T", "\\timing",
"\\unset",
"\\x",
- "\\w", "\\watch",
+ "\\w","\\warn", "\\watch",
"\\z",
"\\!", "\\?",
NULL
--------------2.20.1--
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-22 17:46 Fabien COELHO <fabien.coelho@mines-paristech.fr>
parent: David Fetter <david@fetter.org>
1 sibling, 0 replies; 11+ messages in thread
From: Fabien COELHO @ 2019-04-22 17:46 UTC (permalink / raw)
To: David Fetter <david@fetter.org>; +Cc: Corey Huinker <corey.huinker@gmail.com>; pgsql-hackers
>>>> \warn ...
>>>> \warning ...
>>>
>>> These two seem about the best to me, drawing from the perl warn command.
>>
>> Yep, I was thinking of perl & gmake. Maybe the 4 letter option is better
>> because its the same length as "echo".
>>
>>> I suppose we could go the bash &2 route here, but I don't want to.
>>
>> I agree on this one.
>
> Please find attached v2, name is now \warn.
>
> How might we test this portably?
TAP testing? see pgbench which has tap test which can test stdout & stderr
by calling utility command_checks_all, the same could be done with psql.
--
Fabien.
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-27 14:05 Fabien COELHO <coelho@cri.ensmp.fr>
parent: David Fetter <david@fetter.org>
1 sibling, 1 reply; 11+ messages in thread
From: Fabien COELHO @ 2019-04-27 14:05 UTC (permalink / raw)
To: David Fetter <david@fetter.org>; +Cc: Corey Huinker <corey.huinker@gmail.com>; pgsql-hackers
Hello David,
> Please find attached v2, name is now \warn.
Patch applies cleanly, compiles, "make check ok", although there are no
tests. Doc gen ok.
Code is pretty straightforward.
I'd put the commands in alphabetical order (echo, qecho, warn) instead of
e/w/q in the condition.
The -n trick does not appear in the help lines, ISTM that it could fit, so
maybe it could be added, possibly something like:
\echo [-n] [TEXT] write string to stdout, possibly without trailing newline
and same for \warn and \qecho?
> How might we test this portably?
Hmmm... TAP tests are expected to be portable. Attached a simple POC,
which could be extended to test many more things which are currently out
of coverage (src/bin/psql stuff is covered around 40% only).
--
Fabien.
Attachments:
[text/x-diff] psql-warn-2-tap.patch (2.9K, ../../alpine.DEB.2.21.1904271151260.29865@lancre/2-psql-warn-2-tap.patch)
download | inline diff:
diff --git a/src/bin/psql/.gitignore b/src/bin/psql/.gitignore
index c2862b12d6..d324c1c1fa 100644
--- a/src/bin/psql/.gitignore
+++ b/src/bin/psql/.gitignore
@@ -3,3 +3,4 @@
/sql_help.c
/psql
+/tmp_check
diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile
index 69bb297fe7..9473ab01cb 100644
--- a/src/bin/psql/Makefile
+++ b/src/bin/psql/Makefile
@@ -60,8 +60,15 @@ uninstall:
clean distclean:
rm -f psql$(X) $(OBJS) lex.backup
+ rm -rf tmp_check
# files removed here are supposed to be in the distribution tarball,
# so do not clean them in the clean/distclean rules
maintainer-clean: distclean
rm -f sql_help.h sql_help.c psqlscanslash.c
+
+check:
+ $(prove_check)
+
+installcheck:
+ $(prove_installcheck)
diff --git a/src/bin/psql/t/001_psql.pl b/src/bin/psql/t/001_psql.pl
new file mode 100644
index 0000000000..32dd43279b
--- /dev/null
+++ b/src/bin/psql/t/001_psql.pl
@@ -0,0 +1,32 @@
+use strict;
+use warnings;
+
+use PostgresNode;
+use TestLib;
+use Test::More;
+
+my $node = get_new_node('main');
+$node->init();
+$node->start();
+
+# invoke psql
+# - opts: space-separated options and arguments
+# - stat: expected exit status
+# - in: input stream
+# - out: list of re to check on stdout
+# - err: list of re to check on stderr
+# - name: of the test
+sub psql
+{
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+ my ($opts, $stat, $in, $out, $err, $name) = @_;
+ my @cmd = ('psql', split /\s+/, $opts);
+ $node->command_checks_all(\@cmd, $stat, $out, $err, $name, $in);
+ return;
+}
+
+psql('-c \\q', 0, '', [ qr{^$} ], [ qr{^$} ], 'psql -c');
+psql('', 0,"\\echo hello\n\\warn world\n\\q\n", [ qr{^hello$} ], [ qr{^world$} ], 'psql in/out/err');
+
+$node->stop();
+done_testing();
diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm
index a164cdbd8c..6ad7f681ae 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -519,22 +519,24 @@ sub command_fails_like
}
# Run a command and check its status and outputs.
-# The 5 arguments are:
+# The 5 to 6 arguments are:
# - cmd: ref to list for command, options and arguments to run
# - ret: expected exit status
# - out: ref to list of re to be checked against stdout (all must match)
# - err: ref to list of re to be checked against stderr (all must match)
# - test_name: name of test
+# - in: standard input
sub command_checks_all
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
- my ($cmd, $expected_ret, $out, $err, $test_name) = @_;
+ my ($cmd, $expected_ret, $out, $err, $test_name, $in) = @_;
+ $in = '' if not defined $in;
# run command
my ($stdout, $stderr);
print("# Running: " . join(" ", @{$cmd}) . "\n");
- IPC::Run::run($cmd, '>', \$stdout, '2>', \$stderr);
+ IPC::Run::run($cmd, '<', \$in, '>', \$stdout, '2>', \$stderr);
# See http://perldoc.perl.org/perlvar.html#%24CHILD_ERROR
my $ret = $?;
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-27 17:15 David Fetter <david@fetter.org>
parent: Fabien COELHO <coelho@cri.ensmp.fr>
0 siblings, 1 reply; 11+ messages in thread
From: David Fetter @ 2019-04-27 17:15 UTC (permalink / raw)
To: Fabien COELHO <coelho@cri.ensmp.fr>; +Cc: Corey Huinker <corey.huinker@gmail.com>; pgsql-hackers
On Sat, Apr 27, 2019 at 04:05:20PM +0200, Fabien COELHO wrote:
>
> Hello David,
>
> > Please find attached v2, name is now \warn.
>
> Patch applies cleanly, compiles, "make check ok", although there are no
> tests. Doc gen ok.
>
> Code is pretty straightforward.
>
> I'd put the commands in alphabetical order (echo, qecho, warn) instead of
> e/w/q in the condition.
Done.
> The -n trick does not appear in the help lines, ISTM that it could fit, so
> maybe it could be added, possibly something like:
>
> \echo [-n] [TEXT] write string to stdout, possibly without trailing newline
>
> and same for \warn and \qecho?
Makes sense, but I put it there just for \echo to keep lines short.
> > How might we test this portably?
>
> Hmmm... TAP tests are expected to be portable. Attached a simple POC, which
> could be extended to test many more things which are currently out of
> coverage (src/bin/psql stuff is covered around 40% only).
Thanks for putting this together. I've added this test, and agree that
increasing coverage is important for another patch.
Best,
David.
--
David Fetter <david(at)fetter(dot)org> http://fetter.org/
Phone: +1 415 235 3778
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
Attachments:
[text/x-diff] v3-0001-Add-warn-to-psql.patch (7.7K, ../../20190427171548.GM28936@fetter.org/2-v3-0001-Add-warn-to-psql.patch)
download | inline diff:
From 2f7cbf9980c5ce1049728a0e2a3444cbd106f253 Mon Sep 17 00:00:00 2001
From: David Fetter <david@fetter.org>
Date: Sun, 21 Apr 2019 11:08:40 -0700
Subject: [PATCH v3] Add \warn to psql
To: hackers
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------2.20.1"
This is a multi-part message in MIME format.
--------------2.20.1
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit
- Does what it says on the label
- Adds TAP tests for same
- In passing, expose the -n option for \echo, \qecho, and \warn in \? output
create mode 100644 src/bin/psql/t/001_psql.pl
--------------2.20.1
Content-Type: text/x-patch; name="v3-0001-Add-warn-to-psql.patch"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="v3-0001-Add-warn-to-psql.patch"
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index b86764003d..4edf8e67a1 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1879,6 +1879,26 @@ Tue Oct 26 21:40:57 CEST 1999
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><literal>\warn <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
+ <listitem>
+ <para>
+ Prints the arguments to the standard error, separated by one
+ space and followed by a newline. This can be useful to
+ intersperse information in the output of scripts when not polluting
+ standard output is desired. For example:
+
+<programlisting>
+=> <userinput>\echo :variable</userinput>
+=> <userinput>\warn `date`</userinput>
+Sun Apr 21 10:48:11 PDT 2019
+</programlisting>
+ If the first argument is an unquoted <literal>-n</literal> the trailing
+ newline is not written.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><literal>\ef <optional> <replaceable class="parameter">function_description</replaceable> <optional> <replaceable class="parameter">line_number</replaceable> </optional> </optional> </literal></term>
diff --git a/src/bin/psql/.gitignore b/src/bin/psql/.gitignore
index c2862b12d6..d324c1c1fa 100644
--- a/src/bin/psql/.gitignore
+++ b/src/bin/psql/.gitignore
@@ -3,3 +3,4 @@
/sql_help.c
/psql
+/tmp_check
diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile
index 69bb297fe7..9473ab01cb 100644
--- a/src/bin/psql/Makefile
+++ b/src/bin/psql/Makefile
@@ -60,8 +60,15 @@ uninstall:
clean distclean:
rm -f psql$(X) $(OBJS) lex.backup
+ rm -rf tmp_check
# files removed here are supposed to be in the distribution tarball,
# so do not clean them in the clean/distclean rules
maintainer-clean: distclean
rm -f sql_help.h sql_help.c psqlscanslash.c
+
+check:
+ $(prove_check)
+
+installcheck:
+ $(prove_installcheck)
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 8254d61099..9425f02005 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -319,7 +319,7 @@ exec_command(const char *cmd,
status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
else if (strcmp(cmd, "ev") == 0)
status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
- else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0)
+ else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "warn") == 0 || strcmp(cmd, "qecho") == 0)
status = exec_command_echo(scan_state, active_branch, cmd);
else if (strcmp(cmd, "elif") == 0)
status = exec_command_elif(scan_state, cstack, query_buf);
@@ -1114,7 +1114,7 @@ exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
}
/*
- * \echo and \qecho -- echo arguments to stdout or query output
+ * \echo, \warn and \qecho -- echo arguments to stdout or query output
*/
static backslashResult
exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
@@ -1129,6 +1129,8 @@ exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
if (strcmp(cmd, "qecho") == 0)
fout = pset.queryFout;
+ else if (strcmp(cmd, "warn") == 0)
+ fout = stderr;
else
fout = stdout;
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index d6d41b51d5..434388608a 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -206,11 +206,12 @@ slashUsage(unsigned short int pager)
fprintf(output, _("Input/Output\n"));
fprintf(output, _(" \\copy ... perform SQL COPY with data stream to the client host\n"));
- fprintf(output, _(" \\echo [STRING] write string to standard output\n"));
+ fprintf(output, _(" \\echo [-n] [STRING] write string to standard output (-n leaves off newline)\n"));
+ fprintf(output, _(" \\qecho [-n] [STRING] write string to query output stream (see \\o)\n"));
+ fprintf(output, _(" \\warn [-n] [STRING] write string to standard error\n"));
fprintf(output, _(" \\i FILE execute commands from file\n"));
fprintf(output, _(" \\ir FILE as \\i, but relative to location of current script\n"));
fprintf(output, _(" \\o [FILE] send all query results to file or |pipe\n"));
- fprintf(output, _(" \\qecho [STRING] write string to query output stream (see \\o)\n"));
fprintf(output, "\n");
fprintf(output, _("Conditional\n"));
diff --git a/src/bin/psql/t/001_psql.pl b/src/bin/psql/t/001_psql.pl
new file mode 100644
index 0000000000..32dd43279b
--- /dev/null
+++ b/src/bin/psql/t/001_psql.pl
@@ -0,0 +1,32 @@
+use strict;
+use warnings;
+
+use PostgresNode;
+use TestLib;
+use Test::More;
+
+my $node = get_new_node('main');
+$node->init();
+$node->start();
+
+# invoke psql
+# - opts: space-separated options and arguments
+# - stat: expected exit status
+# - in: input stream
+# - out: list of re to check on stdout
+# - err: list of re to check on stderr
+# - name: of the test
+sub psql
+{
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+ my ($opts, $stat, $in, $out, $err, $name) = @_;
+ my @cmd = ('psql', split /\s+/, $opts);
+ $node->command_checks_all(\@cmd, $stat, $out, $err, $name, $in);
+ return;
+}
+
+psql('-c \\q', 0, '', [ qr{^$} ], [ qr{^$} ], 'psql -c');
+psql('', 0,"\\echo hello\n\\warn world\n\\q\n", [ qr{^hello$} ], [ qr{^world$} ], 'psql in/out/err');
+
+$node->stop();
+done_testing();
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index bcddc7601e..383a17a7f4 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1421,7 +1421,7 @@ psql_completion(const char *text, int start, int end)
"\\t", "\\T", "\\timing",
"\\unset",
"\\x",
- "\\w", "\\watch",
+ "\\w","\\warn", "\\watch",
"\\z",
"\\!", "\\?",
NULL
diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm
index a164cdbd8c..6ad7f681ae 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -519,22 +519,24 @@ sub command_fails_like
}
# Run a command and check its status and outputs.
-# The 5 arguments are:
+# The 5 to 6 arguments are:
# - cmd: ref to list for command, options and arguments to run
# - ret: expected exit status
# - out: ref to list of re to be checked against stdout (all must match)
# - err: ref to list of re to be checked against stderr (all must match)
# - test_name: name of test
+# - in: standard input
sub command_checks_all
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
- my ($cmd, $expected_ret, $out, $err, $test_name) = @_;
+ my ($cmd, $expected_ret, $out, $err, $test_name, $in) = @_;
+ $in = '' if not defined $in;
# run command
my ($stdout, $stderr);
print("# Running: " . join(" ", @{$cmd}) . "\n");
- IPC::Run::run($cmd, '>', \$stdout, '2>', \$stderr);
+ IPC::Run::run($cmd, '<', \$in, '>', \$stdout, '2>', \$stderr);
# See http://perldoc.perl.org/perlvar.html#%24CHILD_ERROR
my $ret = $?;
--------------2.20.1--
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: [PATCH v1] Add \echo_stderr to psql
@ 2019-04-27 20:09 Fabien COELHO <coelho@cri.ensmp.fr>
parent: David Fetter <david@fetter.org>
0 siblings, 0 replies; 11+ messages in thread
From: Fabien COELHO @ 2019-04-27 20:09 UTC (permalink / raw)
To: David Fetter <david@fetter.org>; +Cc: Corey Huinker <corey.huinker@gmail.com>; pgsql-hackers
Hello David,
About v3. Applies, compiles, global & local make check are ok. doc gen ok.
>> I'd put the commands in alphabetical order (echo, qecho, warn) instead of
>> e/w/q in the condition.
>
> Done.
Cannot see it:
+ else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "warn") == 0 || strcmp(cmd, "qecho") == 0)
>> The -n trick does not appear in the help lines, ISTM that it could fit, so
>> maybe it could be added, possibly something like:
>>
>> \echo [-n] [TEXT] write string to stdout, possibly without trailing newline
>>
>> and same for \warn and \qecho?
>
> Makes sense, but I put it there just for \echo to keep lines short.
I think that putting together the 3 echo variants help makes sense, but
maybe someone will object about breaking the abc order.
>>> How might we test this portably?
>>
>> Hmmm... TAP tests are expected to be portable. Attached a simple POC, which
>> could be extended to test many more things which are currently out of
>> coverage (src/bin/psql stuff is covered around 40% only).
>
> Thanks for putting this together. I've added this test, and agree that
> increasing coverage is important for another patch.
Yep.
--
Fabien.
^ permalink raw reply [nested|flat] 11+ messages in thread
end of thread, other threads:[~2019-04-27 20:09 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-04-21 18:31 [PATCH v1] Add \echo_stderr to psql David Fetter <david@fetter.org>
2019-04-21 18:42 ` Pavel Stehule <pavel.stehule@gmail.com>
2019-04-21 19:31 ` Fabien COELHO <coelho@cri.ensmp.fr>
2019-04-21 21:38 ` David Fetter <david@fetter.org>
2019-04-22 03:52 ` Corey Huinker <corey.huinker@gmail.com>
2019-04-22 07:04 ` Fabien COELHO <coelho@cri.ensmp.fr>
2019-04-22 13:45 ` David Fetter <david@fetter.org>
2019-04-22 17:46 ` Fabien COELHO <fabien.coelho@mines-paristech.fr>
2019-04-27 14:05 ` Fabien COELHO <coelho@cri.ensmp.fr>
2019-04-27 17:15 ` David Fetter <david@fetter.org>
2019-04-27 20:09 ` Fabien COELHO <coelho@cri.ensmp.fr>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox