Add option to choose random server port (#65)

Updates the plugin to automatically choose a random port when started when no port is provided. This will allow multiple Neovim instances to open at the same time in different projects.
This commit is contained in:
johnybx
2023-10-21 17:39:37 +02:00
committed by GitHub
parent 2100bf2e43
commit c8a0267ba6
5 changed files with 119 additions and 66 deletions

View File

@@ -2,45 +2,75 @@
-- the Golang server. The Go server is responsible for making API calls
-- to Gitlab and returning the data
local state = require("gitlab.state")
local u = require("gitlab.utils")
local M = {}
local u = require("gitlab.utils")
local M = {}
-- Starts the Go server and call the callback provided
M.start = function(callback)
M.start = function(callback)
local empty_port = "''"
local port = state.settings.port or empty_port
local parsed_port = nil
local callback_called = false
local command = state.settings.bin
.. " "
.. state.settings.project_id
.. " "
.. state.settings.gitlab_url
.. " "
.. state.settings.port
.. " "
.. state.settings.auth_token
.. " "
.. state.settings.log_path
.. " "
.. state.settings.project_id
.. " "
.. state.settings.gitlab_url
.. " "
.. port
.. " "
.. state.settings.auth_token
.. " "
.. state.settings.log_path
vim.fn.jobstart(command, {
on_stdout = function(job_id)
if job_id <= 0 then
vim.notify("Could not start gitlab.nvim binary", vim.log.levels.ERROR)
else
local job_id = vim.fn.jobstart(command, {
on_stdout = function(_, data)
-- if port was not provided then we need to parse it from output of server
if parsed_port == nil then
for _, line in ipairs(data) do
port = line:match("Server started on port:%s+(%d+)")
if port ~= nil then
parsed_port = port
state.settings.port = port
break
end
end
end
-- This assumes that first output of server will be parsable and port will be correctly set.
-- Make sure that this actually check if port was correctly parsed based on server output
-- because server outputs port only if it started successfully.
if parsed_port ~= nil and not callback_called then
callback()
callback_called = true
elseif not callback_called then
vim.notify("Failed to parse server port", vim.log.levels.ERROR)
end
end,
on_stderr = function(_, errors)
local err_msg = ''
local err_msg = ""
for _, err in ipairs(errors) do
if err ~= "" and err ~= nil then
err_msg = err_msg .. err .. "\n"
end
end
if err_msg ~= '' then vim.notify(err_msg, vim.log.levels.ERROR) end
end
if err_msg ~= "" then
vim.notify(err_msg, vim.log.levels.ERROR)
end
end,
on_exit = function(job_id, exit_code, ...)
vim.notify(
"Golang gitlab server exited: job_id: " .. job_id .. ", exit_code: " .. exit_code,
vim.log.levels.ERROR
)
end,
})
if job_id <= 0 then
vim.notify("Could not start gitlab.nvim binary", vim.log.levels.ERROR)
end
end
-- Builds the Go binary
M.build = function(override)
local file_path = u.current_file_path()
@@ -50,12 +80,13 @@ M.build = function(override)
if not override then
local binary_exists = vim.loop.fs_stat(state.settings.bin)
if binary_exists ~= nil then return end
if binary_exists ~= nil then
return
end
end
local cmd = u.is_windows() and
'cd %s\\cmd && go build -o bin.exe && move bin.exe ..\\' or
'cd %s/cmd && go build -o bin && mv bin ../bin'
local cmd = u.is_windows() and "cd %s\\cmd && go build -o bin.exe && move bin.exe ..\\"
or "cd %s/cmd && go build -o bin && mv bin ../bin"
local command = string.format(cmd, state.settings.bin_path)
local null = u.is_windows() and " >NUL" or " > /dev/null"