diff src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 12:a38a2a1036c3

Add import subcommand.
author David Barts <n5jrn@me.com>
date Sun, 22 Jan 2023 09:22:53 -0800
parents c69665ff37d0
children 8f3ddebb4295
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt	Sat Jan 21 15:39:42 2023 -0800
+++ b/src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt	Sun Jan 22 09:22:53 2023 -0800
@@ -2,7 +2,6 @@
 
 import org.apache.commons.cli.*
 import java.sql.ResultSet
-import java.util.*
 import kotlin.system.exitProcess
 
 class MergeSubcommand(): Subcommand() {
@@ -53,7 +52,7 @@
                 val otherEntry = makeEntry(results)
                 val thisEntry = getEntry(db, otherEntry.name)
                 if (thisEntry == null) {
-                    doInsert(otherEntry)
+                    otherEntry.insert(db)
                 } else {
                     doCompare(thisEntry, otherEntry)
                     thisEntry.password.clear()
@@ -81,21 +80,6 @@
         }
     }
 
-    private fun doInsert(entry: Entry) {
-        db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)")
-            .use {
-                it.setLong(1, db.makeKey(entry.name))
-                it.setEncryptedString(2, entry.name, db.encryption)
-                it.setEncryptedString(3, entry.username, db.encryption)
-                it.setEncrypted(4, entry.password, db.encryption)
-                it.setEncryptedString(5, entry.notes, db.encryption)
-                it.setLongOrNull(6, entry.created?.time)
-                it.setLongOrNull(7, entry.modified?.time)
-                it.setLongOrNull(8, entry.accessed?.time)
-                it.executeUpdate()
-            }
-    }
-
     private fun doCompare(thisEntry: Entry, otherEntry: Entry) {
         if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) {
             db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use {
@@ -110,20 +94,6 @@
         }
     }
 
-    private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean {
-        if (commandLine.hasOption(MergeSubcommand.FORCE)) {
-            return true
-        }
-        val REDACTED = "(redacted)"
-        println("EXISTING ENTRY:")
-        thisEntry.printLong(REDACTED)
-        println()
-        println("NEWER ENTRY:")
-        otherEntry.printLong(REDACTED)
-        println()
-        val answer = name.blackcap.passman.readLine("OK to overwrite existing entry? ")
-        println()
-        return answer.trimStart().firstOrNull()?.uppercaseChar() in setOf('T', 'Y')
-    }
-
+    private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean =
+        commandLine.hasOption(FORCE) || askUserIfOkToOverwrite(thisEntry, otherEntry)
 }