From 105bcdbab1d48d5cc1668e7a84663db80e168a3f Mon Sep 17 00:00:00 2001 From: phanium Date: Wed, 19 Jun 2024 19:05:45 +0800 Subject: [PATCH] fix: log on api limited --- lua/fzf-lua-overlay/providers/gitignore.lua | 14 +++++++++----- lua/fzf-lua-overlay/providers/license.lua | 12 ++++++++---- lua/fzf-lua-overlay/util.lua | 12 +++++++++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lua/fzf-lua-overlay/providers/gitignore.lua b/lua/fzf-lua-overlay/providers/gitignore.lua index f58858e..27985ea 100644 --- a/lua/fzf-lua-overlay/providers/gitignore.lua +++ b/lua/fzf-lua-overlay/providers/gitignore.lua @@ -1,10 +1,11 @@ ---@type FzfLuaOverlaySpec local M = {} -local url = 'https://api.github.com/gitignore/templates' +local base_url = 'https://api.github.com/gitignore/templates' local cache_dir = require('fzf-lua-overlay.config').opts.cache_dir local cache_path = vim.fs.joinpath(cache_dir, 'gitignore.json') +local u = require('fzf-lua-overlay.util') M.name = 'fzf_exec' @@ -14,16 +15,18 @@ M.opts = { ['default'] = function(selected) local util = require('fzf-lua-overlay.util') local gitroot = util.gitroot() - if not gitroot then vim.notify('not in a git repository') end + if not gitroot then u.warn('not in a git repository') end local path = vim.fs.joinpath(gitroot, '.gitignore') vim.print(path) if vim.uv.fs_stat(path) then local confirm = vim.fn.confirm('Override?', '&Yes\n&No') if confirm ~= 1 then return end end - local template_url = ('%s/%s'):format(url, selected[1]) - local content = vim.fn.system({ 'curl', '-s', template_url }) + local template_url = ('%s/%s'):format(base_url, selected[1]) + local content = u.gh_curl(template_url) + if not content then return u.warn('api limited') end content = vim.json.decode(content).source + vim.print(content) util.write_file(path, content) vim.cmd.e(path) end, @@ -35,7 +38,8 @@ M.fzf_exec_arg = function(fzf_cb) local json if not vim.uv.fs_stat(cache_path) then - local json_str = vim.fn.system({ 'curl', '-s', url }) + local json_str = u.gh_curl(base_url) + if not json_str then return u.warn('api limited') end util.write_file(cache_path, json_str) json = vim.json.decode(json_str) end diff --git a/lua/fzf-lua-overlay/providers/license.lua b/lua/fzf-lua-overlay/providers/license.lua index 9a17f64..3a1c03e 100644 --- a/lua/fzf-lua-overlay/providers/license.lua +++ b/lua/fzf-lua-overlay/providers/license.lua @@ -1,8 +1,10 @@ -local url = 'https://api.github.com/licenses' +local base_url = 'https://api.github.com/licenses' local cache_dir = require('fzf-lua-overlay.config').opts.cache_dir local cache_path = vim.fs.joinpath(cache_dir, 'license.json') +local u = require('fzf-lua-overlay.util') + ---@type FzfLuaOverlaySpec local M = {} @@ -21,8 +23,9 @@ M.opts = { local confirm = vim.fn.confirm('Override?', '&Yes\n&No') if confirm ~= 1 then return end end - local template_url = ('%s/%s'):format(url, selected[1]) - local content = vim.fn.system { 'curl', '-s', template_url } + local url = ('%s/%s'):format(base_url, selected[1]) + local content = u.gh_curl(url) + if not content then return u.warn('api limited') end content = vim.json.decode(content).body util.write_file(path, content) vim.cmd.e(path) @@ -35,7 +38,8 @@ M.fzf_exec_arg = function(fzf_cb) local json if not vim.uv.fs_stat(cache_path) then - local json_str = vim.fn.system { 'curl', '-s', url } + local json_str = u.gh_curl(base_url) + if not json_str then return u.warn('api limited') end util.write_file(cache_path, json_str) json = vim.json.decode(json_str) end diff --git a/lua/fzf-lua-overlay/util.lua b/lua/fzf-lua-overlay/util.lua index b73fae8..02a6e49 100644 --- a/lua/fzf-lua-overlay/util.lua +++ b/lua/fzf-lua-overlay/util.lua @@ -113,7 +113,17 @@ end)() u.warn = function(msg, ...) msg = string.format(msg, ...) - vim.notify('plugin not installed\n', vim.log.levels.WARN) + vim.notify('[Fzf-lua-overlay] ' .. msg, vim.log.levels.WARN) +end + +u.gh_curl = function(url) + local content = vim.fn.system { 'curl', '-s', url } + content = vim.json.decode(content) + -- gh api limit + if not content or (content.message and content.message:match('API rate limit exceeded')) then + return + end + return content end return u