test: unit (#97)
* refactor(utils): remove unused function * fix(installer): actually pass what should be passed * docs: add explanation comment * test: add utils * refactor(utils): flatten the dir of files * remove .luarc
This commit is contained in:
parent
9aa0038125
commit
7af08c9780
8 changed files with 39 additions and 26 deletions
51
lua/gopher/_utils/gocmd.lua
Normal file
51
lua/gopher/_utils/gocmd.lua
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
local r = require "gopher._utils.runner"
|
||||
local c = require("gopher.config").commands
|
||||
local u = require "gopher._utils"
|
||||
local gocmd = {}
|
||||
|
||||
---@param args string[]
|
||||
---@return string[]
|
||||
local function if_get(args)
|
||||
for i, arg in ipairs(args) do
|
||||
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
|
||||
table.remove(args, i)
|
||||
table.insert(args, i, m)
|
||||
end
|
||||
return args
|
||||
end
|
||||
|
||||
---@param args unknown[]
|
||||
---@return string[]
|
||||
local function if_generate(args)
|
||||
if #args == 1 and args[1] == "%" then
|
||||
args[1] = vim.fn.expand "%"
|
||||
end
|
||||
return args
|
||||
end
|
||||
|
||||
---@param subcmd string
|
||||
---@param args string[]
|
||||
---@return string
|
||||
function gocmd.run(subcmd, args)
|
||||
if #args == 0 and subcmd ~= "generate" then
|
||||
error "please provide any arguments"
|
||||
end
|
||||
|
||||
if subcmd == "get" then
|
||||
args = if_get(args)
|
||||
end
|
||||
|
||||
if subcmd == "generate" then
|
||||
args = if_generate(args)
|
||||
end
|
||||
|
||||
local rs = r.sync { c.go, subcmd, unpack(args) }
|
||||
if rs.code ~= 0 then
|
||||
error("go " .. subcmd .. " failed: " .. rs.stderr)
|
||||
end
|
||||
|
||||
u.notify(c.go .. " " .. subcmd .. " ran successful")
|
||||
return rs.stdout
|
||||
end
|
||||
|
||||
return gocmd
|
||||
Loading…
Add table
Add a link
Reference in a new issue