From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 9ADB5244DC for ; Thu, 5 Sep 2019 17:28:54 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id c6lgnjcKG9JW for ; Thu, 5 Sep 2019 17:28:54 -0400 (EDT) Received: from mail-lf1-f65.google.com (mail-lf1-f65.google.com [209.85.167.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 416C820BA1 for ; Thu, 5 Sep 2019 17:28:54 -0400 (EDT) Received: by mail-lf1-f65.google.com with SMTP id t8so3211347lfc.13 for ; Thu, 05 Sep 2019 14:28:54 -0700 (PDT) From: Cyrill Gorcunov Subject: [tarantool-patches] [PATCH 3/6] box/console: Refactor command handling Date: Fri, 6 Sep 2019 00:28:12 +0300 Message-Id: <20190905212815.7311-4-gorcunov@gmail.com> In-Reply-To: <20190905212815.7311-1-gorcunov@gmail.com> References: <20190905212815.7311-1-gorcunov@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tml Cc: Konstantin Osipov , Alexander Turenko , Cyrill Gorcunov 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