From 551461a909c96d201acf5a7a616d49e8e094fba7 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Fri, 25 Sep 2020 16:26:30 +0200 Subject: [PATCH] cli/sql: new local command `\echo` Release note (cli change): The SQL shell (`cockroach sql`, `cockroach demo`) now supports the client-side command `\echo`, like `psql`. This can be used e.g. to generate informational output when executing SQL scripts non-interactively. --- pkg/cli/interactive_tests/test_local_cmds.tcl | 12 ++++++++++++ pkg/cli/sql.go | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/cli/interactive_tests/test_local_cmds.tcl b/pkg/cli/interactive_tests/test_local_cmds.tcl index 25c908e15d8e..7e042b2184a0 100755 --- a/pkg/cli/interactive_tests/test_local_cmds.tcl +++ b/pkg/cli/interactive_tests/test_local_cmds.tcl @@ -221,6 +221,18 @@ eexpect "with no argument" eexpect root@ end_test +start_test "Check that \\echo behaves well." +send "\\echo\r" +eexpect "\r\n" +eexpect "\r\n" +eexpect root@ + +send "\\echo hello world\r" +# echo removes double spaces within the line. That's expected. +eexpect "hello world" +eexpect root@ +end_test + start_test "Check that commands are also recognized with a final semicolon." send "\\set;\r" eexpect "display_format" diff --git a/pkg/cli/sql.go b/pkg/cli/sql.go index 381933a83d74..6062b72c78a1 100644 --- a/pkg/cli/sql.go +++ b/pkg/cli/sql.go @@ -64,6 +64,9 @@ Query Buffer \r during a multi-line statement, erase all the SQL entered so far. \| CMD run an external command and run its output as SQL statements. +Input/Output + \echo [STRING] write the provided string to standard output. + Informational \l list all databases in the CockroachDB cluster. \dt show the tables of the current schema in the current database. @@ -1029,6 +1032,9 @@ func (c *cliState) doHandleCliCmd(loopState, nextState cliStateEnum) cliStateEnu case `\`, `\?`, `\help`: c.printCliHelp() + case `\echo`: + fmt.Println(strings.Join(cmd[1:], " ")) + case `\set`: return c.handleSet(cmd[1:], loopState, errState)