ks: add completions for bash and zsh (#364049)

This commit is contained in:
Aleksana
2024-12-13 10:54:37 +08:00
committed by GitHub
3 changed files with 110 additions and 1 deletions
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
_ks_completions() {
local cur prev commands secrets
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
commands="add show cp rm ls rand init help version"
case "$prev" in
ks)
# Provide top-level command suggestions
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
return 0
;;
show|cp|rm)
# Provide dynamic completion for secrets
if secrets=$(ks ls 2>/dev/null); then
COMPREPLY=($(compgen -W "$secrets" -- "$cur"))
fi
return 0
;;
add)
# For `ks add`, suggest options for adding secrets
COMPREPLY=($(compgen -W "-n" -- "$cur"))
return 0
;;
rand)
# No specific completions for `ks rand`
return 0
;;
esac
case "${COMP_WORDS[1]}" in
ls|init|help|version)
# No additional completions for these commands
return 0
;;
esac
}
# Register the completion function for the `ks` command
complete -F _ks_completions ks
+55
View File
@@ -0,0 +1,55 @@
#compdef ks
_ks() {
local -a commands
commands=(
"add:Add a secret (-n for secure note)"
"show:Decrypt and reveal a secret"
"cp:Copy a secret to the clipboard"
"rm:Remove a secret from the keychain"
"ls:List all secrets in the keychain"
"rand:Generate a random secret (optionally specify size)"
"init:Initialize the selected keychain"
"help:Show help information"
"version:Print the version number"
)
local context curcontext="$curcontext" state line
typeset -A opt_args
_arguments \
'(-k)-k[Specify keychain]:keychain name:_files' \
'1:command:->command' \
'*::arguments:->args'
case $state in
command)
_describe -t commands 'ks commands' commands
;;
args)
case $line[1] in
add)
_arguments \
'(-n)-n[Add a secure note instead of a password]' \
'1:key[The name of the secret to add]' \
'2:value[The value of the secret to add]' \
'*::values:filename'
;;
show|cp|rm)
local -a secrets
secrets=("${(@f)$(ks ls 2>/dev/null)}")
_describe -t secrets 'secrets' secrets
;;
ls)
# No additional arguments for `ks ls`
;;
rand)
_arguments '1:size[The size of the random secret to generate]'
;;
*)
;;
esac
;;
esac
}
+12 -1
View File
@@ -1,7 +1,8 @@
{
stdenv,
lib,
stdenv,
fetchFromGitHub,
installShellFiles,
}:
stdenv.mkDerivation (finalAttrs: {
@@ -15,9 +16,19 @@ stdenv.mkDerivation (finalAttrs: {
hash = "sha256-v05wqlG7Utq1b7ctvDY9MCdjHVVZZNNzuHaIBwuRjEE=";
};
nativeBuildInputs = [ installShellFiles ];
installPhase = ''
mkdir -p $out/bin
cp ${finalAttrs.pname} $out/bin/
runHook postInstall
'';
postInstall = ''
installShellCompletion \
--bash ${./ks-completion.bash} \
--zsh --name _ks ${./ks-completion.zsh}
'';
meta = {