summaryrefslogtreecommitdiff
path: root/mac/.local/bin/fzfpass
blob: 5190f8e425d8237a02d7e1a4031d0fb4c632c2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh

set -e

usage() {
  echo "Find pass gpg files in Password-Store using fzf."
  echo ""
  echo "Usage: ${0##*/} [-h|--help]"
  echo ""
  echo "Options:"
  echo "  -h, --help  : Show this message"
  echo ""
  echo "Shortcuts:"
  echo "  return     - echo password and copy to clipboard (wayland only)"
  echo "  ctrl+a     - new password (named as per the prompt)"
  echo "  ctrl+e     - edit selected password"
  echo "  ctrl+g     - regenerate selected password"
  echo "  ctrl+r     - rename selected password"
  echo "  ctrl+x     - delete selected password"
  echo "  tab        - tab complete"
  echo "  esc/ctrl+c - exit"
  exit 0
}

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  usage
fi

passdir=${PASSWORD_STORE_DIR:-$HOME/.local/share/.password-store}
cd "$passdir"

# Unlock the password for this session
pass show "$(tree -Ffi | grep '.gpg' | sed 's/.gpg$//g' | sed 's/^..//' | head -n 1)" >/dev/null

# Main fzf session
passfile=$(
  tree -Ffi | grep '.gpg' | sed 's/.gpg$//g' | sed 's/^..//' |
    fzf-tmux \
      --header="🔑 Password Manager" \
      --reverse \
      --no-mouse \
      --preview="pass {}" \
      --header="🔑 ^a: Generate | ^e: Edit | ^g: Generate (no symbol) | ^r: Rename | ^s: Generate (symbol) | ^x: Delete | tab: Replace" \
      --bind="ctrl-a:execute(if [ -z {q} ]; then read -p \"Can't generate empty password. Press enter to continue...\"; else pass generate -n {q} < /dev/tty > /dev/tty 2>&1 && pass edit {q} < /dev/tty > /dev/tty 2>&1; fi)+reload(tree -Ffi | grep '.gpg' | sed 's/.gpg$//g')" \
      --bind="ctrl-e:execute(pass edit {} < /dev/tty > /dev/tty 2>&1)+reload(tree -Ffi | grep '.gpg' | sed 's/.gpg$//g')" \
      --bind="ctrl-r:execute(bash -c '
echo -n \"Enter new name for {}: \" > /dev/tty;
read new_name < /dev/tty;
if [ -n \"\$new_name\" ]; then
  pass mv {} \"\$new_name\" || echo \"Rename failed.\" > /dev/tty;
else
  echo \"No name entered. Rename aborted.\" > /dev/tty;
fi' < /dev/tty > /dev/tty 2>&1)+reload(tree -Ffi | grep '.gpg' | sed 's/.gpg$//g')" \
      --bind="ctrl-g:execute(if [ -z {} ]; then read -p \"Can't generate empty password. Press enter to continue...\"; else pass generate -in {} < /dev/tty > /dev/tty 2>&1 && pass edit {} < /dev/tty > /dev/tty 2>&1; fi)+reload(tree -Ffi | grep '.gpg' | sed 's/.gpg$//g')" \
      --bind="ctrl-s:execute(if [ -z {} ]; then read -p \"Can't generate empty password. Press enter to continue...\"; else pass generate -i {} < /dev/tty > /dev/tty 2>&1 && pass edit {} < /dev/tty > /dev/tty 2>&1; fi)+reload(tree -Ffi | grep '.gpg' | sed 's/.gpg$//g')" \
      --bind="ctrl-x:execute(pass rm {} < /dev/tty > /dev/tty 2>&1)+reload(tree -Ffi | grep '.gpg' | sed 's/.gpg$//g')" \
      --bind="tab:replace-query"
)

show_passdata=false

if [ "$1" = "-i" ]; then
  show_passdata=true
  shift
fi

if [ "$show_passdata" = true ]; then
  passdata="$(pass "$passfile")"
  echo "$passdata"
else
  password="$(pass show "$passfile" | head -n 1)"
  echo "$password"

  if [ -n "$password" ]; then
    case "$(uname)" in
    Darwin*)
      printf "%s" "$password" | pbcopy # Use pbcopy on macOS
      ;;
    Linux*)
      printf "%s" "$password" | xclip -selection clipboard # Use xclip on Linux
      ;;
    *)
      echo "Unsupported operating system"
      ;;
    esac
    sleep 0.1
  fi
fi