From e6a3a1bdc60653977b4bbf606486ba8d0ae99446 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Mon, 22 May 2017 18:06:56 +0200 Subject: [PATCH] TT#16518 ngcp-dlg(cnt|list)-clean: improve arg parse * use lua-argparse to improve argument parsing Change-Id: I25b1a216294f77849101e58eae8fe9daa8da67b7 --- scripts/ngcp-dlgcnt-clean | 30 +++++++++++++++--------------- scripts/ngcp-dlglist-clean | 22 ++++++++++------------ t/Dockerfile | 3 ++- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/scripts/ngcp-dlgcnt-clean b/scripts/ngcp-dlgcnt-clean index 8ae53fd..c4586e0 100755 --- a/scripts/ngcp-dlgcnt-clean +++ b/scripts/ngcp-dlgcnt-clean @@ -3,6 +3,16 @@ local NGCPDlg = require 'ngcp.dlgcnt' local config = "/etc/kamailio/proxy/dlgcnt.lua.cfg" +local argparse = require "argparse" +local parser = argparse() { + name = "ngcp-dlgcnt-clean", + description = "helper to remove kamailio dialogs from redis db" +} + +parser:argument("callid", "Call-Id to remove") +parser:flag("-c --config-db", "redis db defined at config") +parser:flag("-C --config-host", "redis server defined at config") + -- luacheck: globals dlg_config sr = {} sr.log = function (level, str) @@ -21,26 +31,16 @@ local function get_config() end end -local function usage() - print("ngcp-dlgcnt-clean callid") -end - -if #arg ~= 1 then - io.stderr:write("wrong number of arguments\n"); - usage() - os.exit(2) -end - -if arg[1] == "-h" then usage(); os.exit(0); end +local args = parser:parse() get_config() local dlg = NGCPDlg:new() -if arg[1] == '-c' then - print(tostring(dlg.config.pair.db)) +if args.config_db then + print(tostring(dlg_config.pair.db)) os.exit(0) -elseif arg[1] == '-C' then +elseif args.config_host then print(tostring(dlg_config.pair.host)) os.exit(0) end @@ -55,4 +55,4 @@ if dlg_config then --print(string.format("dlg.config:%s", ut.tostring(dlg.config))) end -dlg:del(arg[1]) +dlg:del(args.callid) diff --git a/scripts/ngcp-dlglist-clean b/scripts/ngcp-dlglist-clean index 38ab5b6..0ddb45d 100644 --- a/scripts/ngcp-dlglist-clean +++ b/scripts/ngcp-dlglist-clean @@ -3,6 +3,14 @@ local NGCPDlgList = require 'ngcp.dlglist' local config = "/etc/kamailio/proxy/dlgcnt.lua.cfg" +local argparse = require "argparse" +local parser = argparse() { + name = "ngcp-dlglist-clean", + description = "helper to remove kamailio queue dialogs from redis db" +} + +parser:argument("callid", "Call-Id to remove") + -- luacheck: globals dlg_config sr = {} sr.log = function (level, str) @@ -21,17 +29,7 @@ local function get_config() end end -local function usage() - print("ngcp-dlglist-clean callid") -end - -if #arg ~= 1 then - io.stderr:write("wrong number of arguments\n"); - usage() - os.exit(2) -end - -if arg[1] == "-h" then usage(); os.exit(0); end +local args = parser:parse() get_config() @@ -46,4 +44,4 @@ if dlg_config then --print(string.format("dlg.config:%s", ut.tostring(dlg.config))) end -dlg:destroy(arg[1]) +dlg:destroy(args.callid) diff --git a/t/Dockerfile b/t/Dockerfile index 5f72046..d57e33b 100644 --- a/t/Dockerfile +++ b/t/Dockerfile @@ -10,7 +10,8 @@ ENV REFRESHED_AT 2016-12-2 # TODO - the release-trunk-jessie is too dynamic yet, though required for lua-unit backport RUN echo "deb https://deb.sipwise.com/autobuild/ release-trunk-jessie main" >>/etc/apt/sources.list -RUN apt-get update && apt-get install --assume-yes lua5.1 lua-unit lua-lemock lua-logging python-xmlrunner +RUN apt-get update && apt-get install --assume-yes \ + lua5.1 lua-unit lua-lemock lua-logging lua-argparse python-xmlrunner RUN echo './t/testrunner' >>/root/.bash_history