First commit

This commit is contained in:
Olivier Perret 2017-09-19 17:01:46 +02:00
commit 5d30d6c6cc
3 changed files with 99 additions and 0 deletions

19
README.md Normal file
View File

@ -0,0 +1,19 @@
# kakoune-sudo-write
[kakoune](http://kakoune.org) plugin to save files that you do not have write permissions for. Uses `sudo` and preserves file ownership and read-write attributes.
## Install
Add `sudo-write.kak` to your autoload dir: `~/.config/kak/autoload/`, or source it manually.
## Usage
Call the `sudo-write` command. If your password has not been cached by `sudo`, you will be prompted to input it. Upon success, the buffer is written to the file.
Note that the plugin will not work if you do not use any cache for `sudo`.
**Warning**: while the input is hidden, the password is still passed through stdin to `sudo`. Do not use this plugin if you are not comfortable with this idea.
## License
Unlicense

24
UNLICENSE Normal file
View File

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

56
sudo-write.kak Normal file
View File

@ -0,0 +1,56 @@
# save the current buffer to its file as root
# (optionally pass the user password to sudo if not cached)
declare-option -hidden str sudo_write_tmp
define-command -hidden sudo-write-impl %{
%sh{
echo "set-option buffer sudo_write_tmp '$(mktemp --tmpdir XXXXXXXX)'"
}
write %opt{sudo_write_tmp}
%sh{
sudo -- dd if="$kak_opt_sudo_write_tmp" of="$kak_buffile" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "edit!"
else
echo "echo -markup '{Error}Something went wrong'"
fi
rm -f "$kak_opt_sudo_write_tmp"
}
unset-option buffer sudo_write_tmp
}
define-command -hidden -params 1 cache-password %{
eval -no-hooks -draft %{
edit -scratch *sudo_write_pass*
reg '"' %arg{1}
exec "<a-p>|sudo -S echo ok<ret>"
try %{
exec <a-k>ok<ret>
delete-buffer
} catch %{
delete-buffer
throw
}
}
}
def sudo-write -docstring "Write the content of the buffer using sudo" %{
%sh{
# check if the password is cached
if sudo -n true > /dev/null 2>&1; then
echo "sudo-write-impl"
else
# if not, ask for it
echo "prompt -password 'Password: ' %{
try %{
cache-password %val{text}
sudo-write-impl
} catch %{
echo -markup '{Error}Incorrect password'
}
}"
fi
}
}