mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-01-23 02:35:38 +01:00
Merge f7e36bb3e6 into f1934d2c76
This commit is contained in:
commit
a3c79162d2
2 changed files with 106 additions and 0 deletions
40
plugins/kate-admin-helper/README.md
Normal file
40
plugins/kate-admin-helper/README.md
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
# kate-admin-helper
|
||||
|
||||
This plugin provides an automatic privilege elevation helper for **Kate** (KDE text editor) when opening files in zsh.
|
||||
|
||||
When opening files that the user does not have write access to, the plugin automatically uses the `admin://` KIO protocol, allowing **Kate** to request privilege elevation via Polkit.
|
||||
This avoids running the entire **Kate** process as root and follows modern KDE security practices.
|
||||
|
||||
If privilege elevation is needed, a message will be shown, and the user will be prompted to authenticate with their password through Polkit.
|
||||
|
||||
## Installation
|
||||
|
||||
Add `kate-admin-helper` to your plugin list in `~/.zshrc`:
|
||||
|
||||
```zsh
|
||||
plugins=(
|
||||
...
|
||||
kate-admin-helper
|
||||
)
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Open files as usual:
|
||||
|
||||
```zsh
|
||||
kate /etc/hosts
|
||||
kt /etc/fstab /etc/ssh/sshd_config
|
||||
```
|
||||
|
||||
If the file requires elevated privileges to edit, it will be opened automatically with `admin://`.
|
||||
A colored message will inform you that privilege elevation is needed, and a Polkit prompt will ask for your password.
|
||||
|
||||
The `kt` command is also available and behaves the same way.
|
||||
|
||||
## Requirements
|
||||
|
||||
- **Kate** installed
|
||||
- **kio-admin** installed
|
||||
- A running **Polkit authentication agent** (e.g., `polkit-kde-agent-5` or `polkit-kde-agent-6`)
|
||||
- `pkexec` available to create new files if needed
|
||||
66
plugins/kate-admin-helper/kate-admin-helper.plugin.zsh
Normal file
66
plugins/kate-admin-helper/kate-admin-helper.plugin.zsh
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
# Kate admin helper
|
||||
|
||||
# Color definitions (standard and portable)
|
||||
readonly RED="$(printf '\033[0;31m')"
|
||||
readonly GREEN="$(printf '\033[0;32m')"
|
||||
readonly YELLOW="$(printf '\033[1;33m')"
|
||||
readonly RESET="$(printf '\033[0m')"
|
||||
|
||||
function kt_internal() {
|
||||
if [[ $# -eq 0 ]]; then
|
||||
echo -e "${RED}Usage: kt <file1> [file2 ...]${RESET}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
for file in "$@"; do
|
||||
local abs_path
|
||||
abs_path="$(realpath -m "$file")"
|
||||
|
||||
if [[ -e "$file" ]]; then
|
||||
|
||||
if [[ -w "$file" ]]; then
|
||||
echo -e "${GREEN}Opening '${file}' normally...${RESET}"
|
||||
command kate "$file"
|
||||
else
|
||||
echo -e "${YELLOW}No write permission for '${file}', opening with admin://...${RESET}"
|
||||
command kate "admin://${abs_path}"
|
||||
fi
|
||||
|
||||
else
|
||||
local parent_dir
|
||||
parent_dir="$(dirname "$abs_path")"
|
||||
|
||||
if [[ -w "$parent_dir" ]]; then
|
||||
echo -e "${GREEN}Opening new file '${file}' normally...${RESET}"
|
||||
command kate "$file"
|
||||
else
|
||||
cat <<END
|
||||
${YELLOW}File '${file}' does not exist and no write permission for
|
||||
directory '${parent_dir}', creating it with sudo...${RESET}
|
||||
END
|
||||
|
||||
if pkexec touch "$abs_path"; then
|
||||
echo -e "${GREEN}File '${file}' created successfully.${RESET}"
|
||||
command kate "admin://${abs_path}"
|
||||
else
|
||||
echo -e "${RED}Failed to create file '${file}'.${RESET}"
|
||||
fi
|
||||
fi
|
||||
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Allow kt command: calls the internal logic
|
||||
function kt() {
|
||||
kt_internal "$@"
|
||||
}
|
||||
|
||||
# Remove any previous alias for kate to avoid conflicts
|
||||
unalias kate 2>/dev/null
|
||||
|
||||
# Allow kate command: uses the same internal logic as kt
|
||||
# (Warning: overwriting standard 'kate' command.)
|
||||
function kate() {
|
||||
kt_internal "$@"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue