Skip to content

Commit

Permalink
Merge pull request #12 from erichlf/CheckHealth
Browse files Browse the repository at this point in the history
Breaking: Rename lua directory to devcontainer-cli
  • Loading branch information
erichlf authored Apr 28, 2024
2 parents af3f409 + 6711505 commit f61b37f
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 75 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
---
on: [push, pull_request]
name: default

env:
IMAGE_NAME: nvim-devcontainer-cli

jobs:
docker:
unit-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Pre-build devcontainer image
uses: devcontainers/[email protected]
with:
imageName: ${{ env.IMAGE_NAME }}
imageName: ${{ env.IMAGE_NAME }}
cacheFrom: ${{ env.IMAGE_NAME }}
push: never

- name: Run tests inside the docker image
uses: devcontainers/[email protected]
with:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test:
--headless \
--noplugin \
-u ${TESTS_INIT} \
-c "PlenaryBustedFile tests/devcontainer_cli/folder_utils_spec.lua"
-c "PlenaryBustedFile tests/devcontainer_cli/unit_tests.lua"

test_all:
@nvim \
Expand Down
51 changes: 29 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Devcontainer CLI (Nvim Plugin)
# Devcontainer CLI (NVIM Plugin)

Develop your next Repo in a Devcontainer using Nvim thanks to the
Develop your next Repo in a Devcontainer using *nvim* thanks to the
[Devconatiner CLI](https://github.com/devcontainers/cli) and this plugin
![](doc/gifs/nvim_devcontainer_cli-description.gif)
![devcontainer-cli in action](doc/gifs/nvim_devcontainer_cli-description.gif)

As you can see in the GIF above,
[alacritty](https://github.com/alacritty/alacritty) is being used as a Terminal
Expand All @@ -13,6 +13,8 @@ quite simple, but should be very informative.

---

## Intro

First, what problem is this plugin trying to solve?

**Situation:**
Expand All @@ -32,8 +34,8 @@ plugins), but you don't want to have to run all the cumbersome commands.
There are multiple IDEs out there who give you the possibility to execute
themself inside the Docker container you are developing, fixing the problems
above, but there is nothing which works out-of-the-box for **nvim**. Recently,
Microsoft opened the command line tool,
([Devconatiner CLI](https://github.com/devcontainers/cli)), which allows developers
Microsoft opened the command line tool,
([Devconatiner CLI](https://github.com/devcontainers/cli)), which allows developers
to run devcontainers without VScode.

The current **nvim** plugin aims to take advantage of `devcontainer-cli` for
Expand All @@ -49,12 +51,13 @@ But, what is happening under the hood?
[devcontainer.json](.devcontainer/devcontainer.json) and initializing a
container based on such image.
2. Once the container is running, your dotfiles are installed in the docker
container together with a set of dependencies. To install any dependencies you need either the
dotfiles setup script will need to do that or you can use devcontainer features to install them.
A very nice devcontainer feature that can do this is
container together with a set of dependencies. To install any dependencies
you need either the dotfiles setup script will need to do that or you can
use devcontainer features to install them. A very nice devcontainer feature
that can do this is
[apt package](https://github.com/rocker-org/devcontainer-features/tree/main/src/apt-packages).
3. The last step is connecting inside the container via `devcontainer exec`
([here](https://github.com/erichlf/nvim-devcontainer-cli/blob/main/bin/connect_to_devcontainer.sh)).
([here](https://github.com/erichlf/devcontainer-cli.nvim/blob/main/bin/connect_to_devcontainer.sh)).

The main thing this plugin does is bringup your devcontainer and execute
commands via a convenient interface. It attempts to stay out of your way and
Expand All @@ -63,31 +66,33 @@ allows you to do things as you wish, but gives you the tools to do that easily.
**Inspiration:**

This plugin has been inspired by the work previously done by
[arnaupv](https://github.com/arnaupv/nvim-devcontainer-cli),
[arnaupv](https://github.com/arnaupv/devcontainer-cli.nvim),
[esensar](https://github.com/esensar/nvim-dev-container) and by
[jamestthompson3](https://github.com/jamestthompson3/nvim-remote-containers).
The main difference between this version and arnaupv is that it tries to not
make assumptions about how you work.

# Dependencies
## Dependencies

- [docker](https://docs.docker.com/get-docker/)
- [devcontainer-cli](https://github.com/devcontainers/cli#npm-install)

# 🔧 Installation
## 🔧 Installation

- [lazy.nvim](https://github.com/folke/lazy.nvim)

```lua
{
"erichlf/nvim-devcontainer-cli",
"erichlf/devcontainer-cli.nvim",
opts = {
-- whather to verify that the final devcontainer should be run
interactive = false,
-- search for the devcontainer directory closest to the root in the directory tree
-- search for the devcontainer directory closest to the root in the
-- directory tree
toplevel = true,
-- Remove existing container each time DevcontainerUp is executed
-- If set to True [default_value] it can take extra time as you force to start from scratch
-- If set to True [default_value] it can take extra time as you force to
-- start from scratch
remove_existing_container = true,
-- By default, if no extra config is added, following nvim_dotfiles are
-- installed: "https://github.com/LazyVim/starter"
Expand Down Expand Up @@ -131,7 +136,7 @@ make assumptions about how you work.

The default_config can be found [here](./lua/devcontainer_cli/config/init.lua).

# How to use?
## How to use?

There are 3 main commands: `:DevcontainerUp`, `:DevcontainerExec`, and `:DevcontainerConnect`.

Expand All @@ -155,7 +160,7 @@ There are 3 main commands: `:DevcontainerUp`, `:DevcontainerExec`, and `:Devcont
continue working in your current session and run commands in the
devcontainer via `DevcontainerExec`.

# Tests
## Tests

Tests are executed automatically on each PR using Github Actions.

Expand All @@ -173,12 +178,14 @@ use?** section. Once connected to the devcontainer, execute:
make test
```

# FEATUREs (in order of priority)
## FEATUREs (in order of priority)

1. [x] Capability to create and run a devcontainer using the [Devconatiner CLI](https://github.com/devcontainers/cli).
2. [x] Capability to attach in a running devcontainer.
3. [x] The floating window created during the devcontainer Up process (:DevcontainerUp<cr>) is closed when the process finishes successfully.
4. [x] [Give the possibility of defining custom dotfiles when setting up the devcontainer](https://github.com/erichlf/nvim-devcontainer-cli/issues/1)
5. [ ] Add unit tests using plenary.busted lua module.
6. [ ] The logs printed in the floating window when preparing the Devcontainer are saved and easy to access.
3. [x] The floating window created during the devcontainer Up process
(`:DevcontainerUp<cr>`) is closed when the process finishes successfully.
4. [x] [Give the possibility of defining custom dotfiles when setting up the devcontainer](https://github.com/erichlf/devcontainer-cli.nvim/issues/1)
5. [x] Add unit tests using plenary.busted lua module.
6. [ ] The logs printed in the floating window when preparing the Devcontainer
are saved and easy to access.
7. [ ] Convert bash scripts in lua code.
7 changes: 4 additions & 3 deletions doc/devcontainer_cli.txt → doc/devcontainer-cli.nvim.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
*devcontainer-cli.nvim.txt* For Neovim >= 0.9
================================================================================
*nvim-devcontainer-cli*
*devcontainer-cli.nvim*

nvim-devcontainer-cli is a nvim plugin which intends to use the devcontainer-cli
devcontainer-cli.nvim is a nvim plugin which intends to use the devcontainer-cli
developed by microsoft for creating your own local development environments when
developing docker containers.

Development is in progress, but the plugin can already be used.

To find out more:
https://github.com/erichlf/nvim-devcontainer-cli
https://github.com/erichlf/devcontainer-cli.nvim

:h DevcontainerUp

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local config = require("devcontainer_cli.config")
local devcontainer_utils = require("devcontainer_cli.devcontainer_utils")
local config = require("devcontainer-cli.config")
local devcontainer_utils = require("devcontainer-cli.devcontainer_utils")

local M = {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local config = require("devcontainer_cli.config")
local windows_utils = require("devcontainer_cli.windows_utils")
local folder_utils = require("devcontainer_cli.folder_utils")
local config = require("devcontainer-cli.config")
local windows_utils = require("devcontainer-cli.windows_utils")
local folder_utils = require("devcontainer-cli.folder_utils")

local M = {}

Expand Down Expand Up @@ -45,30 +45,38 @@ end

--- execute command
---@param cmd (string) the command to execute in the devcontainer terminal
local function exec_command(cmd)
function M.exec_command(cmd)
vim.fn.termopen(
cmd,
{
on_exit = on_exit,
on_stdout = function(_, _, _)
vim.api.nvim_win_call(
win,
function()
vim.cmd("normal! G")
end
)
if win ~= -1 then
vim.api.nvim_win_call(
win,
function()
vim.cmd("normal! G")
end
)
else
vim.notify("Executed " .. cmd)
end
end,
}
)
vim.api.nvim_set_current_buf(buffer)
if buffer ~= -1 then
vim.api.nvim_set_current_buf(buffer)
else
vim.notify("No buffer created, therefore no output from command will be visible", vim.log.levels.WARN)
end
end

-- create a new window and execute the given command
---@param cmd (string) the command to execute in the devcontainer terminal
local function spawn_and_execute(cmd)
prev_win = vim.api.nvim_get_current_win()
win, buffer = windows_utils.open_floating_window(on_detach)
exec_command(cmd)
M.exec_command(cmd)
end

-- build the initial part of a devcontainer command
Expand Down
File renamed without changes.
30 changes: 30 additions & 0 deletions lua/devcontainer-cli/health.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local M = {}

local start = vim.health.start or vim.health.report_start
local ok = vim.health.ok or vim.health.report_ok
local warn = vim.health.warn or vim.health.report_warn
local error = vim.health.error or vim.health.report_error

local function verify_binary(binary_name)
if vim.fn.executable(binary_name) ~= 1 then
error(("`%s` executable not found."):format(binary_name), ("Install %s"):format(binary_name))
else
ok(("`%s` executable found."):format(binary_name))
end
end
-- TODO: create a check for DevcontainerUp, this needs to be done after
-- creating the ability to stop a container
-- TODO: create a check for DevcontainerExec

function M.check()
start("devcontainer-cli")
local required_binaries = {
"docker",
"devcontainer",
}
for _, bin_name in ipairs(required_binaries) do
verify_binary(bin_name)
end
end

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

local devcontainer_cli = require("devcontainer_cli.devcontainer_cli")
local config = require("devcontainer_cli.config")
local devcontainer_cli = require("devcontainer-cli.devcontainer_cli")
local config = require("devcontainer-cli.config")
local configured = false

-- setup the devcontainer-cli plugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local config = require("devcontainer_cli.config")
local config = require("devcontainer-cli.config")

local M = {}

Expand Down
25 changes: 0 additions & 25 deletions tests/devcontainer_cli/folder_utils_spec.lua

This file was deleted.

62 changes: 62 additions & 0 deletions tests/devcontainer_cli/unit_tests.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
local folder_utils = require("devcontainer-cli.folder_utils")
local utils = require("devcontainer-cli.devcontainer_utils")

describe("folder_utils.get_root:", function()
it(
"check if first devcontainer directory when toplevel is false",
function()
-- dbg()
-- This test assumes that we are in the root folder of the project
local project_root = vim.fn.getcwd()
-- We change the current directory to a subfolder
vim.fn.chdir("lua/devcontainer-cli")
local devcontainer_cli_folder = vim.fn.getcwd()
-- First we check that the we properly changed the directory
assert(devcontainer_cli_folder == project_root .. "/lua/devcontainer-cli")
-- Verify that the project root is at the current level when toplevel is false
local root_folder = folder_utils.get_root(false)
-- From the subfolder, we check that the get_root function returns the folder where the git repo is located instead of the CWD
print("ROOT: " .. root_folder)
print("PROJECT_ROOT: " .. project_root)
assert(root_folder == project_root)
-- After running the test we come back to the initial location
vim.fn.chdir(project_root)
end
)
end)

describe("folder_utils.get_root:", function()
it(
"check if top most devcontainer directory when toplevel is true",
function()
-- dbg()
-- This test assumes that we are in the root folder of the project
local project_root = vim.fn.getcwd()
-- We change the current directory to a subfolder
vim.fn.chdir("lua/devcontainer-cli")
local devcontainer_cli_folder = vim.fn.getcwd()
-- First we check that the we properly changed the directory
assert(devcontainer_cli_folder == project_root .. "/lua/devcontainer-cli")

-- Verify that the project root is at HOME (location of top most devcontainer directory) when toplevel is true
project_root = os.getenv("HOME")
local root_folder = folder_utils.get_root(true)

-- From the subfolder, we check that the get_root function returns the folder where the git repo is located instead of the CWD
print("ROOT: " .. root_folder)
print("PROJECT_ROOT: " .. project_root)
assert(root_folder == project_root)

vim.fn.chdir(project_root)
end
)
end)

describe("devcontainer_utils.exec_command:", function()
it(
"check if a command can be executed",
function()
utils.exec_command("echo 'Hello World!'")
end
)
end)

0 comments on commit f61b37f

Please sign in to comment.