forked from ibizaman/pass-clip
-
Notifications
You must be signed in to change notification settings - Fork 2
/
clip.bash
executable file
·103 lines (80 loc) · 2.95 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
#!/usr/bin/env bash
######################## Original License ########################
# 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/>.
#
######################## Original License ########################
# Modifications carried out by ArcherN9
# - Removed Rofi
# - The program no longer attempts to generate a password if it does not already exist
# - The original program in its current shape and form was not OSX compliant.
# - Replaced Find with fd
PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
USERNAME=$(whoami)
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 fzf, waits for the user
to select one then copies it to the clipboard.
_EOF
exit 0
}
cmd_clip_short_usage() {
echo "Usage: $PROGRAM $COMMAND [--help,-h]"
}
command_exists() {
command -v "$1" >/dev/null 2>&1 || exit 1
}
cmd_clip() {
local opts fzf=0
local err=$?
# Parse arguments
if command_exists fzf; then
fzf=1
fi
[[ $err -ne 0 ]] && die "$(cmd_clip_short_usage)"
# Figure out if we use fzf
local prompt='Search Query :: '
local fzf_cmd="fzf --print-query --prompt=\"$prompt\" -i --no-mouse"
if [ -n "$term" ]; then
fzf_cmd="$fzf_cmd -q\"$term\""
fi
fzf_cmd="$fzf_cmd | tail -n1"
if [[ $fzf = 1 ]]; then
command_exists fzf || die "Could not find fzf in \$PATH"
menu="$fzf_cmd"
fi
cd "$PASSWORD_STORE_DIR" || die "Could not locate password store directory. Please ensure \$PASSWORD_STORE_DIR is setup."
# Select a passfile
passfile=$(
cd $PASSWORD_STORE_DIR \
&& fd --type f \
| sed -e 's/.gpg$//' \
| sort \
| eval "$menu" )
# If no pass file was selected by the user, exit the program with an error
if [ -z "$passfile" ]; then
die 'No passfile selected.'
fi
# Either copy existing one or generate a new one
if ls "$passfile.gpg" > /dev/null 2>&1; then
cmd_show "$passfile" --clip || exit 1
fi
}
[[ "$1" == "help" || "$1" == "--help" || "$1" == "-h" ]] && cmd_clip_usage
cmd_clip "$@"