From 3416897f4bc972afda269e7b9f60c9e68a47e582 Mon Sep 17 00:00:00 2001 From: Erich L Foster Date: Thu, 6 Jun 2024 19:13:26 +0200 Subject: [PATCH 1/2] Add devcontainer down command --- README.md | 7 +++- lua/devcontainer-cli/devcontainer_cli.lua | 5 +++ lua/devcontainer-cli/devcontainer_utils.lua | 39 ++++++++++++++++----- lua/devcontainer-cli/init.lua | 9 +++++ lua/devcontainer-cli/terminal.lua | 2 +- 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7b7ce4a..aab141a 100644 --- a/README.md +++ b/README.md @@ -97,10 +97,15 @@ make assumptions about how you work. desc = "Bring up the DevContainer", }, { - "Dc", + "Dd", ":DevcontainerConnect", desc = "Connect to DevContainer", }, + { + "Dc", + ":DevcontainerDown", + desc = "Kill the current DevContainer", + }, { "De", ":DevcontainerExec direction='vertical' size='40'", diff --git a/lua/devcontainer-cli/devcontainer_cli.lua b/lua/devcontainer-cli/devcontainer_cli.lua index e45955b..c93c130 100644 --- a/lua/devcontainer-cli/devcontainer_cli.lua +++ b/lua/devcontainer-cli/devcontainer_cli.lua @@ -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 diff --git a/lua/devcontainer-cli/devcontainer_utils.lua b/lua/devcontainer-cli/devcontainer_utils.lua index 916f9c1..7416b83 100644 --- a/lua/devcontainer-cli/devcontainer_utils.lua +++ b/lua/devcontainer-cli/devcontainer_utils.lua @@ -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 = {} @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 ) @@ -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) @@ -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 diff --git a/lua/devcontainer-cli/init.lua b/lua/devcontainer-cli/init.lua index 3b52289..2ae207a 100644 --- a/lua/devcontainer-cli/init.lua +++ b/lua/devcontainer-cli/init.lua @@ -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 diff --git a/lua/devcontainer-cli/terminal.lua b/lua/devcontainer-cli/terminal.lua index e0253bf..681950b 100644 --- a/lua/devcontainer-cli/terminal.lua +++ b/lua/devcontainer-cli/terminal.lua @@ -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 From ec3c466549d7c456b422bf9a1e66ffd4454930b3 Mon Sep 17 00:00:00 2001 From: Erich L Foster Date: Thu, 6 Jun 2024 20:12:12 +0200 Subject: [PATCH 2/2] Add log level for console and file --- lua/devcontainer-cli/log.lua | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lua/devcontainer-cli/log.lua b/lua/devcontainer-cli/log.lua index fdda1e9..9b7d221 100644 --- a/lua/devcontainer-cli/log.lua +++ b/lua/devcontainer-cli/log.lua @@ -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 = { @@ -85,10 +87,6 @@ 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(...) @@ -96,7 +94,7 @@ log.new = function(config, standalone) 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, @@ -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)