diff --git a/pkgs/by-name/ks/ks/ks-completion.bash b/pkgs/by-name/ks/ks/ks-completion.bash new file mode 100644 index 000000000000..8afa49e595f0 --- /dev/null +++ b/pkgs/by-name/ks/ks/ks-completion.bash @@ -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 diff --git a/pkgs/by-name/ks/ks/ks-completion.zsh b/pkgs/by-name/ks/ks/ks-completion.zsh new file mode 100644 index 000000000000..2ba2736b1e3a --- /dev/null +++ b/pkgs/by-name/ks/ks/ks-completion.zsh @@ -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 +} + diff --git a/pkgs/by-name/ks/ks/package.nix b/pkgs/by-name/ks/ks/package.nix index 82929b588235..73fe7a1b8d7e 100644 --- a/pkgs/by-name/ks/ks/package.nix +++ b/pkgs/by-name/ks/ks/package.nix @@ -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 = {