diff src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt @ 2:3c792ad36b3d

Can now update a password and read it back.
author David Barts <n5jrn@me.com>
date Sun, 11 Sep 2022 18:24:55 -0700
parents a6cfdffcaa94
children eafa3779aef8
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Sun Sep 11 16:12:47 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Sun Sep 11 18:24:55 2022 -0700
@@ -97,14 +97,18 @@
 
     private fun update(): Unit {
         updateOne("username")
-        updateOne("password")
+        if (generate) {
+            generatePassword()
+        } else {
+            updateOne("password")
+        }
         updateOne("notes")
         if (fieldValues.isEmpty()) {
             error("no values changed")
             return
         }
 
-        db.connection.prepareStatement("update passwords set updated = ?, $fields where id = ?").use { stmt ->
+        db.connection.prepareStatement("update passwords set modified = ?, $fields where id = ?").use { stmt ->
             stmt.setLong(1, System.currentTimeMillis())
             fieldValues.indices.forEach { fieldIndex ->
                 val fieldValue = fieldValues[fieldIndex]
@@ -127,7 +131,11 @@
             getPassword(prompt, verify = true)
         } else {
             val rawValue = readLine(prompt)
-            if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) { null } else { rawValue }
+            if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) {
+                null
+            } else {
+                rawValue
+            }
         }
 
         val noChange = when (value) {
@@ -139,6 +147,10 @@
             return
         }
 
+        addOne(name, value)
+    }
+
+    private fun addOne(name: String, value: Any?) {
         if (fields.isNotEmpty()) {
             fields.append(", ")
         }
@@ -147,4 +159,12 @@
         fieldValues.add(value)
     }
 
+    private fun generatePassword(): Unit {
+        val newPassword = generate(length, allowSymbols)
+        if (verbose) {
+            printPassword(newPassword)
+        }
+        addOne("password", newPassword)
+    }
+
 }
\ No newline at end of file