changeset 18:8f3ddebb4295

Was using wrong db object to decrypt, fixed.
author David Barts <n5jrn@me.com>
date Tue, 04 Apr 2023 20:38:52 -0700
parents 4427199eb218
children 7d80cbcb67bb
files src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt
diffstat 1 files changed, 29 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt	Sun Feb 05 11:14:25 2023 -0800
+++ b/src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt	Tue Apr 04 20:38:52 2023 -0700
@@ -8,6 +8,7 @@
     private companion object {
         const val FORCE = "force"
         const val HELP = "help"
+        const val VERBOSE = "verbose"
     }
     private lateinit var commandLine: CommandLine
     private lateinit var db: Database
@@ -20,6 +21,7 @@
 
     private fun parseArguments(args: Array<String>) {
         val options = Options().apply {
+            addOption("v", MergeSubcommand.VERBOSE, false, "Verbose mode, print what we are doing.")
             addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.")
             addOption("h", MergeSubcommand.HELP, false, "Print this help message.")
         }
@@ -49,9 +51,11 @@
         otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords").use { stmt ->
             val results = stmt.executeQuery()
             while (results.next()) {
-                val otherEntry = makeEntry(results)
+                val otherEntry = makeEntry(otherDb, results)
+                vprint("read ${see(otherEntry.name)}…")
                 val thisEntry = getEntry(db, otherEntry.name)
                 if (thisEntry == null) {
+                    vprintln(" missing, inserting it")
                     otherEntry.insert(db)
                 } else {
                     doCompare(thisEntry, otherEntry)
@@ -62,26 +66,27 @@
         }
     }
 
-    private fun makeEntry(results: ResultSet) = Entry(
-        name = results.getDecryptedString(1, db.encryption)!!,
-        username = results.getDecryptedString(2, db.encryption)!!,
-        password = results.getDecrypted(3, db.encryption)!!,
-        notes = results.getDecryptedString(4, db.encryption),
+    private fun makeEntry(dbParam: Database, results: ResultSet) = Entry(
+        name = results.getDecryptedString(1, dbParam.encryption)!!,
+        username = results.getDecryptedString(2, dbParam.encryption)!!,
+        password = results.getDecrypted(3, dbParam.encryption)!!,
+        notes = results.getDecryptedString(4, dbParam.encryption),
         created = results.getDate(5),
         modified = results.getDate(6),
         accessed = results.getDate(7)
     )
 
-    private fun getEntry(otherDb: Database, name: String): Entry? {
-        otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { stmt ->
-            stmt.setLong(1, otherDb.makeKey(name))
+    private fun getEntry(dbParam: Database, name: String): Entry? {
+        dbParam.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { stmt ->
+            stmt.setLong(1, dbParam.makeKey(name))
             val results = stmt.executeQuery()
-            return if (results.next()) makeEntry(results) else null
+            return if (results.next()) makeEntry(dbParam, results) else null
         }
     }
 
     private fun doCompare(thisEntry: Entry, otherEntry: Entry) {
         if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) {
+            vprintln(" newer, updating it")
             db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use {
                 it.setEncryptedString(1, otherEntry.name, db.encryption)
                 it.setEncryptedString(2, otherEntry.username, db.encryption)
@@ -91,9 +96,23 @@
                 it.setLong(6, db.makeKey(thisEntry.name))
                 it.executeUpdate()
             }
+        } else {
+            vprintln(" older or update denied, ignoring it")
         }
     }
 
     private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean =
         commandLine.hasOption(FORCE) || askUserIfOkToOverwrite(thisEntry, otherEntry)
+
+    private fun vprint(message: String) {
+        if (commandLine.hasOption(VERBOSE)) {
+            print(message)
+        }
+    }
+
+    private fun vprintln(message: String) {
+        if (commandLine.hasOption(VERBOSE)) {
+            println(message)
+        }
+    }
 }