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

Add import subcommand.
author David Barts <n5jrn@me.com>
date Sun, 22 Jan 2023 09:22:53 -0800
parents c69665ff37d0
children ea65ab890f66
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt	Sat Jan 21 15:39:42 2023 -0800
+++ b/src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt	Sun Jan 22 09:22:53 2023 -0800
@@ -37,44 +37,31 @@
         }
         val nameIn = commandLine.args[0];
         val db = Database.open()
-        val id = db.makeKey(nameIn)
-
-        db.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use {
-            it.setLong(1, id)
-            val result = it.executeQuery()
-            if (!result.next()) {
-                die("no record matches ${see(nameIn)}")
+        val entry = Entry.fromDatabase(db, nameIn)
+        if (entry == null) {
+            die("no record matches ${see(nameIn)}")
+            return // Kotlin is too stupid to realize we never get here
+        }
+        try {
+            print(ALT_SB + CLEAR)
+            val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null }
+            if (commandLine.hasOption(LONG)) {
+                entry.printLong(redaction)
+            } else {
+                entry.print(redaction)
             }
-            val entry = Entry(
-                name = result.getDecryptedString(1, db.encryption)!!,
-                username = result.getDecryptedString(2, db.encryption)!!,
-                password = result.getDecrypted(3, db.encryption)!!,
-                notes = result.getDecryptedString(4, db.encryption),
-                created = result.getDate(5),
-                modified = result.getDate(6),
-                accessed = result.getDate(7)
-            )
-            try {
-                print(ALT_SB + CLEAR)
-                val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null }
-                if (commandLine.hasOption(LONG)) {
-                    entry.printLong(redaction)
-                } else {
-                    entry.print(redaction)
-                }
-                if (commandLine.hasOption(CLIPBOARD)) {
-                    writeToClipboard(entry.password)
-                }
-                name.blackcap.passman.readLine("Press ENTER to continue: ")
-            } finally {
-                print(CLEAR + NORM_SB)
-                entry.password.clear()
+            if (commandLine.hasOption(CLIPBOARD)) {
+                writeToClipboard(entry.password)
             }
+            name.blackcap.passman.readLine("Press ENTER to continue: ")
+        } finally {
+            print(CLEAR + NORM_SB)
+            entry.password.clear()
         }
 
         db.connection.prepareStatement("update passwords set accessed = ? where id = ?").use {
             it.setLong(1, System.currentTimeMillis())
-            it.setLong(2, id)
+            it.setLong(2, db.makeKey(nameIn))
             it.execute()
         }
     }