diff --git a/lua/octo/gh/init.lua b/lua/octo/gh/init.lua index d34a3765..82348cbe 100644 --- a/lua/octo/gh/init.lua +++ b/lua/octo/gh/init.lua @@ -187,27 +187,31 @@ end ---@return table the updated args table M.insert_args = function(args, options) for key, value in pairs(options) do - local flag = create_flag(key) + if type(key) == "number" then + table.insert(args, value) + else + local flag = create_flag(key) - if type(value) == "table" then - for k, v in pairs(value) do - if type(v) == "table" then - for _, vv in ipairs(v) do + if type(value) == "table" then + for k, v in pairs(value) do + if type(v) == "table" then + for _, vv in ipairs(v) do + table.insert(args, flag) + table.insert(args, k .. "[]=" .. vv) + end + else table.insert(args, flag) - table.insert(args, k .. "[]=" .. vv) + table.insert(args, k .. "=" .. v) end - else + end + elseif type(value) == "boolean" then + if value then table.insert(args, flag) - table.insert(args, k .. "=" .. v) end - end - elseif type(value) == "boolean" then - if value then + else table.insert(args, flag) + table.insert(args, value) end - else - table.insert(args, flag) - table.insert(args, value) end end @@ -239,7 +243,7 @@ end ---Run a graphql query ---@param opts table the options for the graphql query ----@return table +---@return table|nil function M.graphql(opts) local run_opts = opts.opts or {} return M.run { @@ -252,4 +256,48 @@ function M.graphql(opts) } end +M.api = { + graphql = M.graphql, +} + +local create_subcommand = function(command) + local subcommand = {} + subcommand.command = command + + setmetatable(subcommand, { + __index = function(t, key) + return function(opts) + opts = opts or {} + + local run_opts = opts.opts or {} + + local args = { + t.command, + key, + } + + opts.opts = nil + args = M.insert_args(args, opts) + + return M.run { + args = args, + mode = run_opts.mode, + cb = run_opts.cb, + stream_cb = run_opts.stream_cb, + headers = run_opts.headers, + hostname = run_opts.hostname, + } + end + end, + }) + + return subcommand +end + +setmetatable(M, { + __index = function(_, key) + return create_subcommand(key) + end, +}) + return M diff --git a/lua/tests/plenary/gh_spec.lua b/lua/tests/plenary/gh_spec.lua index 387b38b2..064afba8 100644 --- a/lua/tests/plenary/gh_spec.lua +++ b/lua/tests/plenary/gh_spec.lua @@ -73,3 +73,34 @@ describe("insert_args:", function() eq(args, expected) end) end) + +describe("CLI commands", function() + it("gh. returns table", function() + local commands = { + "issue", + "pr", + "repo", + "gist", + "random", + } + + for _, command in ipairs(commands) do + local actual = gh[command] + eq(actual.command, command) + assert.is_table(actual) + end + end) + it("gh.. is a function", function() + local subcommands = { + "list", + "view", + "develop", + "create", + "random", + } + for _, subcommand in ipairs(subcommands) do + local actual = gh.issue[subcommand] + assert.is_function(actual) + end + end) +end)