-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_completion.otp
58 lines (55 loc) · 2.02 KB
/
bash_completion.otp
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
################################################
# Bash completion function for otp.
################################################
#
# This file should be placed in the system's
# /etc/bash_completion.d/otp or it can be placed
# at ~/.bash_completion.otp and this line added
# to ~/.bash_completion:
#
# source ~/.bash_completion.otp
################################################
_otp()
{
local cur
COMPREPLY=()
_get_comp_words_by_ref cur
# On 10/28/2023, when gpg encryption was added, this
# cache file was added. The primary reason was performance
# because tab completion was sluggish with it requiring
# gpg decryption each time. A little smarter here would be
# to compare the timestamps between otp.conf and our cache,
# but that would require knowing the location of otp.conf.
cache_file=~/.otp_completions.cache
MAX_AGE=$((60 * 30)); # Thirty minutes
if [ -f "$cache_file" ]; then
cache_mtime=$(date +%s -r "$cache_file")
now=$(date +%s)
let age=$(("$now" - "$cache_mtime"))
if [ "$age" -gt 0 -a "$age" -lt "$MAX_AGE" ]; then
COMPREPLY=( $( compgen -W "$( cat "$cache_file" )" -- "$cur" ) )
return
fi
fi
# If otp-test exists, we use it to see if completions are
# possible. This was introduced on 10/28/2023, when gpg
# encryption was added, because the user will need to provide
# the passphrase when gpg-agent does not have it cached.
# If otp-test returns a non-zero value then we print the message
# that it provided. In most cases it will be that the passphrase
# is needed for gpg to decrypt the config file.
OTP_TEST=$(which otp-test)
if [ -x "$OTP_TEST" ]; then
msg=$(otp-test 2>/dev/null)
if [ "$?" -eq 0 ]; then
otp -l > "$cache_file"
COMPREPLY=( $( compgen -W "$( cat "$cache_file" )" -- "$cur" ) )
else
echo "$msg"
fi
else
otp -l > "$cache_file"
COMPREPLY=( $( compgen -W "$( cat "$cache_file" )" -- "$cur" ) )
fi
} &&
complete -F _otp otp