Skip to content

Commit

Permalink
Add devcontainer down command
Browse files Browse the repository at this point in the history
  • Loading branch information
erichlf committed Jun 6, 2024
1 parent 55c5251 commit 0541168
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
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
41 changes: 33 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,30 @@ 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 command = "docker ps -q -a --filter label=devcontainer.config_file=" .. workspace
local pid = nil
vim.fn.jobstart(command, {
on_stdout = function(_, data)
pid = data
end
})

if pid == nil then
log.warn("Couldn't find devcontainer to kill")
return
end

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

0 comments on commit 0541168

Please sign in to comment.