_checkinit helper

This commit is contained in:
alterNERDtive 2022-07-19 12:53:22 +02:00
parent ef14d2f0a9
commit c7238bec91
Signed by: alterNERDtive
GPG key ID: 547787A4FE6533F1

26
gpgtool
View file

@ -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