Created
May 15, 2022 22:31
-
-
Save oessessnex/d63ebe89380abff5a3ee70d6e76e4ec8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local M = {} | |
local uv = vim.loop | |
local fn = vim.fn | |
local api = vim.api | |
function password() | |
fn.inputsave() | |
local user = fn.expand("$USER") | |
local pw = fn.inputsecret(string.format("password for %s: ", user)) | |
fn.inputrestore() | |
return pw | |
end | |
function test(pw, k) | |
local stdin = uv.new_pipe() | |
uv.spawn("sudo", { | |
args = {"-S", "-k", "true"}, | |
stdio = {stdin, nil, nil} | |
}, k) | |
stdin:write(pw) | |
stdin:write("\n") | |
stdin:shutdown() | |
end | |
function write(pw, buf, lines, k) | |
local stdin = uv.new_pipe() | |
uv.spawn("sudo", { | |
args = {"-S", "-k", "tee", buf}, | |
stdio = {stdin, nil, nil} | |
}, k) | |
stdin:write(pw) | |
stdin:write("\n") | |
local last = table.remove(lines) | |
for _, line in ipairs(lines) do | |
stdin:write(line) | |
stdin:write("\n") | |
end | |
stdin:write(last) | |
stdin:shutdown() | |
end | |
function M.sudowrite() | |
local pw = password() | |
local buf = api.nvim_buf_get_name(0) | |
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false) | |
local function exitWrite(code, _) | |
if code == 0 then | |
vim.schedule(function() | |
api.nvim_echo({{string.format('"%s" written', buf), "Normal"}}, true, {}) | |
api.nvim_buf_set_option(0, "modified", false) | |
end) | |
end | |
end | |
local function exitTest(code, _) | |
if code == 0 then | |
write(pw, buf, lines, exitWrite) | |
else | |
vim.schedule(function() | |
api.nvim_echo({{"incorrect password", "ErrorMsg"}}, true, {}) | |
end) | |
end | |
end | |
test(pw, exitTest) | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops seems like it was issue from side of
pam-fprint-grosshack
, I'll check if anything there is preventing from usingsudo -S
. Thanks for the script, its working :)