feat: add sudo-demo plugin

This commit is contained in:
sxyazi
2024-11-09 22:33:47 +08:00
parent 4adba4e07b
commit eea135bdc9
3 changed files with 90 additions and 0 deletions

21
sudo-demo.yazi/LICENSE Normal file
View File

@@ -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.

25
sudo-demo.yazi/README.md Normal file
View File

@@ -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 = "<C-t>"
run = "plugin sudo-demo"
```
Press <kbd>Ctrl</kbd> + <kbd>t</kbd> 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.

44
sudo-demo.yazi/init.lua Normal file
View File

@@ -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,
}