addpass.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. source $HOME/.config/rofi-pass/config
  4. if [[ -n "$3" && "$2" == "--root" ]]; then
  5. root="${2}"
  6. elif [[ -n $root ]]; then
  7. root=$root
  8. elif [[ -n $PASSWORD_STORE_DIR ]]; then
  9. root=$PASSWORD_STORE_DIR
  10. else
  11. root="$HOME/.password-store"
  12. fi
  13. if [[ $1 == "--help" || $1 == "-h" ]]; then
  14. echo "add pass files for rofi-pass"
  15. echo "(C) 2015 Rasmus Steinke <rasi at xssn dot at>"
  16. echo ""
  17. echo "--name \"foobar\" Mandatory first argument - filename of password file"
  18. echo "--root \"foobar\" Optional second argument - Absolute path to password store"
  19. echo ""
  20. echo "+FIELD \"barbaz\" Every field name has to start with \"+\""
  21. echo " Values should be quoted"
  22. echo ""
  23. echo "Example:"
  24. echo "addpass --name \"my password file\" --root \"$HOME/passwords\" +user \"Richard\" +foo \"bar\" +autotype \"foo :tab user :tab pass\""
  25. exit
  26. else
  27. echo "$1"
  28. if [[ $1 != "--name" ]]; then
  29. echo "Missing --name option. Try --help"
  30. exit
  31. elif [[ $1 == "--name" ]]; then
  32. Name="$2"
  33. fi
  34. fi
  35. echo "Using database \"$root\""
  36. OIFS=$IFS;
  37. IFS="+";
  38. fields="$@";
  39. fieldsArray=($fields);
  40. read -p "Enter password for entry \"${Name}\" > " -s pass
  41. cd "${root}"
  42. group=$(echo -e "No Group\n---\n$(find -type d -not -iwholename '*.git*' -printf '%d\t%P\n' | sort -r -nk1 | cut -f2-)" | rofi -dmenu -p "Choose Group > ")
  43. echo -e "\n\nStoring file ${Name} in group ${group}"
  44. printEntry () {
  45. echo -e "$pass\n---"
  46. for ((i=1; i<${#fieldsArray[@]}; ++i)); do
  47. field=$(echo "${fieldsArray[$i]}" | awk -F' ' '{print $1}')
  48. option=$(echo "${fieldsArray[$i]}" | cut -d ' ' -f 2- | sed -e 's/[[:blank:]]\+$//')
  49. echo "$field: $option" | grep -Ev 'name:|--root|root:|^:' #${fieldsArray[$i]}";
  50. done
  51. }
  52. if [[ "$group" == "No Group" ]]; then
  53. printEntry | PASSWORD_STORE_DIR="${root}" pass insert -m "${Name}"
  54. elif [[ "$group" == "" ]]; then
  55. exit
  56. else
  57. printEntry | PASSWORD_STORE_DIR="${root}" pass insert -m "${group}/${Name}"
  58. fi