From e7114be3b7d0716a40c599d4e82df437684f70ba Mon Sep 17 00:00:00 2001 From: Will Dean Date: Thu, 9 Jan 2025 00:22:26 +0100 Subject: [PATCH] generalize the args --- lua/octo/gh/init.lua | 78 +++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/lua/octo/gh/init.lua b/lua/octo/gh/init.lua index 8454918e..a2af3c35 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 +local 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 insert_args(args, opts) end ---Run a graphql query