changeset 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 8aacca5c2d53
children eafa3779aef8
files src/main/kotlin/name/blackcap/passman/Clipboard.kt src/main/kotlin/name/blackcap/passman/Entry.kt src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt
diffstat 3 files changed, 33 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/Clipboard.kt	Sun Sep 11 16:12:47 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Clipboard.kt	Sun Sep 11 18:24:55 2022 -0700
@@ -31,7 +31,7 @@
     }
 }
 
-/* xxx: this often makes a string out of a password */
+/* XXX: This will make a string out of a password. Shikata ga nai. */
 fun writeToClipboard(charArray: CharArray) {
     CLIPBOARD.setContents(ClipboardData(String(charArray)), ClipboardOwner())
 }
--- a/src/main/kotlin/name/blackcap/passman/Entry.kt	Sun Sep 11 16:12:47 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Entry.kt	Sun Sep 11 18:24:55 2022 -0700
@@ -17,18 +17,22 @@
             )
 
         fun withGeneratedPassword(length: Int, allowSymbols: Boolean, verbose: Boolean): Entry {
-            val generated = generate(length, allowSymbols)
-            if (verbose) {
-                println("Generated password: $generated")
-            }
             return Entry(
                 name = _getName(),
                 username = _getUsername(),
-                password = generated,
+                password = _genPassword(length, allowSymbols, verbose),
                 notes = _getNotes()
             )
         }
 
+        private fun _genPassword(length: Int, allowSymbols: Boolean, verbose: Boolean): CharArray {
+            val generated = generate(length, allowSymbols)
+            if (verbose) {
+                printPassword(generated)
+            }
+            return generated
+        }
+
         private fun _getName() = mustReadLine("Name of site: ")
 
         private fun _getUsername() = mustReadLine("Username: ")
--- 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