credentials: handle all cases with one function

This commit is contained in:
Lucas 2019-12-27 23:20:28 +00:00
parent c0f051acd7
commit 83d15c417d

View File

@ -50,24 +50,35 @@ get_2fa()
sekrit get "services/$1/2fa" | otpcli $otpcli_opts | clip && printf "\n"
}
get_from_flags()
get_service()
{
service=$1
user=$2
pass=$3
sfa=$4
if [ $user = yes ]; then
if [ $user = no ] && [ $pass = no ] && [ $sfa = no ]; then
sekrit has "services/$service/pass" ||
err "Unknown service \"$service\"."
pass=yes
sekrit has services/"$service"/user && user=yes
sekrit has services/"$service"/2fa && sfa=yes
fi
if [ $user = check ]; then
sekrit has services/"$service"/user ||
err "Service \"$service\" has no user."
user=yes
fi
if [ $pass = yes ]; then
if [ $pass = check ]; then
sekrit has services/"$service"/pass ||
err "Service \"$service\" has no pass."
pass=yes
fi
if [ $sfa = yes ]; then
if [ $sfa = check ]; then
sekrit has services/"$service"/2fa ||
err "Service \"$service\" has no 2fa."
sfa=yes
fi
[ $user = yes ] && get_user "$service"
@ -75,27 +86,16 @@ get_from_flags()
[ $sfa = yes ] && get_2fa "$service"
}
get_all()
{
service=$1
sekrit has "services/$service/pass" ||
err "Unknown service \"$service\"."
sekrit has "services/$service/user" && get_user "$service"
get_pass "$service"
sekrit has "services/$service/2fa" && get_2fa "$service"
}
sfa=no
pass=no
user=no
while getopts 2pu flag; do
case $flag in
2) sfa=yes
2) sfa=check
;;
p) pass=yes
p) pass=check
;;
u) user=yes
u) user=check
;;
*) usage
;;
@ -104,10 +104,6 @@ done
shift $(($OPTIND - 1))
[ $# -eq 1 ] && [ -n "$1" ] || usage
service=$1
key=$1
if [ $sfa = yes ] || [ $pass = yes ] || [ $user = yes ]; then
get_from_flags "$service" $user $pass $sfa
else
get_all "$service"
fi
get_service "$key" $user $pass $sfa