From: Cyrill Gorcunov <gorcunov@gmail.com> To: tml <tarantool-patches@freelists.org> Cc: Konstantin Osipov <kostja@tarantool.org>, Alexander Turenko <alexander.turenko@tarantool.org>, Cyrill Gorcunov <gorcunov@gmail.com> Subject: [tarantool-patches] [PATCH 3/6] box/console: Refactor command handling Date: Fri, 6 Sep 2019 00:28:12 +0300 [thread overview] Message-ID: <20190905212815.7311-4-gorcunov@gmail.com> (raw) In-Reply-To: <20190905212815.7311-1-gorcunov@gmail.com> We will need to parse and verify "\set" commands in remote console text-wire protocol thus to not duplicate the code lets factor helper out for reuse sake. Part-of #3834 --- v3: - More comments src/box/lua/console.lua | 43 +++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua index 5430e6787..04cca35ca 100644 --- a/src/box/lua/console.lua +++ b/src/box/lua/console.lua @@ -149,11 +149,16 @@ local function output_save(fmt, opts) } end -local function format(status, ...) +local function current_output() local d = box.session.storage.console_output_format if d == nil then - d = default_output_format + return default_output_format end + return d +end + +local function format(status, ...) + local d = current_output() return output_handlers[d["fmt"]](status, d["opts"], ...) end @@ -244,21 +249,46 @@ local operators = { q = quit } -local function preprocess(storage, line) +-- +-- Generate command arguments from the line to +-- be passed into command handlers. +local function parse_operators(line) local items = {} for item in string.gmatch(line, '([^%s]+)') do items[#items + 1] = item end if #items == 0 then - return help_wrapper() + return 0, nil end if operators[items[1]] == nil then + return #items, nil + end + return #items, items +end + +-- +-- Preprocess a command via operator helpers. +local function preprocess(storage, line) + local nr_items, items = parse_operators(line) + if nr_items == 0 then + return help_wrapper() + end + if items == nil then local msg = "Invalid command \\%s. Type \\help for help." return format(false, msg:format(items[1])) end return operators[items[1]](storage, unpack(items)) end +-- +-- Return a command without a leading slash. +local function get_command(line) + if line:sub(1, 1) == '\\' then + return line:sub(2) + end + return nil +end + -- -- Evaluate command on local instance -- @@ -266,8 +296,9 @@ local function local_eval(storage, line) if not line then return nil end - if line:sub(1, 1) == '\\' then - return preprocess(storage, line:sub(2)) + local command = get_command(line) + if command then + return preprocess(storage, command) end if storage.language == 'sql' then return format(pcall(box.execute, line)) -- 2.20.1
next prev parent reply other threads:[~2019-09-05 21:28 UTC|newest] Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-09-05 21:28 [tarantool-patches] [PATCH v3 0/6] box/console: Improve lua mode for remote console Cyrill Gorcunov 2019-09-05 21:28 ` [tarantool-patches] [PATCH 1/6] box/console: Add mapping for direct symbols Cyrill Gorcunov 2019-09-09 15:11 ` [tarantool-patches] " Sergey Ostanevich 2019-09-09 15:54 ` [tarantool-patches] " Cyrill Gorcunov 2019-09-10 3:59 ` [tarantool-patches] Re[2]: [tarantool-patches] " Sergey Ostanevich 2019-09-05 21:28 ` [tarantool-patches] [PATCH 2/6] box/console: Add explicit output EOS mapping Cyrill Gorcunov 2019-09-05 21:28 ` Cyrill Gorcunov [this message] 2019-09-05 21:28 ` [tarantool-patches] [PATCH 4/6] box/console: Fix hang in remote console lua mode Cyrill Gorcunov 2019-09-05 21:28 ` [tarantool-patches] [PATCH 5/6] box/console: Provide console.eos() api Cyrill Gorcunov 2019-09-05 21:28 ` [tarantool-patches] [PATCH 6/6] box/console: Rename output_parse to parse_output Cyrill Gorcunov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20190905212815.7311-4-gorcunov@gmail.com \ --to=gorcunov@gmail.com \ --cc=alexander.turenko@tarantool.org \ --cc=kostja@tarantool.org \ --cc=tarantool-patches@freelists.org \ --subject='Re: [tarantool-patches] [PATCH 3/6] box/console: Refactor command handling' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox