-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclip.bash
executable file
·139 lines (119 loc) · 4.86 KB
/
clip.bash
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
# pass clip - Password Store Extension (https://www.passwordstore.org/)
# Copyright (C) 2019 Pierre PENNINCKX <[email protected]>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
cmd_clip_usage() {
cat <<-_EOF
Usage:
$PROGRAM clip [options]
Provide an interactive solution to copy passwords to the
clipboard. It will show all pass-names in either fzf or rofi,
waits for the user to select one then copy it to the clipboard.
The user can select fzf or rofi by giving either --fzf or --rofi.
By default, rofi will be selected and pass-clip will fallback to
fzf. If the selected password does not exist, a new one will be
generated automatically then copied to the clipboard. Specific
password length can be given using --length and no symbols can
be activated with --no-symbols. Note the latter two options must
be given on the command line, one cannot specify them through
fzf or rofi.
Options:
-f, --fzf Use fzf to select pass-name.
-r, --rofi Use rofi to select pass-name.
-c, --choose Use choose to select pass-name.
-n, --no-symbols Do not use any non-alphanumeric characters.
-l, --length Provide a password length.
_EOF
exit 0
}
cmd_clip_short_usage() {
echo "Usage: $PROGRAM $COMMAND [--help,-h] [--fzf,-f]|[--rofi,-r][--choose,-c] [--no-symbols,-n] [-l <s>,--length <s>]"
}
command_exists() {
command -v "$1" >/dev/null 2>&1 || exit 1
}
cmd_clip() {
# Parse arguments
local opts fzf=0 rofi=0 choose=0
local symbols="" length="25"
local term=""
opts="$($GETOPT -o s:frcnl: -l search-term:,fzf,rofi,choose,no-symbols,length: -n "$PROGRAM $COMMAND" -- "$@")"
local err=$?
eval set -- "$opts"
while true; do case "$1" in
-f|--fzf) fzf=1; shift ;;
-r|--rofi) rofi=1; shift ;;
-c|--choose) choose=1; shift ;;
-n|--no-symbols) symbols="--no-symbols"; shift ;;
-l|--length) length="$2"; shift 2 ;;
-s|--search-term) term="$2"; shift 2 ;;
--) shift; break ;;
esac done
if [[ $fzf = 0 && $rofi = 0 && $choose = 0 ]]; then
if command_exists fzf; then
fzf=1
elif command_exists rofi; then
rofi=1
elif command_exists choose; then
choose=1
fi
fi
[[ $err -ne 0 ]] && die "$(cmd_clip_short_usage)"
# Figure out if we use fzf or rofi
local prompt='Copy password into clipboard for 45 seconds'
local fzf_cmd="fzf --print-query --prompt=\"$prompt \""
local rofi_cmd="rofi -dmenu -i -p \"$prompt\""
local choose_cmd="choose -m -p \"$prompt\""
if [ -n "$term" ]; then
fzf_cmd="$fzf_cmd -q\"$term\""
rofi_cmd="$rofi_cmd -filter \"$term\""
choose_cmd="$choose_cmd -o \"$term\""
fi
fzf_cmd="$fzf_cmd | tail -n1"
if [[ ($fzf = 1 && $rofi = 1) || ($fzf = 1 && $choose = 1) || ($rofi = 1 && $choose = 1) ]]; then
die 'Either --fzf,-f or --rofi,-r or --choose,-c must be given'
fi
if [[ $rofi = 1 ]]; then
command_exists rofi || die "Could not find rofi in \$PATH"
menu="$rofi_cmd"
elif [[ $fzf = 1 ]]; then
command_exists fzf || die "Could not find fzf in \$PATH"
menu="$fzf_cmd"
elif [[ $choose = 1 ]]; then
command_exists choose || die "Could not find choose in \$PATH"
menu="$choose_cmd"
else
die "Could not find either fzf, rofi or choose in \$PATH"
fi
cd "$PASSWORD_STORE_DIR" || exit 1
# Select a passfile
passfile=$(find -L "$PASSWORD_STORE_DIR" -path '*/.git' -prune -o -path '*/.extensions' -prune -o -iname '*.gpg' -printf '%P\n' |
sed -e 's/.gpg$//' |
sort |
eval "$menu")
if [ -z "$passfile" ]; then
die 'No passfile selected.'
fi
# Either copy existing one or generate a new one
if ls "$PASSWORD_STORE_DIR/$passfile.gpg" >/dev/null 2>&1; then
cmd_show "$passfile" --clip || exit 1
else
cmd_generate "$passfile" "$length" $symbols --clip || exit 1
fi
}
[[ "$1" == "help" || "$1" == "--help" || "$1" == "-h" ]] && cmd_clip_usage
cmd_clip "$@"