diff --git a/lua/octo/gh/init.lua b/lua/octo/gh/init.lua index ff180de8..d34a3765 100644 --- a/lua/octo/gh/init.lua +++ b/lua/octo/gh/init.lua @@ -173,16 +173,45 @@ function M.run(opts) end end ----Format fields for the graphql query ----@param fields table key value pairs for graphql query ----@return table -local format_fields = function(fields) - local formatted_fields = {} - for key, value in pairs(fields) do - table.insert(formatted_fields, "-F") - table.insert(formatted_fields, key .. "=" .. value) +local create_flag = function(key) + if #key == 1 then + return "-" .. key + else + return "--" .. key end - return formatted_fields +end + +---Insert the options into the args table +---@param args table the arguments table +---@param options table the options to insert +---@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(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 .. "=" .. v) + end + end + elseif type(value) == "boolean" then + if value then + table.insert(args, flag) + end + else + table.insert(args, flag) + table.insert(args, value) + end + end + + return args end ---Create the arguments for the graphql query @@ -195,28 +224,17 @@ end local create_graphql_args = function(query, fields, paginate, slurp, jq) local args = { "api", "graphql" } - fields = fields or {} - local formatted_fields = format_fields(fields) - for _, field in ipairs(formatted_fields) do - table.insert(args, field) - end - table.insert(args, "-f") - table.insert(args, string.format("query=%s", query)) - - if paginate then - table.insert(args, "--paginate") - end - - if slurp then - table.insert(args, "--slurp") - end - - if jq then - table.insert(args, "--jq") - table.insert(args, jq) - end + local opts = { + f = { + query = query, + }, + F = fields, + paginate = paginate, + slurp = slurp, + jq = jq, + } - return args + return M.insert_args(args, opts) end ---Run a graphql query diff --git a/lua/tests/plenary/gh_spec.lua b/lua/tests/plenary/gh_spec.lua new file mode 100644 index 00000000..387b38b2 --- /dev/null +++ b/lua/tests/plenary/gh_spec.lua @@ -0,0 +1,75 @@ +local gh = require "octo.gh" +local eq = assert.are.same + +describe("insert_args:", function() + it("true booleans show up as flags", function() + local args = {} + local opts = { + slurp = false, + paginate = true, + } + gh.insert_args(args, opts) + local expected = { + "--paginate", + } + eq(args, expected) + end) + it("single characters have single hyphen", function() + local args = {} + local opts = { + F = { + query = "query", + }, + } + gh.insert_args(args, opts) + local expected = { + "-F", + "query=query", + } + eq(args, expected) + end) + it("non-single changes have two hyphens", function() + local args = {} + local opts = { + jq = ".", + } + gh.insert_args(args, opts) + local expected = { + "--jq", + ".", + } + eq(args, expected) + end) + it("list of fields get brackets", function() + local args = {} + local opts = { + f = { + items = { "a", "b", "c" }, + }, + } + gh.insert_args(args, opts) + local expected = { + "-f", + "items[]=a", + "-f", + "items[]=b", + "-f", + "items[]=c", + } + eq(args, expected) + end) + it("integer values", function() + local args = {} + local opts = { + f = { + num_issues = 15, + }, + } + gh.insert_args(args, opts) + local expected = { + "-f", + "num_issues=15", + } + eq(args, expected) + end) +end)