diff src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt @ 21:ea65ab890f66

More work to support interactive feature.
author David Barts <n5jrn@me.com>
date Tue, 02 Jul 2024 11:27:39 -0700
parents 302d224bbd57
children
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Sun Jun 30 22:28:52 2024 -0700
+++ b/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Tue Jul 02 11:27:39 2024 -0700
@@ -4,7 +4,6 @@
 import java.sql.Types
 import java.util.*
 import kotlin.properties.Delegates
-import kotlin.system.exitProcess
 
 class UpdateSubcommand(): Subcommand() {
     private lateinit var commandLine: CommandLine
@@ -28,6 +27,9 @@
 
     override fun run(args: Array<String>) {
         parseArguments(args)
+        if (commandLine.hasOption(HELP)) {
+            return
+        }
         checkDatabase()
         try {
             update()
@@ -47,14 +49,14 @@
         try {
             commandLine = DefaultParser().parse(options, args)
         } catch (e: ParseException) {
-            die(e.message ?: "syntax error", 2)
+            throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
         }
         if (commandLine.hasOption(HELP)) {
             HelpFormatter().printHelp("$SHORTNAME update [options] name", options)
-            exitProcess(0)
+            return
         }
         checkArguments()
-        db = Database.open()
+        db = Database.default
         nameIn = commandLine.args[0]
         id = db.makeKey(nameIn)
         val rawLength = commandLine.getOptionValue(LENGTH)
@@ -64,7 +66,7 @@
             -1
         }
         if (length < MIN_GENERATED_LENGTH) {
-            die("${see(rawLength)} - invalid length")
+            throw SubcommandException(message = "${see(rawLength)} - invalid length")
         }
     }
 
@@ -79,13 +81,13 @@
             }
         }
         if (bad) {
-            exitProcess(2);
+            throw SubcommandException(status = 2)
         }
         if (commandLine.args.isEmpty()) {
-            die("expecting site name", 2)
+            throw SubcommandException(message = "expecting site name", status = 2)
         }
         if (commandLine.args.size > 1) {
-            die("unexpected trailing arguments", 2)
+            throw SubcommandException(message = "unexpected trailing arguments", status = 2)
         }
     }
 
@@ -96,7 +98,7 @@
             result.next()
             val count = result.getInt(1)
             if (count < 1) {
-                die("no record matches " + see(nameIn))
+                throw SubcommandException(message = "no record matches " + see(nameIn))
             }
         }
     }
@@ -140,9 +142,13 @@
     }
 
     private fun updateOne(name: String): Unit {
-        val prompt = name.replaceFirstChar { it.titlecase(Locale.getDefault()) } + ": "
+        val prompt = name.replaceFirstChar { it.uppercase(Locale.getDefault()) } + ": "
         val value: Any? = if (name in SENSITIVE_FIELDS) {
-            updatePassword()
+            try {
+                getPassword(prompt)
+            } catch (e: ConsoleException) {
+                throw SubcommandException(message = e.message, cause = e)
+            }
         } else {
             val rawValue = readLine(prompt)
             if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) {
@@ -181,17 +187,4 @@
         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")
-        }
-    }
 }