From c7238bec919cce65e92f5d1979b29e3958a2140b Mon Sep 17 00:00:00 2001 From: alterNERDtive Date: Tue, 19 Jul 2022 12:53:22 +0200 Subject: [PATCH] _checkinit helper --- gpgtool | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/gpgtool b/gpgtool index 945c361..93a8b71 100755 --- a/gpgtool +++ b/gpgtool @@ -86,16 +86,13 @@ EOF # Cleans up GPGtool. Clears the temp directory and closes the mounted volume. clear () { - # Abort if not currently initialized: - [[ ! -f "${_tempdir}" ]] && echo "GPGtool not currently initialized, aborting …" && exit 1 - - local tempdir="$(cat /tmp/gpgtool.tempdir)" - source "${tempdir}/env" + _checkinit # Unmount device: if [[ ! -z "${mountdev}" ]]; then echo "Unmounting ${mountdev} …" udisksctl unmount -b "${mountdev}" + [[ $? != 0 ]] && echo "Error unmounting ${mountdev}, aborting …" && exit 1 fi # Lock device: if [[ "${dev}" != "${mountdev}" ]]; then @@ -111,11 +108,7 @@ clear () { # Lists all keys stored in the key path. list () { - # Abort if not currently initialized: - [[ ! -f "${_tempdir}" ]] && echo "GPGtool not currently initialized, aborting …" && exit 1 - - local tempdir="$(cat /tmp/gpgtool.tempdir)" - source "${tempdir}/env" + _checkinit # Abort if no keys found: [[ -z "$(ls ${keypath})" ]] && @@ -131,7 +124,7 @@ list () { if [[ -z "$(ls -l ${_pkdir} | grep ^l)" ]] then - echo "No open keys. Try \`gpgtool open FINGERPRINT\`." + echo "No open keys. Try \`gpgtool open FINGERPRINT [FINGERPRINT …]\`." else echo "Currently Opened Keys:" ls -l ${_pkdir} | grep ^l | awk {'print $NF'} @@ -143,6 +136,17 @@ usage () { echo FIXXME } +# Abort if not currently initialized, otherwise load state. +_checkinit () { + [[ ! -f "${_tempdir}" ]] && echo "GPGtool not currently initialized, aborting …" && exit 1 + + tempdir="$(cat /tmp/gpgtool.tempdir)" + + [[ ! -f "${tempdir}/env" ]] && \ + echo "State information missing. Try \`gpgtool clear && gpgtool init\`. Aborting …" && exit 1 + source "${tempdir}/env" +} + # Abort for missing dependencies: for dep in ${_dependencies}; do if ! command -v ${dep} >/dev/null 2>&1; then