From b91974bc3e9f6131018f031c642423ea7bed96ee Mon Sep 17 00:00:00 2001 From: Marco Kellershoff <1384938+gorillamoe@users.noreply.github.com> Date: Wed, 14 Aug 2024 18:10:18 +0200 Subject: [PATCH] fix ext-processing + add jwt example (#133) --- ...onment-variables-based-on-response-json.md | 12 +++--- lua/kulala/cmd/shell_utils.lua | 33 +++++++++++++++ lua/kulala/external_processing/init.lua | 41 ++++++++++++++----- lua/kulala/globals/init.lua | 2 +- 4 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 lua/kulala/cmd/shell_utils.lua diff --git a/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md b/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md index 83435c9..696ee66 100644 --- a/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md +++ b/docs/docs/usage/dynamically-setting-environment-variables-based-on-response-json.md @@ -41,6 +41,8 @@ If the response is a *complex* JSON object, you can use the `@env-stdin-cmd` directive to set environment variables using an external command (e.g., `jq`). +In this example `jq` is used to extract the `ctx` string from a JWT token. + ```http title="with-external-jq.http" POST https://httpbin.org/post HTTP/1.1 Content-Type: application/json @@ -48,13 +50,10 @@ Accept: application/json # Setting the environment variables to be used in the next request. # Any external command can be used to set the environment variables. # The command should output the environment variable as string. -# @env-stdin-cmd AUTH_TOKEN jq -rcj .json.token -# @env-stdin-cmd AUTH_USERNAME jq -rcj .json.username +# @env-stdin-cmd JWT_CONTEXT jq -r '.token | gsub("-";"+") | gsub("_";"/") | split(".") | .[1] | @base64d | fromjson | .ctx' { - "username": "{{USERNAME}}", - "password": "{{PASSWORD}}", - "token": "foobar" + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0eXBlIjoiZ29yaWxsYSIsIm5hbWUiOiJHb3JpbGxhIE1vZSIsImN0eCI6IlNvbWUgY29udGV4dCIsIndlYnNpdGUiOiJodHRwczovL2dvcmlsbGEubW9lIn0.YmEG9bOo1o9opeWnCsfW621A-sB_5WXSBI2FjtvwXlk" } ### @@ -62,10 +61,9 @@ Accept: application/json POST https://httpbin.org/post HTTP/1.1 Content-Type: application/json Accept: application/json -Authorization: Bearer {{AUTH_TOKEN}} { "success": true, - "username": "{{AUTH_USERNAME}}" + "context": "{{JWT_CONTEXT}}" } ``` diff --git a/lua/kulala/cmd/shell_utils.lua b/lua/kulala/cmd/shell_utils.lua new file mode 100644 index 0000000..2329eea --- /dev/null +++ b/lua/kulala/cmd/shell_utils.lua @@ -0,0 +1,33 @@ +local M = {} + +local cache = {} + +M.has_command = function(cmd) + return vim.fn.executable(cmd) == 1 +end + +M.has_powershell = function() + if cache.has_powershell ~= nil then + return cache._has_powershell + end + cache.has_powershell = M.has_command("powershell") + return cache.has_powershell +end + +M.has_sh = function() + if cache.has_sh ~= nil then + return cache.has_sh + end + cache.has_sh = M.has_command("sh") + return cache.has_sh +end + +M.has_zsh = function() + if cache.has_zsh ~= nil then + return cache.has_zsh + end + cache.has_zsh = M.has_command("zsh") + return cache.has_zsh +end + +return M diff --git a/lua/kulala/external_processing/init.lua b/lua/kulala/external_processing/init.lua index c960def..9d11993 100644 --- a/lua/kulala/external_processing/init.lua +++ b/lua/kulala/external_processing/init.lua @@ -1,26 +1,47 @@ local FS = require("kulala.utils.fs") +local ShellUtils = require("kulala.cmd.shell_utils") local DB = require("kulala.db") +local Logger = require("kulala.logger") local M = {} M.env_stdin_cmd = function(cmdstring, contents) local cmd = {} - local splitted = vim.split(cmdstring, " ") - local env_name = splitted[1] - local cmd_exists = FS.command_exists(splitted[2]) - if not cmd_exists then - vim.notify("env_stdin_cmd --> Command not found: " .. cmd[2] .. ".", vim.log.levels.ERROR) + if ShellUtils.has_sh() then + -- Use sh on Unix-like systems + table.insert(cmd, "sh") + table.insert(cmd, "-c") + elseif ShellUtils.has_zsh() then + -- Use zsh on macOS + table.insert(cmd, "zsh") + table.insert(cmd, "-c") + elseif ShellUtils.has_powershell() then + -- Use PowerShell on Windows + table.insert(cmd, "powershell") + table.insert(cmd, "-Command") + else + Logger.error("env_stdin_cmd --> Shell not found: powershell, sh, or zsh.") return end - table.remove(splitted, 1) - for _, token in ipairs(splitted) do - table.insert(cmd, token) + + -- Extract environment variable name (first token) + local env_name, cmd_string = cmdstring:match("^(%S+)(.*)$") + if not env_name then + Logger.error("env_stdin_cmd --> Malformed metatag") + return end + + -- Append the command string to the command table + table.insert(cmd, cmd_string) + + -- Execute the command with the provided contents as stdin local res = vim.system(cmd, { stdin = contents, text = true }):wait().stdout - if res == nil then - vim.notify("env_stdin_cmd --> Command failed: " .. cmd[2] .. ".", vim.log.levels.ERROR) + + if not res then + Logger.error("env_stdin_cmd --> Command failed: " .. cmdstring .. ".") return else + -- Save the result to the environment variable DB.data.env[env_name] = res end end diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index caa19ce..4c1b5db 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs") local M = {} -M.VERSION = "3.0.1" +M.VERSION = "3.0.2" M.UI_ID = "kulala://ui" M.SCRATCHPAD_ID = "kulala://scratchpad" M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt"