pass-fzy.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. IFS=$'\n\t'
  4. # Function to copy the selected field to the clipboard
  5. copy_to_clipboard() {
  6. local choice="$1"
  7. local field="$2"
  8. local value
  9. # Read the content of the password file
  10. content=$(pass show "$choice")
  11. # Determine the value to copy based on the selected field
  12. if [[ $field == "password" ]]; then
  13. # Password is always the first line
  14. value=$(echo "$content" | head -n 1)
  15. else
  16. # Find the line that starts with the field and extract its value
  17. value=$(echo "$content" | grep "^$field:" | cut -d ' ' -f2-)
  18. fi
  19. if [[ -n $value ]]; then
  20. # Copy the value to the clipboard
  21. echo -n "$value" | xclip -selection clipboard
  22. echo "${field} copied to clipboard."
  23. else
  24. echo "Field '$field' not found."
  25. fi
  26. }
  27. pushd "$PASSWORD_STORE_DIR"
  28. # Select the password file using fzy
  29. choice="$(find . -type f -name "*.gpg" | fzf)"
  30. choice="${choice%*.gpg}"
  31. # Extract fields from the selected password file
  32. fields=$(pass show "$choice" | grep -o '^[^:]*' | tail -n +2)
  33. fields="password"$'\n'"$fields"
  34. # Select the field to copy using fzy
  35. field=$(echo "$fields" | fzf)
  36. # Copy the selected field to the clipboard
  37. copy_to_clipboard "$choice" "$field"