Mercurial > cgi-bin > hgweb.cgi > PassMan
view src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt @ 9:72619175004e
Fix issues found in testing.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sat, 01 Oct 2022 09:57:23 -0700 |
parents | 698c4a3d758d |
children | c69665ff37d0 |
line wrap: on
line source
package name.blackcap.passman import org.apache.commons.cli.* import kotlin.system.exitProcess class ReadSubcommand(): Subcommand() { private companion object { const val CLIPBOARD = "clipboard" const val HELP = "help" const val LONG = "long" const val ALT_SB = "\u001b[?1049h" const val NORM_SB = "\u001b[?1049l" const val CLEAR = "\u001b[H\u001b[2J" } private lateinit var commandLine: CommandLine override fun run(args: Array<String>) { val options = Options().apply { addOption("c", CLIPBOARD, false, "Copy username and password into clipboard.") addOption("h", HELP, false, "Print this help message.") addOption("l", LONG, false, "Long format listing.") } try { commandLine = DefaultParser().parse(options, args) } catch (e: ParseException) { die(e.message ?: "syntax error", 2) } if (commandLine.hasOption(HELP)) { HelpFormatter().printHelp("$SHORTNAME read", options) exitProcess(0) } if (commandLine.args.isEmpty()) { die("expecting site name", 2) } if (commandLine.args.size > 1) { die("unexpected trailing arguments", 2) } 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( 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() } } db.connection.prepareStatement("update passwords set accessed = ? where id = ?").use { it.setLong(1, System.currentTimeMillis()) it.setLong(2, id) it.execute() } } }