Skip to content

Commit

Permalink
fix: log on api limited
Browse files Browse the repository at this point in the history
  • Loading branch information
phanen committed Jun 19, 2024
1 parent 4120359 commit 105bcdb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
14 changes: 9 additions & 5 deletions lua/fzf-lua-overlay/providers/gitignore.lua
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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,
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions lua/fzf-lua-overlay/providers/license.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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)
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion lua/fzf-lua-overlay/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 105bcdb

Please sign in to comment.