From 93a0a15f122f9c9286d3d989861c787fb63b6a1d Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Mon, 1 Oct 2018 21:39:35 -0500 Subject: [PATCH] Refactored to move HTTP query params to CLI arguments translation into the cliutils package. --- pit.nimble | 2 +- src/pit_api.nim | 33 +++++---------------------------- src/pitpkg/version.nim | 2 +- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/pit.nimble b/pit.nimble index b191193..278c4c6 100644 --- a/pit.nimble +++ b/pit.nimble @@ -11,5 +11,5 @@ bin = @["pit", "pit_api"] # Dependencies -requires @[ "nim >= 0.18.0", "cliutils 0.4.1", "docopt 0.6.5", "jester 0.2.0", +requires @[ "nim >= 0.18.0", "cliutils 0.5.0", "docopt 0.6.5", "jester 0.2.0", "langutils >= 0.4.0", "timeutils 0.3.0", "uuids 0.1.9" ] diff --git a/src/pit_api.nim b/src/pit_api.nim index af735dd..dd6bc6e 100644 --- a/src/pit_api.nim +++ b/src/pit_api.nim @@ -46,25 +46,6 @@ template checkAuth(cfg: PitApiCfg) = response.data[2]["Content-Type"] = TXT response.data[3] = getCurrentExceptionMsg() -proc paramsToArgs(params: StringTableRef): tuple[stripAnsi: bool, args: seq[string]] = - result = (false, @[]) - - if params.hasKey("color"): - if params["color"] != "true": - result[0] = true - - for k,v in params: - if k == "color": continue - elif k.startsWith("arg"): result[1].add(v) # support ?arg1=val1&arg2=val2 -> cmd val1 val2 - else : - result[1].add("--" & k) - if v != "true": result[1].add(v) # support things like ?verbose=true -> cmd --verbose - -let STRIP_ANSI_REGEX = re"\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]" - -proc stripAnsi(str: string): string = - return str.replace(STRIP_ANSI_REGEX, "") - proc start*(cfg: PitApiCfg) = var stopFuture = newFuture[void]() @@ -81,15 +62,13 @@ proc start*(cfg: PitApiCfg) = get "/issues": checkAuth(cfg); if not authed: return true - var (hasColor, args) = paramsToArgs(request.params) + var args = queryParamsToCliArgs(request.params) args = @["list"] & args info "args: \n" & args.join(" ") let execResult = execWithOutput("pit", ".", args) if execResult[2] != 0: resp(Http500, stripAnsi($execResult[0] & "\n" & $execResult[1]), TXT) - else: - if hasColor: resp(stripAnsi(execResult[0]), TXT) - else: resp(execResult[0], TXT) + else: resp(stripAnsi(execResult[0]), TXT) post "/issues": checkAuth(cfg); if not authed: return true @@ -97,15 +76,13 @@ proc start*(cfg: PitApiCfg) = get "/issue/@issueId": checkAuth(cfg); if not authed: return true - var (hasColor, args) = paramsToArgs(request.params) - args = @["list", issueId] & args + var args = queryParamsToCliArgs(request.params) + args = @["list", @"issueId"] & args info "args: \n" & args.join(" ") let execResult = execWithOutput("pit", ".", args) if execResult[2] != 0: resp(Http500, stripAnsi($execResult[0] & "\n" & $execResult[1]), TXT) - else: - if hasColor: resp(stripAnsi(execResult[0]), TXT) - else: resp(execResult[0], TXT) + else: resp(stripAnsi(execResult[0]), TXT) waitFor(stopFuture) diff --git a/src/pitpkg/version.nim b/src/pitpkg/version.nim index e8c3323..8fe58f3 100644 --- a/src/pitpkg/version.nim +++ b/src/pitpkg/version.nim @@ -1 +1 @@ -const PIT_VERSION = "4.4.1" +const PIT_VERSION = "4.4.2"