console
The console module allows one Tarantool instance to access another Tarantool instance, and allows one Tarantool instance to start listening on an admin port.
Methods14
function M.set_default_output(value: string)
Set default output format.
The possible values are 'yaml' (the default) or 'lua'. For Lua output, an optional 'line' or 'block' modifier can be appended (for example, 'lua,line' or 'lua,block') to format the result as a single line or as a block. The output format can be changed within a session by executing console.eval('\set output yaml|lua').
valuestringthe output format: 'yaml', 'lua', 'lua,line', or 'lua,block'.
function M.get_default_output(...) -> format { fmt: ("yaml"|"lua"), opts: ("line"|"block")? }
Get default output format.
Return the current default output format.
...
format{ fmt: ("yaml"|"lua"), opts: ("line"|"block")? }Current default output format.
function M.eos(eos_value: nil | string) -> eos nil | string
Set or get the end-of-output string for the current output format.
Without arguments, return the current end-of-output string. With an argument, set it for the lua output; if the current output is yaml, raise an error.
This is the string that appears at the end of output in a response to any Lua request. The default value is ; semicolon. Saying eos() will return the current value. For example, after require('console').eos('!!') responses will end with '!!'.
eos_valuenil | stringthe new end-of-output string.
eosnil | stringthe current end-of-output string, if no value is passed.
function M.delimiter(delim: nil | string) -> delimiter nil | string
Set a custom end-of-request marker for Tarantool console.
The default end-of-request marker is a newline (line feed). Custom markers are not necessary because Tarantool can tell when a multi-line request has not ended (for example, if it sees that a function declaration does not have an end keyword). Nonetheless for special needs, or for entering multi-line requests in older Tarantool versions, you can change the end-of-request marker. As a result, newline alone is not treated as end of request.
To go back to normal mode, say: console.delimiter('')<marker>.
Example:
tarantool> console = require('console'); console.delimiter('!')
---
...
tarantool> function f ()
> statement_1 = 'a'
> statement_2 = 'b'
> end!
---
...
tarantool> console.delimiter('')!
---
...
delimnil | stringa custom end-of-request marker for Tarantool console.
delimiternil | stringthe current delimiter, if no value is passed.
function M.initial_env() -> env table
Get the initial console environment.
If the consolesessionscope_vars compat option is set to new, each console session has its own variable scope (environment) created before execution of a first command. The initial environment is returned by console.initial_env(). It is a table that may be modified by a user or completely replaced with a new table using console.set_initial_env(). These calls don't affect existing console sessions, only ones that are created afterwards.
envtablethe initial console environment.
function M.set_initial_env(env: nil | table)
Set initial console environment.
Accepts a table or a nil value. If the value is nil, the initial environment is reset to a default value. See console.initial_env() for details.
envnil | tablethe initial console environment.
function M.eval(line: string) -> output nil | string
Evaluate a line of Lua code or a console command in the current console.
linestringthe line to evaluate.
outputnil | stringthe formatted output of the evaluation.
function M.on_start(foo: fun(self: table) -> nil | nil)
Set or clear the hook called when a console session starts.
foofun(self: table) -> nil | nilthe hook function, or nil to clear it.
function M.on_client_disconnect(foo: fun(self: table) -> nil | nil)
Set or clear the hook called when a connected client console disconnects.
foofun(self: table) -> nil | nilthe hook function, or nil to clear it.
function M.ac(yes_no: nil | boolean)
Set the auto-completion flag.
If auto-completion is true, and the user is using Tarantool as a client or the user is using Tarantool via console.connect(), then hitting the TAB key may cause tarantool to complete a word automatically. The default auto-completion value is true.
yes_nonil | booleanthe auto-completion flag.
function M.start() -> nil
Start the console on the current interactive terminal.
Example:
A special use of console.start() is with initialization files. Normally, if one starts the Tarantool instance with a tarantool initialization file there is no console. This can be remedied by adding these lines at the end of the initialization file:
local console = require('console')
console.start()
function M.connect(uri: string, opts: { timeout: number? } | nil) -> ok boolean
Connect to the instance at URI.
Change the prompt from 'tarantool>' to {uri}>, and act henceforth as a client until the user ends the session or types control-D.
The console.connect function allows one Tarantool instance, in interactive mode, to access another Tarantool instance. Subsequent requests will appear to be handled locally, but in reality the requests are being sent to the remote instance and the local instance is acting as a client. Once connection is successful, the prompt will change and subsequent requests are sent to, and executed on, the remote instance. Results are displayed on the local instance. To return to local mode, enter control-D.
If the Tarantool instance at uri requires authentication, the connection might look something like: console.connect('admin:secretpassword@distanthost.com:3301').
There are no restrictions on the types of requests that can be entered, except those which are due to privilege restrictions -- by default the login to the remote instance is done with user name = 'guest'. The remote instance could allow for this by granting at least one privilege: box.schema.user.grant('guest','execute','universe').
Possible errors: the connection will fail if the target Tarantool instance does not accept connections via box.cfg{listen=...} or console.listen(...).
Example:
tarantool> console = require('console')
---
...
tarantool> console.connect('198.18.44.44:3301')
---
...
198.18.44.44:3301> -- prompt is telling us that instance is remote
uristringthe URI of the remote instance.
opts{ timeout: number? } | nilconnection options.
okbooleantrue if the connection is established.
function M.listen(uri: nil | string | integer) -> socket table
Listen for incoming requests.
The primary way of listening for incoming requests is via the connection-information string, or URI, specified in box.cfg{listen=...}.
The alternative way of listening is via the URI specified in console.listen(...). This alternative way is called "administrative" or simply "admin port". The listening is usually over a local host with a Unix domain socket.
The "admin" address is the URI to listen on. If it is not specified, unix/:/tmp/tarantool-console.sock is used. The parameter is expressed with URI = Uniform Resource Identifier format, for example "/tmpdir/unixdomainsocket.sock", or a numeric TCP port. Connections are often made with telnet. A typical port value is 3313.
Example:
tarantool> console = require('console')
---
...
tarantool> console.listen('unix/:/tmp/X.sock')
... main/103/console/unix/:/tmp/X I> started
---
- fd: 6
name:
host: unix/
family: AF_UNIX
type: SOCK_STREAM
protocol: 0
port: /tmp/X.sock
...
urinil | string | integerthe URI or TCP port to listen on.
sockettablethe created server socket.
function M.completion_handler(text: string, start: integer, last: integer) -> string[] | nil
Completion handler used by the console for word completion.