summaryrefslogtreecommitdiff
path: root/lua/config/sudo.lua
blob: b9c9299b44fdd33c17ccaea6fdce146934bab34b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- ~/.config/nvim/lua/config/sudo.lua
-- :Sudow - Write the current buffer with sudo privileges

vim.api.nvim_create_user_command("Sudow", function()
	local filepath = vim.fn.expand("%:p")
	if filepath == "" then
		vim.notify("No file to write", vim.log.levels.ERROR)
		return
	end

	-- Prompt for password
	local password = vim.fn.inputsecret("Mot de passe: ")
	if password == "" then
		vim.notify("Cancelled", vim.log.levels.WARN)
		return
	end

	-- Write buffer contents to a temp file
	local tmpfile = vim.fn.tempname()
	vim.cmd("silent write! " .. vim.fn.fnameescape(tmpfile))

	-- Use sudo to copy temp file to target
	local cmd = string.format(
		"echo %s | sudo -S cp %s %s 2>&1",
		vim.fn.shellescape(password),
		vim.fn.shellescape(tmpfile),
		vim.fn.shellescape(filepath)
	)
	local output = vim.fn.system(cmd)
	local success = vim.v.shell_error == 0

	-- Clean up temp file
	os.remove(tmpfile)

	if success then
		-- Reload buffer to clear modified flag
		vim.cmd("edit!")
		vim.notify("Written with sudo: " .. filepath, vim.log.levels.INFO)
	else
		vim.notify("sudo write failed: " .. output, vim.log.levels.ERROR)
	end
end, { desc = "Write current buffer with sudo" })

-- Allow lowercase :sudow to expand to :Sudow
vim.cmd("cnoreabbrev sudow Sudow")