Skip to content

Commit

Permalink
fix ext-processing + add jwt example (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
gorillamoe authored Aug 14, 2024
1 parent e121bcf commit b91974b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,29 @@ 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
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"
}
###
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}}"
}
```
33 changes: 33 additions & 0 deletions lua/kulala/cmd/shell_utils.lua
Original file line number Diff line number Diff line change
@@ -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
41 changes: 31 additions & 10 deletions lua/kulala/external_processing/init.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit b91974b

Please sign in to comment.