diff src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt @ 9:72619175004e

Fix issues found in testing.
author David Barts <n5jrn@me.com>
date Sat, 01 Oct 2022 09:57:23 -0700
parents 698c4a3d758d
children cbe4c797c9a6
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Fri Sep 23 20:59:52 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Sat Oct 01 09:57:23 2022 -0700
@@ -50,7 +50,7 @@
             die(e.message ?: "syntax error", 2)
         }
         if (commandLine.hasOption(HELP)) {
-            HelpFormatter().printHelp("$SHORTNAME update", options)
+            HelpFormatter().printHelp("$SHORTNAME update [options] name", options)
             exitProcess(0)
         }
         checkArguments()
@@ -77,14 +77,14 @@
                 }
             }
         }
+        if (bad) {
+            exitProcess(2);
+        }
         if (commandLine.args.isEmpty()) {
-            error("expecting site name")
+            die("expecting site name", 2)
         }
         if (commandLine.args.size > 1) {
-            error("unexpected trailing arguments")
-        }
-        if (bad) {
-            exitProcess(2);
+            die("unexpected trailing arguments", 2)
         }
     }
 
@@ -131,13 +131,17 @@
     }
 
     private fun cleanUp(): Unit {
-        fieldValues.forEach { if (it is CharArray) { it.clear() } }
+        fieldValues.forEach {
+            if (it is CharArray) {
+                it.clear()
+            }
+        }
     }
 
     private fun updateOne(name: String): Unit {
         val prompt = name.replaceFirstChar { it.titlecase(Locale.getDefault()) } + ": "
         val value: Any? = if (name in SENSITIVE_FIELDS) {
-            getPassword(prompt, verify = true)
+            updatePassword()
         } else {
             val rawValue = readLine(prompt)
             if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) {
@@ -176,4 +180,17 @@
         addOne("password", newPassword)
     }
 
+    private fun updatePassword(): CharArray {
+        while (true) {
+            val pw1 = getPassword("Password: ")
+            if (pw1.isEmpty()) {
+                return pw1
+            }
+            val pw2 = getPassword("Verification: ")
+            if (pw1 contentEquals pw2) {
+                return pw1
+            }
+            error("mismatch, try again")
+        }
+    }
 }