Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lua/http_client/commands/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local M = {}

M.request = require('http_client.commands.request')
M.select_env = require('http_client.commands.select_env')

return M

Original file line number Diff line number Diff line change
@@ -1,85 +1,10 @@
local M = {}

local vvv = require('http_client.utils.verbose')
local parser = require('http_client.core.parser')
local environment = require('http_client.core.environment')
local file_utils = require('http_client.utils.file_utils')
local http_client = require('http_client.core.http_client')
local parser = require('http_client.core.parser')
local dry_run = require('http_client.ui.dry_run')
local vvv = require('http_client.utils.verbose')

local config = {}

M.setup = function(cfg)
config = cfg
return M
end

M.select_env_file = function()
local files = file_utils.find_files('*.env.json')
local default_file = config.default_env_file or '.env.json'
local default_index = nil

-- Find the index of the default file
for i, file in ipairs(files) do
if file:match(default_file .. "$") then
default_index = i
break
end
end

vim.ui.select(files, {
prompt = 'Select environment file:',
default = default_index
}, function(choice)
if choice then
environment.set_env_file(choice)
print('\n\nEnvironment file set to: ' .. choice)
-- Automatically select environment after file selection
M.select_env()
end
end)
end

M.select_env = function()
if not environment.get_current_env_file() then
print('\nNo environment file selected. Please select an environment file first.')
return
end

local env_data = file_utils.read_json_file(environment.get_current_env_file())
if not env_data then
print('\nFailed to read environment file')
return
end

-- Set *default environment first
local success = environment.set_env('*default')
if success then
print('\nEnvironment set to: *default')
else
print('\nFailed to set default environment')
return
end

local env_names = { '*default' }
for name, _ in pairs(env_data) do
if name ~= '*default' then
table.insert(env_names, name)
end
end

vim.ui.select(env_names, {
prompt = 'Select environment (current: *default):',
}, function(choice)
if choice and choice ~= '*default' then
local success = environment.set_env(choice)
if success then
print('\nEnvironment set to: ' .. choice)
else
print('\nFailed to set environment: ' .. choice)
end
end
end)
end

M.run_request = function()
local verbose = vvv.get_verbose_mode()
Expand All @@ -102,7 +27,7 @@ M.run_request = function()
print(
'Environment variables are needed but not set. Please select an environment file or set properties via response handler.'
)
return
return
end

request = parser.replace_placeholders(request, env)
Expand Down Expand Up @@ -135,16 +60,6 @@ M.stop_request = function()
http_client.clear_current_request()
end

M.toggle_verbose_mode = function()
local current_state = vvv.get_verbose_mode()
vvv.set_verbose_mode(not current_state)
print(string.format("HTTP Client verbose mode %s", not current_state and "enabled" or "disabled"))
end

M.dry_run = function()
dry_run.display_dry_run(M)
end

M.run_all = function()
local verbose = vvv.get_verbose_mode()
vvv.set_verbose_mode(verbose)
Expand Down
76 changes: 76 additions & 0 deletions lua/http_client/commands/select_env.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
local M = {}

local environment = require('http_client.core.environment')
local file_utils = require('http_client.utils.file_utils')

M.select_env_file = function(config)
local files = file_utils.find_files('*.env.json')
local default_file = config.default_env_file or '.env.json'
local default_index = nil

-- Find the index of the default file
for i, file in ipairs(files) do
if file:match(default_file .. "$") then
default_index = i
break
end
end

vim.ui.select(files, {
prompt = 'Select environment file:',
default = default_index
}, function(choice)
if choice then
environment.set_env_file(choice)
print('\n\nEnvironment file set to: ' .. choice)
-- Automatically select environment after file selection
M.select_env()
end
end)
end

M.select_env = function()
if not environment.get_current_env_file() then
print('\nNo environment file selected. Please select an environment file first.')
return
end

local env_data = file_utils.read_json_file(environment.get_current_env_file())
if not env_data then
print('\nFailed to read environment file')
return
end

-- Set *default environment first
local success = environment.set_env('*default')
if success then
print('\nEnvironment set to: *default')
else
print('\nFailed to set default environment')
return
end

local env_names = { '*default' }
for name, _ in pairs(env_data) do
if name ~= '*default' then
table.insert(env_names, name)
end
end

vim.ui.select(env_names, {
prompt = 'Select environment (current: *default):',
}, function(choice)
if choice and choice ~= '*default' then
local success = environment.set_env(choice)
if success then
print('\nEnvironment set to: ' .. choice)
else
print('\nFailed to set environment: ' .. choice)
end
end
end)
end


return M

34 changes: 18 additions & 16 deletions lua/http_client/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,37 @@ function M.setup(opts)

-- Set up commands
vim.api.nvim_create_user_command('HttpEnvFile', function()
M.commands.select_env_file()
M.commands.select_env.select_env_file(M.config.get())
end, {
desc = 'Select an environment file for HTTP requests.'
})

vim.api.nvim_create_user_command('HttpEnv', function()
M.commands.select_env()
M.commands.select_env.select_env()
end, {
desc = 'Select an environment for HTTP request (requires one argument).',
})

vim.api.nvim_create_user_command('HttpRun', function()
M.commands.run_request()
M.commands.request.run_request()
end, {
desc = 'Run the HTTP request under cursor. Use ! to enable verbose mode.',
})

vim.api.nvim_create_user_command('HttpRunAll', function()
M.commands.request.run_all()
end, {
desc = 'Run all HTTP requests in the current file.'
})

vim.api.nvim_create_user_command('HttpStop', function()
M.commands.request.stop_request()
end, {
desc = 'Stop the currently running HTTP request.'
})



vim.api.nvim_create_user_command('HttpVerbose', function()
local current_state = M.v.get_verbose_mode()
M.v.set_verbose_mode(not current_state)
Expand All @@ -72,24 +86,12 @@ function M.setup(opts)
desc = 'Toggle verbose mode for HTTP request.'
})

vim.api.nvim_create_user_command('HttpStop', function()
M.commands.stop_request()
end, {
desc = 'Stop the currently running HTTP request.'
})

vim.api.nvim_create_user_command('HttpDryRun', function()
vim.api.nvim_create_user_command('HttpDryRun', function()
M.dry_run.display_dry_run(M)
end, {
desc = 'Perform a dry run of the HTTP request without sending it.'
})

vim.api.nvim_create_user_command('HttpRunAll', function()
M.commands.run_all()
end, {
desc = 'Run all HTTP requests in the current file.'
})



setup_docs()
Expand Down