annotate src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 11:c69665ff37d0

Add merge subcommand (untested).
author David Barts <n5jrn@me.com>
date Sat, 21 Jan 2023 15:39:42 -0800
parents 72619175004e
children a38a2a1036c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 package name.blackcap.passman
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
2
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
3 import org.apache.commons.cli.*
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
4 import java.sql.ResultSet
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
5 import java.util.*
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
6 import kotlin.system.exitProcess
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
7
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 class MergeSubcommand(): Subcommand() {
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
9 private companion object {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
10 const val FORCE = "force"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
11 const val HELP = "help"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
12 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
13 private lateinit var commandLine: CommandLine
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
14 private lateinit var db: Database
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
15
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 override fun run(args: Array<String>) {
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
17 parseArguments(args)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
18 db = Database.open()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
19 doMerge()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
20 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
21
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
22 private fun parseArguments(args: Array<String>) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
23 val options = Options().apply {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
24 addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
25 addOption("h", MergeSubcommand.HELP, false, "Print this help message.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
26 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
27 try {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
28 commandLine = DefaultParser().parse(options, args)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
29 } catch (e: ParseException) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
30 die(e.message ?: "syntax error", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
31 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
32 if (commandLine.hasOption(MergeSubcommand.HELP)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
33 HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
34 exitProcess(0)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
35 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
36 if (commandLine.args.isEmpty()) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
37 die("expecting other database name", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
38 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
39 if (commandLine.args.size > 1) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
40 die("unexpected trailing arguments", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
41 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
42 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
43
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
44 private fun doMerge() {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
45 val otherFile = commandLine.args[0]
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
46 val otherDb = Database.open(
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
47 fileName = otherFile,
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
48 passwordPrompt = "Key for ${see(otherFile)}: ", create = false
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
49 )
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
50 otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords").use { stmt ->
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
51 val results = stmt.executeQuery()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
52 while (results.next()) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
53 val otherEntry = makeEntry(results)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
54 val thisEntry = getEntry(db, otherEntry.name)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
55 if (thisEntry == null) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
56 doInsert(otherEntry)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
57 } else {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
58 doCompare(thisEntry, otherEntry)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
59 thisEntry.password.clear()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
60 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
61 otherEntry.password.clear()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
62 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
63 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 }
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
65
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
66 private fun makeEntry(results: ResultSet) = Entry(
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
67 name = results.getDecryptedString(1, db.encryption)!!,
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
68 username = results.getDecryptedString(2, db.encryption)!!,
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
69 password = results.getDecrypted(3, db.encryption)!!,
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
70 notes = results.getDecryptedString(4, db.encryption),
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
71 created = results.getDate(5),
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
72 modified = results.getDate(6),
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
73 accessed = results.getDate(7)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
74 )
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
75
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
76 private fun getEntry(otherDb: Database, name: String): Entry? {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
77 otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { stmt ->
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
78 stmt.setLong(1, otherDb.makeKey(name))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
79 val results = stmt.executeQuery()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
80 return if (results.next()) makeEntry(results) else null
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
81 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
82 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
83
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
84 private fun doInsert(entry: Entry) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
85 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
86 .use {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
87 it.setLong(1, db.makeKey(entry.name))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
88 it.setEncryptedString(2, entry.name, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
89 it.setEncryptedString(3, entry.username, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
90 it.setEncrypted(4, entry.password, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
91 it.setEncryptedString(5, entry.notes, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
92 it.setLongOrNull(6, entry.created?.time)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
93 it.setLongOrNull(7, entry.modified?.time)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
94 it.setLongOrNull(8, entry.accessed?.time)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
95 it.executeUpdate()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
96 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
97 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
98
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
99 private fun doCompare(thisEntry: Entry, otherEntry: Entry) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
100 if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
101 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
102 it.setEncryptedString(1, otherEntry.name, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
103 it.setEncryptedString(2, otherEntry.username, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
104 it.setEncrypted(3, otherEntry.password, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
105 it.setEncryptedString(4, otherEntry.notes, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
106 it.setLong(5, otherEntry.modifiedOrCreated.time)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
107 it.setLong(6, db.makeKey(thisEntry.name))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
108 it.executeUpdate()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
109 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
110 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
111 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
112
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
113 private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
114 if (commandLine.hasOption(MergeSubcommand.FORCE)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
115 return true
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
116 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
117 val REDACTED = "(redacted)"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
118 println("EXISTING ENTRY:")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
119 thisEntry.printLong(REDACTED)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
120 println()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
121 println("NEWER ENTRY:")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
122 otherEntry.printLong(REDACTED)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
123 println()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
124 val answer = name.blackcap.passman.readLine("OK to overwrite existing entry? ")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
125 println()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
126 return answer.trimStart().firstOrNull()?.uppercaseChar() in setOf('T', 'Y')
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
127 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
128
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
129 }