From 0c5eda9e55cc84d810601a40eeb6d829d85e1553 Mon Sep 17 00:00:00 2001
From: Sergey Matveev <stargrave@stargrave.org>
Date: Tue, 29 Sep 2020 20:26:44 +0300
Subject: [PATCH] "has" command and correct return codes

---
 passman | 46 +++++++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/passman b/passman
index daf0730..9d6e4e7 100755
--- a/passman
+++ b/passman
@@ -16,17 +16,35 @@
 
 DB=~/.passmandb
 
-generator()
-{
+generator() {
     gpg --armor --gen-random 1 16 | tr '+/' '-_' | tr -d =
 }
 
-cliper()
-{
+cliper() {
     xclip -in -selection $1
 }
 
+ENTITY=""
+
+finder() {
+    cd $DB
+    result=$(find . -type d -path "*$1*" | while read ent; do
+        [ -e "$ent/passwd" ] || continue
+        echo "$ent" | sed 's#^\./##'
+    done)
+    if [ $(echo "$result" | wc -l) -ne 1 ]; then
+        echo "$result"
+        exit 1
+    fi
+    [ -n "$result" ] || exit 1
+    ENTITY=$result
+    echo "$result"
+}
+
 case "$1" in
+has)
+    finder "$2"
+    ;;
 gen)
     generator
     ;;
@@ -41,23 +59,13 @@ add)
     echo "$passwd" > $DB/$dst/passwd
     ;;
 *)
-    cd $DB
-    result=$(find . -type d -path "*$1*" | while read ent; do
-        [ -e "$ent/passwd" ] || continue
-        echo "$ent" | sed 's#^\./##'
-    done)
-    if [ $(echo "$result" | wc -l) -ne 1 ]; then
-        echo "$result"
-        exit
-    fi
-    [ -n "$result" ] || exit 1
-    echo $result
-    if [ -e "$result"/data ]; then
+    finder "$1"
+    if [ -e "$ENTITY"/data ]; then
         echo -n "Associated data: "
-        cat "$result"/data
+        cat "$ENTITY"/data
     fi
-    perl -ne 'chop and print' "$result"/passwd | cliper clipboard
-    echo $(basename "$result") | perl -ne 'chop and print' | cliper primary
+    perl -ne 'chop and print' "$ENTITY"/passwd | cliper clipboard
+    echo $(basename "$ENTITY") | perl -ne 'chop and print' | cliper primary
     sleep 10
     echo -n | cliper clipboard
     ;;
-- 
2.51.0