Skip to content

Commit bf9f437

Browse files
authored
Merge pull request #21 from erichlf/down
Add DockerDown command
2 parents 55c5251 + ec3c466 commit bf9f437

File tree

6 files changed

+58
-18
lines changed

6 files changed

+58
-18
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ make assumptions about how you work.
9797
desc = "Bring up the DevContainer",
9898
},
9999
{
100-
"<leader>Dc",
100+
"<leader>Dd",
101101
":DevcontainerConnect<CR>",
102102
desc = "Connect to DevContainer",
103103
},
104+
{
105+
"<leader>Dc",
106+
":DevcontainerDown<CR>",
107+
desc = "Kill the current DevContainer",
108+
},
104109
{
105110
"<leader>De",
106111
":DevcontainerExec direction='vertical' size='40'<CR>",

lua/devcontainer-cli/devcontainer_cli.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ function M.connect()
7272
vim.cmd("wqa")
7373
end
7474

75+
-- kill the current running docker container associated with the current project
76+
function M.down()
77+
utils.down()
78+
end
79+
7580
return M

lua/devcontainer-cli/devcontainer_utils.lua

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
local config = require("devcontainer-cli.config")
2222
local folder_utils = require("devcontainer-cli.folder_utils")
2323
local terminal = require("devcontainer-cli.terminal")
24+
local log = require("devcontainer-cli.log")
2425

2526
local M = {}
2627

@@ -96,7 +97,7 @@ end
9697
local function _devcontainer_command(action)
9798
local devcontainer_root = folder_utils.get_root(config.toplevel)
9899
if devcontainer_root == nil then
99-
vim.notify("Unable to find devcontainer directory...", vim.log.levels.ERROR)
100+
log.error("unable to find devcontainer directory...")
100101
return nil
101102
end
102103

@@ -159,9 +160,7 @@ function M.bringup()
159160
},
160161
function(input)
161162
if (input == "q" or input == "Q") then
162-
vim.notify(
163-
"\nUser cancelled bringing up devcontainer"
164-
)
163+
log.info("\nUser cancelled bringing up devcontainer")
165164
else
166165
terminal.spawn(command)
167166
end
@@ -184,7 +183,7 @@ function M._exec_cmd(cmd, direction, size)
184183
end
185184

186185
command = command .. " " .. config.shell .. " -c '" .. cmd .. "'"
187-
vim.notify(command)
186+
log.info(command)
188187
terminal.spawn(command, direction, size)
189188
end
190189

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

@@ -206,7 +205,7 @@ function M.exec(cmd, direction, size)
206205
if input ~= nil then
207206
M._exec_cmd(input, direction, size)
208207
else
209-
vim.notify("No command received, ignoring.", vim.log.levels.WARN)
208+
log.warn("no command received, ignoring.")
210209
end
211210
end
212211
)
@@ -242,7 +241,7 @@ function M.create_connect_cmd()
242241
elseif vim.fn.executable("Terminal.app") == 1 then
243242
connect_command = { "Terminal.app" }
244243
else
245-
vim.notify("No supported terminal emulator found.", vim.log.levels.ERROR)
244+
log.error("no supported terminal emulator found.")
246245
end
247246

248247
table.insert(connect_command, dev_command)
@@ -259,4 +258,28 @@ function M.create_connect_cmd()
259258
return true
260259
end
261260

261+
-- issues command to down devcontainer
262+
function M.down()
263+
local workspace = folder_utils.get_root(config.toplevel)
264+
if workspace == nil then
265+
log.error("Couldn't determine project root")
266+
return
267+
end
268+
269+
local tag = workspace .. "/.devcontainer/devcontainer.json"
270+
local command = "docker ps -q -a --filter label=devcontainer.config_file=" .. tag
271+
log.debug("Attempting to get pid of devcontainer using command: " .. command)
272+
local result = vim.fn.systemlist(command)
273+
274+
if #result == 0 then
275+
log.warn("Couldn't find devcontainer to kill")
276+
return
277+
end
278+
279+
local pid = result[1]
280+
command = "docker kill " .. pid
281+
log.info("Killing docker container with pid: " .. pid)
282+
terminal.spawn(command)
283+
end
284+
262285
return M

lua/devcontainer-cli/init.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ function M.setup(opts)
6969
}
7070
)
7171

72+
vim.api.nvim_create_user_command(
73+
"DevcontainerDown",
74+
devcontainer_cli.down,
75+
{
76+
nargs = 0,
77+
desc = "Kill the current devcontainer.",
78+
}
79+
)
80+
7281
log.debug("Finished setting up devcontainer-cli")
7382
end
7483

lua/devcontainer-cli/log.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ local default_config = {
2020
-- Should write to a file
2121
use_file = true,
2222

23-
-- Any messages above this level will be logged.
24-
level = "debug",
23+
-- Any messages above this level will be logged to log file.
24+
log_level = "debug",
25+
-- Any messages above this level will be logged to console.
26+
console_level = "info",
2527

2628
-- Level configuration
2729
modes = {
@@ -85,18 +87,14 @@ log.new = function(config, standalone)
8587

8688

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

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

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

122120
-- Output to log file
123-
if config.use_file then
121+
if config.use_file and level < levels[config.log_level] then
124122
local fp = io.open(outfile, "a")
125123
local str = string.format("[%-6s%s] %s: %s\n",
126124
nameupper, os.date(), lineinfo, msg)

lua/devcontainer-cli/terminal.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ local _on_fail = function(exit_code)
3232
end
3333

3434
local _on_success = function()
35-
log.INFO("Devcontainer process succeeded!")
35+
log.info("Devcontainer process succeeded!")
3636
end
3737

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

0 commit comments

Comments
 (0)