From 2934979ce4837f79eb87e73311183c14435884f0 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 5 Dec 2019 10:58:10 +0000 Subject: [PATCH] sekrit style fixes --- bin/credentials.sh | 99 +++++++++++++++++++++++----------------------- bin/sekrit.sh | 2 +- bin/xsekrit.sh | 4 +- 3 files changed, 52 insertions(+), 53 deletions(-) diff --git a/bin/credentials.sh b/bin/credentials.sh index cfaf1bf..6e4f8d1 100644 --- a/bin/credentials.sh +++ b/bin/credentials.sh @@ -10,7 +10,8 @@ # Dedication along with this software. If not, see # . -usage() { +usage() +{ cat - <<. >&2 Usage: ${0##*/} [-2pu] service @@ -18,66 +19,68 @@ Usage: exit 1 } -clip() { xclip -q -r -l 1 -sel clip 2>/dev/null; } - -get_user() { - echo user - sekrit get services/"$1"/user | clip +err() +{ + printf "%s: %s\n" "${0##*/}" "$*" >&2 + exit 1 } -get_pass() { - echo pass - sekrit get services/"$1"/pass | clip +clip() +{ + xclip -q -r -l 1 -sel clip 2>/dev/null } -get_2fa() { - echo 2fa +get_user() +{ + printf user + sekrit get "services/$1/user" | clip && printf "\n" +} + +get_pass() +{ + printf pass + sekrit get "services/$1/pass" | clip && printf "\n" +} + +get_2fa() +{ + printf 2fa otpcli_opts= case $1 in isnic) otpcli_opts="-H sha512 -d 8" ;; esac - sekrit get services/"$1"/2fa | otpcli $otpcli_opts | clip + sekrit get "services/$1/2fa" | otpcli $otpcli_opts | clip && printf "\n" } -get_from_flags() { - user=$1 - pass=$2 - sfa=$3 - s=$4 +get_from_flags() +{ + service=$1 + user=$2 + pass=$3 + sfa=$4 - if [ $user = yes ] && ! sekrit has services/"$s"/user; then - printf "%s: service \"%s\" has no user.\n" \ - "${0##*/}" "$s" >&2 - exit 1 - fi - if [ $pass = yes ] && ! sekrit has services/"$s"/pass; then - printf "%s: service \"%s\" has no pass.\n" \ - "${0##*/}" "$s" >&2 - exit 1 - fi - if [ $sfa = yes ] && ! sekrit has services/"$s"/2fa; then - printf "%s: service \"%s\" has no 2fa.\n" \ - "${0##*/}" "$s" >&2 - exit 1 - fi + [ $user = yes ] && sekrit has services/"$service"/user || + err "Service \"$service\" has no user." + [ $pass = yes ] && sekrit has services/"$service"/pass || + err "Service \"$service\" has no pass." + [ $sfa = yes ] && sekrit has services/"$service"/2fa || + err "Service \"$service\" has no 2fa." - [ $user = yes ] && get_user "$s" - [ $pass = yes ] && get_pass "$s" - [ $sfa = yes ] && get_2fa "$s" + [ $user = yes ] && get_user "$service" + [ $pass = yes ] && get_pass "$service" + [ $sfa = yes ] && get_2fa "$service" } -get_all() { - s=$1 - if ! sekrit has services/"$s"/pass; then - printf "%s: Unknown service \"%s\".\n" \ - "${0##*/}" "$s" >&2 - exit 1 - fi +get_all() +{ + service=$1 + sekrit has "services/$service/pass" || + err "Unknown service \"$service\"." - sekrit has services/"$s"/user && get_user "$s" - get_pass "$s" - sekrit has services/"$s"/2fa && get_2fa "$s" + sekrit has "services/$service/user" && get_user "$service" + get_pass "$service" + sekrit has "services/$service/2fa" && get_2fa "$service" } sfa=no @@ -97,13 +100,11 @@ while getopts 2pu flag; do done shift $(($OPTIND - 1)) -if [ $# -ne 1 ] || [ -z "$1" ]; then - usage -fi +[ $# -eq 1 ] && [ -n "$1" ] || usage service=$1 if [ $sfa = yes ] || [ $pass = yes ] || [ $user = yes ]; then - get_from_flags $user $pass $sfa "$service" + get_from_flags "$service" $user $pass $sfa else get_all "$service" fi diff --git a/bin/sekrit.sh b/bin/sekrit.sh index b18d0bd..9a2f192 100644 --- a/bin/sekrit.sh +++ b/bin/sekrit.sh @@ -68,7 +68,7 @@ sekrit_add() [ -f "$f" ] && err "Key \"$key\" already exists." - if [ $# -ne 0 ]; then + if [ $# -gt 0 ]; then printf "%s\n" "$*" else cat - diff --git a/bin/xsekrit.sh b/bin/xsekrit.sh index 218be2e..9d4e140 100644 --- a/bin/xsekrit.sh +++ b/bin/xsekrit.sh @@ -10,8 +10,6 @@ # Dedication along with this software. If not, see # . -clip() { xclip -q -l 1 -sel clip 2>/dev/null; } - if [ $# -ne 1 ] || [ -z "$1" ]; then printf "Usage: %s key\n" "${0##*/}" >&2 exit 1 @@ -20,4 +18,4 @@ if ! sekrit has "$1"; then printf "%s: Unknown key \"%s\".\n" "${0##*/}" "$1" >&2 exit 1 fi -sekrit get "$1" | clip +sekrit get "$1" | xclip -q -r -l 1 -sel clip 2>/dev/null