Mercurial > cgi-bin > hgweb.cgi > PassMan
view src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 28:287eadf5ab30 default tip
Check for timeouts inside subcommands while in interactive mode as well.
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 31 Jul 2024 11:21:18 -0700 |
parents | ea65ab890f66 |
children |
line wrap: on
line source
package name.blackcap.passman import org.apache.commons.cli.* import java.sql.ResultSet class MergeSubcommand(): Subcommand() { private companion object { const val FORCE = "force" const val HELP = "help" const val VERBOSE = "verbose" } private lateinit var commandLine: CommandLine private lateinit var db: Database override fun run(args: Array<String>) { parseArguments(args) if (commandLine.hasOption(MergeSubcommand.HELP)) { return } db = Database.default doMerge() } private fun parseArguments(args: Array<String>) { val options = Options().apply { addOption("v", MergeSubcommand.VERBOSE, false, "Verbose mode, print what we are doing.") addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.") addOption("h", MergeSubcommand.HELP, false, "Print this help message.") } try { commandLine = DefaultParser().parse(options, args) } catch (e: ParseException) { throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e) } if (commandLine.hasOption(MergeSubcommand.HELP)) { HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options) return } if (commandLine.args.isEmpty()) { throw SubcommandException(message = "expecting other database name", status = 2) } if (commandLine.args.size > 1) { throw SubcommandException(message = "unexpected trailing arguments", status = 2) } } private fun doMerge() { val otherFile = commandLine.args[0] val otherDb = Database.open( fileName = otherFile, passwordPrompt = "Key for ${see(otherFile)}: ", create = false ) otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords").use { stmt -> val results = stmt.executeQuery() while (results.next()) { val otherEntry = makeEntry(otherDb, results) vprint("read ${see(otherEntry.name)}…") val thisEntry = getEntry(db, otherEntry.name) if (thisEntry == null) { vprintln(" missing, inserting it") otherEntry.insert(db) } else { doCompare(thisEntry, otherEntry) thisEntry.password.clear() } otherEntry.password.clear() } } } private fun makeEntry(dbParam: Database, results: ResultSet) = Entry( name = results.getDecryptedString(1, dbParam.encryption)!!, username = results.getDecryptedString(2, dbParam.encryption)!!, password = results.getDecrypted(3, dbParam.encryption)!!, notes = results.getDecryptedString(4, dbParam.encryption), created = results.getDate(5), modified = results.getDate(6), accessed = results.getDate(7) ) private fun getEntry(dbParam: Database, name: String): Entry? { dbParam.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { stmt -> stmt.setLong(1, dbParam.makeKey(name)) val results = stmt.executeQuery() return if (results.next()) makeEntry(dbParam, results) else null } } private fun doCompare(thisEntry: Entry, otherEntry: Entry) { if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) { vprintln(" newer, updating it") db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use { it.setEncryptedString(1, otherEntry.name, db.encryption) it.setEncryptedString(2, otherEntry.username, db.encryption) it.setEncrypted(3, otherEntry.password, db.encryption) it.setEncryptedString(4, otherEntry.notes, db.encryption) it.setLong(5, otherEntry.modifiedOrCreated.time) it.setLong(6, db.makeKey(thisEntry.name)) it.executeUpdate() } } else { vprintln(" older or update denied, ignoring it") } } private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean = commandLine.hasOption(FORCE) || askUserIfOkToOverwrite(thisEntry, otherEntry) private fun vprint(message: String) { if (commandLine.hasOption(VERBOSE)) { print(message) } } private fun vprintln(message: String) { if (commandLine.hasOption(VERBOSE)) { println(message) } } }