Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison src/main/kotlin/name/blackcap/passman/ListSubcommand.kt @ 7:f245b9a53495
Efficiency improvements.
author | David Barts <n5jrn@me.com> |
---|---|
date | Tue, 20 Sep 2022 21:54:32 -0700 |
parents | 711cc42e96d7 |
children | 698c4a3d758d |
comparison
equal
deleted
inserted
replaced
6:711cc42e96d7 | 7:f245b9a53495 |
---|---|
33 OptionDescriptor(MODIFIED, "Match time last modified.") | 33 OptionDescriptor(MODIFIED, "Match time last modified.") |
34 ) | 34 ) |
35 val REDACTED = "(redacted)".toCharArray() | 35 val REDACTED = "(redacted)".toCharArray() |
36 } | 36 } |
37 private lateinit var commandLine: CommandLine | 37 private lateinit var commandLine: CommandLine |
38 private val matchers = mutableMapOf<String, MutableList<(Any) -> Boolean>>() | 38 private val matchers = mutableMapOf<String, MutableList<(Any?) -> Boolean>>() |
39 | 39 |
40 private data class OptionDescriptor(val name: String, val help: String) | 40 private data class OptionDescriptor(val name: String, val help: String) |
41 | 41 |
42 override fun run(args: Array<String>) { | 42 override fun run(args: Array<String>) { |
43 parseArgs(args) | 43 parseArgs(args) |
73 if (!commandLine.hasOption(CASE)) { | 73 if (!commandLine.hasOption(CASE)) { |
74 regexOptions.add(RegexOption.IGNORE_CASE) | 74 regexOptions.add(RegexOption.IGNORE_CASE) |
75 } | 75 } |
76 try { | 76 try { |
77 if (it.name !in matchers) { | 77 if (it.name !in matchers) { |
78 matchers[it.name] = mutableListOf<(Any) -> Boolean>() | 78 matchers[it.name] = mutableListOf<(Any?) -> Boolean>() |
79 } | 79 } |
80 matchers[it.name]!! += { x -> x is String && x.contains(Regex(value, regexOptions)) } | 80 matchers[it.name]!! += { x -> x is String && x.contains(Regex(value, regexOptions)) } |
81 } catch (e: PatternSyntaxException) { | 81 } catch (e: PatternSyntaxException) { |
82 die("${see(value)} - invalid regular expression") | 82 die("${see(value)} - invalid regular expression") |
83 } | 83 } |
99 if (value == null) { | 99 if (value == null) { |
100 die("${see(rawValue)} - invalid time expression") | 100 die("${see(rawValue)} - invalid time expression") |
101 throw RuntimeException("will never happen") | 101 throw RuntimeException("will never happen") |
102 } | 102 } |
103 if (it.name !in matchers) { | 103 if (it.name !in matchers) { |
104 matchers[it.name] = mutableListOf<(Any) -> Boolean>() | 104 matchers[it.name] = mutableListOf<(Any?) -> Boolean>() |
105 } | 105 } |
106 when(op) { | 106 when(op) { |
107 '>' -> matchers[it.name]!! += { x -> x is Long && x > value } | 107 '>' -> matchers[it.name]!! += { x -> x is Long && x > value } |
108 '=' -> matchers[it.name]!! += { x -> x is Long && (x/1000L) == (value/1000L) } | 108 '=' -> matchers[it.name]!! += { x -> x is Long && (x/1000L) == (value/1000L) } |
109 '<' -> matchers[it.name]!! += { x -> x is Long && x < value } | 109 '<' -> matchers[it.name]!! += { x -> x is Long && x < value } |
119 db.connection.prepareStatement("select $NAME, $USERNAME, $NOTES, $CREATED, $MODIFIED, $ACCESSED from passwords").use { | 119 db.connection.prepareStatement("select $NAME, $USERNAME, $NOTES, $CREATED, $MODIFIED, $ACCESSED from passwords").use { |
120 val results = it.executeQuery() | 120 val results = it.executeQuery() |
121 val printer = if (commandLine.hasOption(LONG)) Entry::printLong else Entry::print | 121 val printer = if (commandLine.hasOption(LONG)) Entry::printLong else Entry::print |
122 var count = 0; | 122 var count = 0; |
123 while (results.next()) { | 123 while (results.next()) { |
124 val entry = Entry( | |
125 name = results.getDecryptedString(1, db.encryption), | |
126 username = results.getDecryptedString(2, db.encryption), | |
127 password = REDACTED, | |
128 notes = results.getDecryptedString(3, db.encryption), | |
129 created = results.getDate(4), | |
130 modified = results.getDate(5), | |
131 accessed = results.getDate(6) | |
132 ) | |
124 val passed = matchers | 133 val passed = matchers |
125 .map { entry -> entry.value.fold(true) { | 134 .map { x -> x.value.fold(true) { |
126 acc, pred -> acc && pred(results.getDecryptedString(entry.key, db.encryption)) } } | 135 acc, pred -> acc && pred(entry.getField(x.key)) } } |
127 .reduceOrNull() { a, b -> a && b } | 136 .reduceOrNull() { a, b -> a && b } |
128 ?: true | 137 ?: true |
129 if (passed) { | 138 if (passed) { |
130 val entry = Entry( | |
131 name = results.getDecryptedString(1, db.encryption), | |
132 username = results.getDecryptedString(2, db.encryption), | |
133 password = REDACTED, | |
134 notes = results.getDecryptedString(3, db.encryption), | |
135 created = results.getDate(4), | |
136 modified = results.getDate(5), | |
137 accessed = results.getDate(6) | |
138 ) | |
139 if (count > 0) { | 139 if (count > 0) { |
140 println() | 140 println() |
141 } | 141 } |
142 printer(entry, null) | 142 printer(entry, null) |
143 count++ | 143 count++ |
149 } | 149 } |
150 println("$count record$s found") | 150 println("$count record$s found") |
151 } | 151 } |
152 } | 152 } |
153 } | 153 } |
154 | |
155 private fun Options.addMultiOption(opt: String?, longOpt: String, hasArg: Boolean, description: String): Unit { | |
156 addOption(Option(opt, longOpt, hasArg, description).apply { args = Option.UNLIMITED_VALUES }) | |
157 } |