Welcome to little lamb

Code » ssp » commit 038bde7

Better use of exit codes

author Olivier Brunel
2023-04-18 16:47:50 UTC
committer Olivier Brunel
2023-04-19 07:38:47 UTC
parent ae0ab0492d0fca7c4b71bba08d31f9cad42976ce

Better use of exit codes

Use EX_NOINPUT when there's no database (unless it's add ofc).

Also use EX_DATA_ERR if an try isn't found/doesn't exist.

src/ssp/list.c +6 -7
src/ssp/remove.c +6 -7
src/ssp/rename.c +6 -7
src/ssp/show.c +4 -3

diff --git a/src/ssp/list.c b/src/ssp/list.c
index e419629..f320d38 100644
--- a/src/ssp/list.c
+++ b/src/ssp/list.c
@@ -90,7 +90,7 @@ list_main(int argc, const char *argv[], const char *env[], const char usage[], v
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
     if (!r)
-        dief(0, "no database");
+        dief(EX_NOINPUT, "no database");
 
     i = 0;
     db_reset(ctx);
@@ -109,12 +109,11 @@ list_main(int argc, const char *argv[], const char *env[], const char usage[], v
     }
     if (r < 0)
         diefu(EX_DATA_ERR, "read database: file corrupted");
-    if (i) {
-        /* add new line + flush buffers */
-        if (!(list.options & (OPT_DETAILS | OPT_FMT_INI))) out(NULL);
-    } else {
-        out("no entry matching ", ESC, list.ptrn, ESC);
-    }
+    if (!i)
+        dief(EX_DATA_ERR, "no entry matching ", ESC, list.ptrn, ESC);
+
+    /* add new line + flush buffers */
+    if (!(list.options & (OPT_DETAILS | OPT_FMT_INI))) out(NULL);
 
     return 0;
 }
diff --git a/src/ssp/remove.c b/src/ssp/remove.c
index d063e8f..d96daf0 100644
--- a/src/ssp/remove.c
+++ b/src/ssp/remove.c
@@ -26,17 +26,16 @@ remove_main(int argc, const char *argv[], const char *env[], const char usage[],
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
     if (!r)
-        dief(0, "no database");
+        dief(EX_NOINPUT, "no database");
 
     r = db_find(entry, ctx);
     if (r < 0)
         diefu(EX_DATA_ERR, "find entry ", ESC, entry, ESC);
-    if (!r) {
-        out("no entry ", ESC, entry, ESC, " in database");
-    } else {
-        out("Removing ", ESC, entry, ESC, "...");
-        rebuild_db(entry, NULL, NULL, ctx);
-    }
+    if (!r)
+        dief(EX_DATA_ERR, "no entry ", ESC, entry, ESC, " in database");
+
+    out("Removing ", ESC, entry, ESC, "...");
+    rebuild_db(entry, NULL, NULL, ctx);
 
     return 0;
 }
diff --git a/src/ssp/rename.c b/src/ssp/rename.c
index 0bee7ce..656bada 100644
--- a/src/ssp/rename.c
+++ b/src/ssp/rename.c
@@ -33,17 +33,16 @@ rename_main(int argc, const char *argv[], const char *env[], const char usage[],
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
     if (!r)
-        dief(0, "no database");
+        dief(EX_NOINPUT, "no database");
 
     r = db_find(entry, ctx);
     if (r < 0)
         diefu(EX_DATA_ERR, "find entry ", ESC, entry, ESC);
-    if (!r) {
-        out("no entry ", ESC, entry, ESC, " in database");
-    } else {
-        out("Renaming ", ESC, entry, ESC, " to ", ESC, new, ESC, "...");
-        rebuild_db(entry, new, db_otp(ctx), ctx);
-    }
+    if (!r)
+        dief(EX_DATA_ERR, "no entry ", ESC, entry, ESC, " in database");
+
+    out("Renaming ", ESC, entry, ESC, " to ", ESC, new, ESC, "...");
+    rebuild_db(entry, new, db_otp(ctx), ctx);
 
     return 0;
 }
diff --git a/src/ssp/show.c b/src/ssp/show.c
index eb05e08..cfd2a21 100644
--- a/src/ssp/show.c
+++ b/src/ssp/show.c
@@ -128,7 +128,7 @@ show_main(int argc, const char *argv[], const char *env[], const char usage[], v
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
     if (!r)
-        dief(0, "no database");
+        dief(EX_NOINPUT, "no database");
 
     const char *entry = get_entry_name(argv[i], ctx);
     if (!entry)
@@ -138,8 +138,9 @@ show_main(int argc, const char *argv[], const char *env[], const char usage[], v
     if (r < 0)
         diefu(EX_DATA_ERR, "find entry ", ESC, entry, ESC);
     if (!r)
-        out("no entry ", ESC, entry, ESC, " in database");
-    else if (show.options & OPT_FMT_INI)
+        dief(EX_DATA_ERR, "no entry ", ESC, entry, ESC, " in database");
+
+    if (show.options & OPT_FMT_INI)
         show_entry_ini(show.options & OPT_NO_COMMENTS, ctx);
     else
         show_entry(show.options, ctx);