From eea135bdc92a8922e42d21c6ffc8944db67a9f22 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 9 Nov 2024 22:33:47 +0800 Subject: [PATCH] feat: add sudo-demo plugin --- sudo-demo.yazi/LICENSE | 21 +++++++++++++++++++ sudo-demo.yazi/README.md | 25 +++++++++++++++++++++++ sudo-demo.yazi/init.lua | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 sudo-demo.yazi/LICENSE create mode 100644 sudo-demo.yazi/README.md create mode 100644 sudo-demo.yazi/init.lua diff --git a/sudo-demo.yazi/LICENSE b/sudo-demo.yazi/LICENSE new file mode 100644 index 0000000..fb5b1d6 --- /dev/null +++ b/sudo-demo.yazi/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 yazi-rs + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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 OR COPYRIGHT HOLDERS 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. diff --git a/sudo-demo.yazi/README.md b/sudo-demo.yazi/README.md new file mode 100644 index 0000000..154c756 --- /dev/null +++ b/sudo-demo.yazi/README.md @@ -0,0 +1,25 @@ +# sudo-demo.yazi + +Just an example showing how to use `sudo` in a Yazi plugin, and the plugin itself doesn't offer any features beyond logging a message. + +## Installation + +```sh +ya pack -a yazi-rs/plugins:sudo-demo +``` + +## Usage + +Add this to your `~/.config/yazi/keymap.toml`: + +```toml +[[manager.prepend_keymap]] +on = "" +run = "plugin sudo-demo" +``` + +Press Ctrl + t to run the plugin, you should [see a message in the log](https://yazi-rs.github.io/docs/plugins/overview#logging). + +## License + +This plugin is MIT-licensed. For more information check the [LICENSE](LICENSE) file. diff --git a/sudo-demo.yazi/init.lua b/sudo-demo.yazi/init.lua new file mode 100644 index 0000000..adac9b9 --- /dev/null +++ b/sudo-demo.yazi/init.lua @@ -0,0 +1,44 @@ +--- Verify if `sudo` is already authenticated +--- @return boolean +local function sudo_already() + local status = Command("sudo"):args({ "--validate", "--non-interactive" }):status() + assert(status, "Failed to run `sudo --validate --non-interactive`") + return status.success +end + +--- Run a program with `sudo` privilege +--- @param program string +--- @param args table +--- @return Output|nil output +--- @return integer|nil err +--- 0: success +--- 1: sudo failed +local function run_with_sudo(program, args) + local cmd = Command("sudo"):args({ program, table.unpack(args) }):stdout(Command.PIPED):stderr(Command.PIPED) + if sudo_already() then + return cmd:output() + end + + local permit = ya.hide() + print(string.format("Sudo password required to run: `%s %s`", program, table.concat(args))) + local output = cmd:output() + permit:drop() + + if output.status.success or sudo_already() then + return output + end + return nil, 1 +end + +return { + entry = function() + local output = run_with_sudo("ls", { "-l" }) + if not output then + return ya.err("Failed to run `sudo ls -l`") + end + + ya.err("stdout", output.stdout) + ya.err("stderr", output.stderr) + ya.err("status.code", output.status.code) + end, +}