# HG changeset patch # User David Barts # Date 1663736072 25200 # Node ID f245b9a53495825963d9af780bda35d045c5e891 # Parent 711cc42e96d71c42a8270b8609c6a70d0a9466a9 Efficiency improvements. diff -r 711cc42e96d7 -r f245b9a53495 src/main/kotlin/name/blackcap/passman/Database.kt --- a/src/main/kotlin/name/blackcap/passman/Database.kt Tue Sep 20 20:52:21 2022 -0700 +++ b/src/main/kotlin/name/blackcap/passman/Database.kt Tue Sep 20 21:54:32 2022 -0700 @@ -120,12 +120,6 @@ public fun ResultSet.getDecrypted(columnIndex: Int, encryption: Encryption) = encryption.decrypt(getBytes(columnIndex)) -public fun ResultSet.getDecryptedString(columnLabel: String, encryption: Encryption) = - encryption.decryptToString(getBytes(columnLabel)) - -public fun ResultSet.getDecrypted(columnLabel: String, encryption: Encryption) = - encryption.decrypt(getBytes(columnLabel)) - public fun PreparedStatement.setEncryptedString(columnIndex: Int, value: String, encryption: Encryption) = setBytes(columnIndex, encryption.encryptFromString(value)) diff -r 711cc42e96d7 -r f245b9a53495 src/main/kotlin/name/blackcap/passman/Entry.kt --- a/src/main/kotlin/name/blackcap/passman/Entry.kt Tue Sep 20 20:52:21 2022 -0700 +++ b/src/main/kotlin/name/blackcap/passman/Entry.kt Tue Sep 20 21:54:32 2022 -0700 @@ -1,11 +1,19 @@ package name.blackcap.passman import java.util.* +import kotlin.reflect.KProperty +import kotlin.reflect.full.declaredMemberProperties class Entry(val name: String, val username: String, val password: CharArray, val notes: String, val created: Date? = null, val modified: Date? = null, val accessed: Date? = null) { companion object { + private val FIELD_MAP = mutableMapOf>().apply { + Entry::class.declaredMemberProperties.forEach{ + this[it.name] = it.getter + } + } + fun withPromptedPassword() = Entry( name = _getName(), username = _getUsername(), @@ -57,6 +65,10 @@ printDate("Accessed", accessed) } + fun getField(name: String): Any? { + return FIELD_MAP[name]!!.call(this) + } + private fun printDate(tag: String, date: Date?) { kotlin.io.print("${tag}: ") if (date == null) { diff -r 711cc42e96d7 -r f245b9a53495 src/main/kotlin/name/blackcap/passman/ListSubcommand.kt --- a/src/main/kotlin/name/blackcap/passman/ListSubcommand.kt Tue Sep 20 20:52:21 2022 -0700 +++ b/src/main/kotlin/name/blackcap/passman/ListSubcommand.kt Tue Sep 20 21:54:32 2022 -0700 @@ -35,7 +35,7 @@ val REDACTED = "(redacted)".toCharArray() } private lateinit var commandLine: CommandLine - private val matchers = mutableMapOf Boolean>>() + private val matchers = mutableMapOf Boolean>>() private data class OptionDescriptor(val name: String, val help: String) @@ -75,7 +75,7 @@ } try { if (it.name !in matchers) { - matchers[it.name] = mutableListOf<(Any) -> Boolean>() + matchers[it.name] = mutableListOf<(Any?) -> Boolean>() } matchers[it.name]!! += { x -> x is String && x.contains(Regex(value, regexOptions)) } } catch (e: PatternSyntaxException) { @@ -101,7 +101,7 @@ throw RuntimeException("will never happen") } if (it.name !in matchers) { - matchers[it.name] = mutableListOf<(Any) -> Boolean>() + matchers[it.name] = mutableListOf<(Any?) -> Boolean>() } when(op) { '>' -> matchers[it.name]!! += { x -> x is Long && x > value } @@ -121,21 +121,21 @@ val printer = if (commandLine.hasOption(LONG)) Entry::printLong else Entry::print var count = 0; while (results.next()) { + val entry = Entry( + name = results.getDecryptedString(1, db.encryption), + username = results.getDecryptedString(2, db.encryption), + password = REDACTED, + notes = results.getDecryptedString(3, db.encryption), + created = results.getDate(4), + modified = results.getDate(5), + accessed = results.getDate(6) + ) val passed = matchers - .map { entry -> entry.value.fold(true) { - acc, pred -> acc && pred(results.getDecryptedString(entry.key, db.encryption)) } } + .map { x -> x.value.fold(true) { + acc, pred -> acc && pred(entry.getField(x.key)) } } .reduceOrNull() { a, b -> a && b } ?: true if (passed) { - val entry = Entry( - name = results.getDecryptedString(1, db.encryption), - username = results.getDecryptedString(2, db.encryption), - password = REDACTED, - notes = results.getDecryptedString(3, db.encryption), - created = results.getDate(4), - modified = results.getDate(5), - accessed = results.getDate(6) - ) if (count > 0) { println() } @@ -151,7 +151,3 @@ } } } - -private fun Options.addMultiOption(opt: String?, longOpt: String, hasArg: Boolean, description: String): Unit { - addOption(Option(opt, longOpt, hasArg, description).apply { args = Option.UNLIMITED_VALUES }) -} \ No newline at end of file