-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl.bash
executable file
·126 lines (97 loc) · 3.14 KB
/
cl.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
#!/bin/bash
# pass extension
#
# This script gets sourced in /path/to/password-store/src/password-store.sh.
# Therefore, we can use several functions and vars defined in there, such as
#
# die()
# check_sneaky_paths()
# $X_SELECTION
# $GPG
# $GPG_OPTS
#------------------------------------------------------------------------------
# cleanup
#------------------------------------------------------------------------------
set -u -o pipefail
trap cleanup INT ABRT KILL SEGV PIPE TERM STOP USR1 USR2
#------------------------------------------------------------------------------
# functions
#------------------------------------------------------------------------------
err(){
die "error: pass cl: $@"
}
xclip_store_meta(){
xclip -i -sel $meta_sel > /dev/null 2>&1
[ $? -eq 0 ] || err "xclip_store_meta failed"
}
get_meta(){
local start=2
[ $# -eq 1 ] && start=$1
tail -n +$start
}
cleanup(){
unset content pass
}
usage(){
cat << EOF
pass cl [-r <regex> | -l <lineno> ] [-s] <entry>
options:
-r <regex> : select line containing <regex> for metadata, remove <regex>
from result
-l <lineno>: instead of <regex>, copy metadata from line number <lineno>
-s : swap metadata ($meta_sel) and password ($pass_sel) selection
content
EOF
}
#------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------
meta_sel=${PASSWORD_STORE_X_SELECTION_META:-primary}
pass_sel=$X_SELECTION
local regex=
local lineno=
local swap_sels=false
while getopts r:l:sh opt; do
case $opt in
r) regex="$OPTARG";;
l) lineno="$OPTARG";;
s) swap_sels=true;;
h) usage; exit 0;;
\?) exit 1;;
esac
done
shift $((OPTIND - 1))
[ $# -ge 1 ] || err "missing argument"
[ -n "$lineno" ] && [ -n "$regex" ] && err "use either -r or -l"
local path="$1"
local passfile="$PREFIX/$path.gpg"
[ -f "$passfile" ] || err "$path is not in the password store"
check_sneaky_paths "$path"
content=$($GPG -d "${GPG_OPTS[@]}" "$passfile")
nlines=$(echo "$content" | wc -l)
if [ $nlines -gt 1 ]; then
pass=$(echo "$content" | head -n1)
if [ -n "$regex" ]; then
meta=$(echo "$content" | get_meta | grep -E -m1 "$regex") || \
err "regex '$regex' doesn't match"
echo "$meta" | sed -E "s/$regex//" | xclip_store_meta \
|| err "could not copy metadata with regex '$regex', exit $?"
elif [ -n "$lineno" ]; then
echo "$content" | get_meta $lineno | head -n1 | xclip_store_meta || \
err "could not copy metadata from line number $lineno, exit $?"
else
echo "$content" | get_meta | head -n1 \
| xclip_store_meta || err "could not copy metadata from 2nd line, exit $?"
fi
echo "Copied metadata to $meta_sel selection."
else
pass="$content"
fi
clip "$pass" "$path"
if $swap_sels; then
meta_content=$(xclip -o -sel $meta_sel)
xclip -o -sel $pass_sel | xclip -i -sel $meta_sel
echo "$meta_content" | xclip -i -sel $pass_sel
echo "Swapped selction $pass_sel <-> $meta_sel"
fi
cleanup