Skip to content

Commit a5a103c

Browse files
author
zhangfuwen
committed
bugfix: failed to write cache, create error, cd to project before open
it
1 parent e5a25b4 commit a5a103c

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

lua/github_nvim/cache.lua

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,11 @@ function M.save(cache_name, key, results, ttl)
102102
ttl = ttl or DEFAULT_TTL,
103103
}
104104

105-
-- print("save results: ", vim.inspect(results))
106-
-- print("data: ", vim.inspect(data))
107-
108-
local f = io.open(file, "w")
109-
if not f then
105+
local success = util.write_text(file, vim.json.encode(data))
106+
if not success then
110107
print("❌ Failed to write cache file:", file)
111108
return
112109
end
113-
print("file is: ", vim.inspect(file))
114-
115-
local content = vim.json.encode(data)
116-
-- print("content is " .. content)
117-
local err = f:write(content)
118-
print(vim.inspect(err))
119-
if err then
120-
print("failed to write to file, msg:", err)
121-
end
122-
f:close()
123110

124111
-- Optional: cleanup after save
125112
M.cleanup(cache_name)

lua/github_nvim/create.lua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
util = require("github_nvim.util")
1+
local util = require("github_nvim.util")
22

33
M = {}
44
--gh repo create github.nvim --template nvimdev/nvim-plugin-template --public --clone
@@ -172,8 +172,19 @@ function M.create(config)
172172
util.mkdir_p(user_dir)
173173
end
174174

175-
local clone_command = "gh repo clone " .. user_name .. config.sep .. repo_name
176-
message = "running command " .. clone_command .. " ..."
175+
--gh repo create github.nvim --template nvimdev/nvim-plugin-template --public --clone
176+
local create_command = "gh repo create " .. repo_name
177+
if use_template then
178+
create_command = create_command .. " --template " .. template_input
179+
end
180+
if is_repo_public then
181+
create_command = create_command .. " --public"
182+
end
183+
if will_clone then
184+
create_command = create_command .. " --clone"
185+
end
186+
187+
message = "running command " .. create_command .. " ..."
177188
update_status()
178189

179190
local function on_command_exit(result)
@@ -196,7 +207,7 @@ function M.create(config)
196207
end
197208

198209

199-
vim.system({ "bash", "-c", clone_command }, {
210+
vim.system({ "bash", "-c", create_command }, {
200211
text = true,
201212
cwd = user_dir,
202213
}, on_command_exit)
@@ -224,7 +235,7 @@ function M.create(config)
224235
update_status()
225236

226237
print(string.format("repo: %s, visibility: %s", repo_input, is_repo_public and "public" or "private"))
227-
do_clone(user_name, repo_name)
238+
do_create(user_name, repo_name)
228239
end
229240

230241

lua/github_nvim/pickers/github_repos.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ end
3535
local finders = require('telescope.finders')
3636
local pickers = require("telescope.pickers")
3737
local action_state = require('telescope.actions.state')
38+
local actions = require('telescope.actions')
3839
local current_prompt_bufnr = 0
3940
local filter = "all" -- local, remote
4041

@@ -80,6 +81,7 @@ local finder = finders.new_table({
8081
})
8182

8283
local function selection_open_project()
84+
actions._close(current_prompt_bufnr, true)
8385
local selection = action_state.get_selected_entry()
8486
local github_dir = config.github_dir
8587
local sep = config.sep

lua/github_nvim/util.lua

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ function M.mkdir_p(path)
1414
end
1515

1616
function M.open_project(dir)
17-
vim.cmd("Telescope find_files cwd="..dir)
17+
vim.cmd("cd " .. dir)
18+
vim.cmd("Telescope find_files cwd=" .. dir)
1819
end
1920

2021
function M.rm_rf(path)
@@ -23,6 +24,43 @@ function M.rm_rf(path)
2324
return result.code
2425
end
2526

27+
function M.write_obj(filepath, data)
28+
local content = vim.json.encode(data)
29+
return M.write_text(filepath, content)
30+
end
31+
32+
function M.write_text(filepath, content)
33+
-- Expand path safely
34+
local expanded_path = vim.fn.expand(filepath)
35+
if expanded_path == "" then
36+
print("❌ Invalid file path")
37+
return false, "Invalid path"
38+
end
39+
40+
-- Use vim.loop for safe file operations
41+
local fd, err = vim.loop.fs_open(expanded_path, "w", 438) -- 438 = 0666
42+
if not fd then
43+
print("❌ Failed to open file:", err)
44+
return false, err
45+
end
46+
47+
local bytes_written, err = vim.loop.fs_write(fd, content, -1)
48+
if not bytes_written then
49+
print("❌ Failed to write:", err)
50+
vim.loop.fs_close(fd)
51+
return false, err
52+
end
53+
54+
local ok, err = vim.loop.fs_close(fd)
55+
if not ok then
56+
print("❌ Failed to close:", err)
57+
return false, err
58+
end
59+
60+
print("✅ Successfully wrote", bytes_written, "bytes to", filepath)
61+
return true
62+
end
63+
2664
function M.mysplit(inputstr, sep)
2765
-- if sep is null, set it as space
2866
if sep == nil then
@@ -192,5 +230,4 @@ function M.get_github_repos()
192230
return items
193231
end
194232

195-
196233
return M

0 commit comments

Comments
 (0)