commit 5d30d6c6cc74ef0b0ed69ee05581012cad9ae37c Author: Olivier Perret Date: Tue Sep 19 17:01:46 2017 +0200 First commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..194f43c --- /dev/null +++ b/README.md @@ -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 diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..00d2e13 --- /dev/null +++ b/UNLICENSE @@ -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 \ No newline at end of file diff --git a/sudo-write.kak b/sudo-write.kak new file mode 100644 index 0000000..92f4996 --- /dev/null +++ b/sudo-write.kak @@ -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 "|sudo -S echo ok" + try %{ + exec ok + 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 + } +} +