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: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ make assumptions about how you work.
desc = "Bring up the DevContainer",
},
{
"<leader>Dc",
"<leader>Dd",
":DevcontainerConnect<CR>",
desc = "Connect to DevContainer",
},
{
"<leader>Dc",
":DevcontainerDown<CR>",
desc = "Kill the current DevContainer",
},
{
"<leader>De",
":DevcontainerExec direction='vertical' size='40'<CR>",
Expand Down
5 changes: 5 additions & 0 deletions lua/devcontainer-cli/devcontainer_cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ function M.connect()
vim.cmd("wqa")
end

-- kill the current running docker container associated with the current project
function M.down()
utils.down()
end

return M
39 changes: 31 additions & 8 deletions lua/devcontainer-cli/devcontainer_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
local config = require("devcontainer-cli.config")
local folder_utils = require("devcontainer-cli.folder_utils")
local terminal = require("devcontainer-cli.terminal")
local log = require("devcontainer-cli.log")

local M = {}

Expand Down Expand Up @@ -96,7 +97,7 @@ end
local function _devcontainer_command(action)
local devcontainer_root = folder_utils.get_root(config.toplevel)
if devcontainer_root == nil then
vim.notify("Unable to find devcontainer directory...", vim.log.levels.ERROR)
log.error("unable to find devcontainer directory...")
return nil
end

Expand Down Expand Up @@ -159,9 +160,7 @@ function M.bringup()
},
function(input)
if (input == "q" or input == "Q") then
vim.notify(
"\nUser cancelled bringing up devcontainer"
)
log.info("\nUser cancelled bringing up devcontainer")
else
terminal.spawn(command)
end
Expand All @@ -184,7 +183,7 @@ function M._exec_cmd(cmd, direction, size)
end

command = command .. " " .. config.shell .. " -c '" .. cmd .. "'"
vim.notify(command)
log.info(command)
terminal.spawn(command, direction, size)
end

Expand All @@ -195,7 +194,7 @@ end
---@param size (number|nil) size of the window to create
function M.exec(cmd, direction, size)
if terminal.is_open() then
vim.notify("There is already a devcontainer process running.", vim.log.levels.WARN)
log.warn("there is already a devcontainer process running.")
return
end

Expand All @@ -206,7 +205,7 @@ function M.exec(cmd, direction, size)
if input ~= nil then
M._exec_cmd(input, direction, size)
else
vim.notify("No command received, ignoring.", vim.log.levels.WARN)
log.warn("no command received, ignoring.")
end
end
)
Expand Down Expand Up @@ -242,7 +241,7 @@ function M.create_connect_cmd()
elseif vim.fn.executable("Terminal.app") == 1 then
connect_command = { "Terminal.app" }
else
vim.notify("No supported terminal emulator found.", vim.log.levels.ERROR)
log.error("no supported terminal emulator found.")
end

table.insert(connect_command, dev_command)
Expand All @@ -259,4 +258,28 @@ function M.create_connect_cmd()
return true
end

-- issues command to down devcontainer
function M.down()
local workspace = folder_utils.get_root(config.toplevel)
if workspace == nil then
log.error("Couldn't determine project root")
return
end

local tag = workspace .. "/.devcontainer/devcontainer.json"
local command = "docker ps -q -a --filter label=devcontainer.config_file=" .. tag
log.debug("Attempting to get pid of devcontainer using command: " .. command)
local result = vim.fn.systemlist(command)

if #result == 0 then
log.warn("Couldn't find devcontainer to kill")
return
end

local pid = result[1]
command = "docker kill " .. pid
log.info("Killing docker container with pid: " .. pid)
terminal.spawn(command)
end

return M
9 changes: 9 additions & 0 deletions lua/devcontainer-cli/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ function M.setup(opts)
}
)

vim.api.nvim_create_user_command(
"DevcontainerDown",
devcontainer_cli.down,
{
nargs = 0,
desc = "Kill the current devcontainer.",
}
)

log.debug("Finished setting up devcontainer-cli")
end

Expand Down
14 changes: 6 additions & 8 deletions lua/devcontainer-cli/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ local default_config = {
-- Should write to a file
use_file = true,

-- Any messages above this level will be logged.
level = "debug",
-- Any messages above this level will be logged to log file.
log_level = "debug",
-- Any messages above this level will be logged to console.
console_level = "info",

-- Level configuration
modes = {
Expand Down Expand Up @@ -85,18 +87,14 @@ log.new = function(config, standalone)


local log_at_level = function(level, level_config, message_maker, ...)
-- Return early if we're below the config.level
if level < levels[config.level] then
return
end
local nameupper = level_config.name:upper()

local msg = message_maker(...)
local info = debug.getinfo(2, "Sl")
local lineinfo = info.short_src .. ":" .. info.currentline

-- Output to console
if config.use_console then
if config.use_console and level < levels[config.console_level] then
local console_string = string.format(
"[%-6s%s] %s: %s",
nameupper,
Expand All @@ -120,7 +118,7 @@ log.new = function(config, standalone)
end

-- Output to log file
if config.use_file then
if config.use_file and level < levels[config.log_level] then
local fp = io.open(outfile, "a")
local str = string.format("[%-6s%s] %s: %s\n",
nameupper, os.date(), lineinfo, msg)
Expand Down
2 changes: 1 addition & 1 deletion lua/devcontainer-cli/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local _on_fail = function(exit_code)
end

local _on_success = function()
log.INFO("Devcontainer process succeeded!")
log.info("Devcontainer process succeeded!")
end

-- on_exit callback function to delete the open buffer when devcontainer exits
Expand Down