Skip to content

Fix log level and error handling #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 5, 2025
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
30 changes: 24 additions & 6 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
on: [push, pull_request]
on: [pull_request]
name: default
env:
IMAGE_NAME: nvim-devcontainer-cli
jobs:
unit-tests:
runs-on: ubuntu-latest
strategy:
matrix:
neovim-version: [
# "v0.9.0",
# "v0.9.1",
# "v0.9.2",
# "v0.9.4",
# "v0.9.5",
# "v0.10.0",
# "v0.10.1",
# "v0.10.2",
# "v0.10.3",
"stable",
]
fail-fast: false

name: NEOVIM ${{ matrix.neovim-version }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Pre-build devcontainer image
uses: actions/checkout@v4
- name: Pre-build devcontainer image for NEOVIM ${{ matrix.neovim-version }}
uses: devcontainers/[email protected]
with:
imageName: ${{ env.IMAGE_NAME }}
cacheFrom: ${{ env.IMAGE_NAME }}
imageName: ${{ env.IMAGE_NAME }}-${{ matrix.neovim-version }}
cacheFrom: ${{ env.IMAGE_NAME }}-${{ matrix.neovim-version }}
push: never
options: --build-args NEOVIM_VERSION=${{ matrix.neovim-version }}
- name: Run tests inside the docker image
uses: devcontainers/[email protected]
with:
cacheFrom: ${{ env.IMAGE_NAME }}
cacheFrom: ${{ env.IMAGE_NAME }}-${{ matrix.neovim-version }}
push: never
runCmd: make test
75 changes: 44 additions & 31 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright (c) 2024 Erich L Foster
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -18,8 +18,48 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

ARG NEOVIM_VERSION="stable"
FROM ubuntu:22.04 as builder

ARG NEOVIM_VERSION

# Install dependencies needed for building devcontainers/cli and developing in neovim
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
apt-utils \
build-essential \
curl \
wget \
nodejs \
npm \
lua5.1 \
luajit \
luarocks \
git \
# apt clean-up
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /tmp

# Install NEOVIM
RUN \
if [ "$NEOVIM_VERSION" = "stable" ]; then \
curl -fLO https://github.com/neovim/neovim/releases/download/${NEOVIM_VERSION}/nvim-linux-x86_64.tar.gz; \
else \
curl -fLO https://github.com/neovim/neovim/releases/download/${NEOVIM_VERSION}/nvim-linux64.tar.gz; \
fi \
&& rm -rf /opt/nvim \
&& tar -C /opt -xzf nvim-*.tar.gz \
&& ln -s /opt/nvim-*/bin/nvim /usr/local/bin/nvim \
&& chmod u+x /usr/local/bin/nvim

WORKDIR /app

# Installing the devcontainers CLI and Lua Dependencies for testing LUA projects
RUN npm install -g @devcontainers/[email protected] \
&& luarocks install busted

ENV USER_NAME=my-app
ARG GROUP_NAME=$USER_NAME
ARG USER_ID=1000
Expand All @@ -31,37 +71,10 @@ RUN groupadd --gid $GROUP_ID $GROUP_NAME && \
&& apt-get update \
&& apt-get install -y --no-install-recommends sudo \
&& echo $USER_NAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USER_NAME \
&& chmod 0440 /etc/sudoers.d/$USER_NAME
&& chmod 0440 /etc/sudoers.d/$USER_NAME

# Switch to user
USER $USER_NAME

# Install dependencies needed for building devcontainers/cli and developing in neovim
RUN sudo apt-get update && \
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
apt-utils \
build-essential \
curl \
wget \
nodejs \
npm \
lua5.1 \
luajit \
luarocks \
git \
# apt clean-up
&& sudo apt-get autoremove -y \
&& sudo rm -rf /var/lib/apt/lists/*

ENV NPM_CONFIG_PREFIX=/home/$USER_NAME/.npm-global

WORKDIR /app

# Installing the devcontainers CLI
RUN npm install -g @devcontainers/[email protected]

# Installing Lua Dependencies for testing LUA projects
RUN sudo luarocks install busted

# this will prevent the .local directory from being owned by root on bind mount
RUN mkdir -p /home/$USER_NAME/.local/share/nvim/lazy
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ make assumptions about how you work.

## Dependencies

- NeoVim 0.9.0+
- [docker](https://docs.docker.com/get-docker/)
- [devcontainer-cli](https://github.com/devcontainers/cli#npm-install)
- [toggleterm](https://github.com/akinsho/toggleterm.nvim)
Expand Down Expand Up @@ -148,12 +149,12 @@ make assumptions about how you work.
dotfiles_branch = "devcontainer-cli", -- branch to clone from dotfiles_repository`
dotfiles_targetPath = "~/dotfiles", -- location to install dotfiles
-- script to run after dotfiles are cloned
dotfiles_intallCommand = "install.sh",
dotfiles_intallCommand = "install.sh",
shell = "bash", -- shell to use when executing commands
-- The particular binary to use for connecting to in the devcontainer
-- Most likely this should remain nvim
nvim_binary = "nvim",
-- Set the logging level for console (notifications) and file logging.
-- Set the logging level for console (notifications) and file logging.
-- The available levels are trace, debug, info, warn, error, or fatal.
-- Set the log level for file logging
log_level = "debug",
Expand Down
37 changes: 19 additions & 18 deletions lua/devcontainer-cli/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local global_config = require("devcontainer-cli.config")
-- User configuration section
local default_config = {
-- Name of the plugin. Prepended to log messages
plugin = 'decontainer-cli',
plugin = "devcontainer-cli",

-- Should print the output to neovim while running
use_console = true,
Expand All @@ -29,12 +29,12 @@ local default_config = {

-- Level configuration
modes = {
{ name = "trace", hl = "Comment", },
{ name = "debug", hl = "Comment", },
{ name = "info", hl = "None", },
{ name = "warn", hl = "WarningMsg", },
{ name = "error", hl = "ErrorMsg", },
{ name = "fatal", hl = "ErrorMsg", },
{ name = "trace", hl = "Comment" },
{ name = "debug", hl = "Comment" },
{ name = "info", hl = "None" },
{ name = "warn", hl = "WarningMsg" },
{ name = "error", hl = "ErrorMsg" },
{ name = "fatal", hl = "ErrorMsg" },
},

-- Can limit the number of decimals displayed for floats
Expand All @@ -49,7 +49,7 @@ local unpack = unpack or table.unpack
log.new = function(config, standalone)
config = vim.tbl_deep_extend("force", default_config, config)

local outfile = string.format('%s/%s.log', vim.fn.stdpath("cache"), config.plugin)
local outfile = string.format("%s/%s.log", vim.fn.stdpath("cache"), config.plugin)

local obj
if standalone then
Expand All @@ -66,12 +66,12 @@ log.new = function(config, standalone)
local round = function(x, increment)
increment = increment or 1
x = x / increment
return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
return (x > 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)) * increment
end

local make_string = function(...)
local t = {}
for i = 1, select('#', ...) do
for i = 1, select("#", ...) do
local x = select(i, ...)

if type(x) == "number" and config.float_precision then
Expand All @@ -87,7 +87,6 @@ log.new = function(config, standalone)
return table.concat(t, " ")
end


local log_at_level = function(level, level_config, message_maker, ...)
local nameupper = level_config.name:upper()

Expand Down Expand Up @@ -120,13 +119,15 @@ log.new = function(config, standalone)
end

-- Output to log file
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)
if fp ~= nil then
if config.use_file and level < levels[config.log_level] then
local ok, err = pcall(function()
local fp = assert(io.open(outfile, "a"))
local str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg)
fp:write(str)
fp:close()
end)
if not ok then
vim.notify(string.format("Failed to write to log file: %s", err), vim.log.levels.ERROR)
end
end
end
Expand All @@ -136,9 +137,9 @@ log.new = function(config, standalone)
return log_at_level(i, x, make_string, ...)
end

obj[("fmt_%s" ):format(x.name)] = function()
obj[("fmt_%s"):format(x.name)] = function()
return log_at_level(i, x, function(...)
local passed = {...}
local passed = { ... }
local fmt = table.remove(passed, 1)
local inspected = {}
for _, v in ipairs(passed) do
Expand Down
14 changes: 8 additions & 6 deletions tests/minimal_init.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
-- Copyright (c) 2024 Erich L Foster
--
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
vim.opt.runtimepath:prepend(vim.fn.stdpath("data") .. "/site")
vim.opt.packpath = vim.opt.runtimepath:get()

-- Your existing plenary setup
local plenary_dir = os.getenv("PLENARY_DIR") or "/tmp/plenary.nvim"
local is_not_a_directory = vim.fn.isdirectory(plenary_dir) == 0
if is_not_a_directory then
vim.fn.system({ "git", "clone", "https://github.com/nvim-lua/plenary.nvim", plenary_dir })
if vim.fn.isdirectory(plenary_dir) == 0 then
vim.fn.system({ "git", "clone", "--depth=1", "https://github.com/nvim-lua/plenary.nvim", plenary_dir })
end

vim.opt.rtp:append(".")
Expand Down
Loading