annotate src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 23:af86b8e0b88c

Clean up exit output.
author David Barts <n5jrn@me.com>
date Tue, 02 Jul 2024 18:18:29 -0700
parents ea65ab890f66
children
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
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 class MergeSubcommand(): Subcommand() {
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
7 private companion object {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
8 const val FORCE = "force"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
9 const val HELP = "help"
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
10 const val VERBOSE = "verbose"
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
11 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
12 private lateinit var commandLine: CommandLine
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
13 private lateinit var db: Database
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
14
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 override fun run(args: Array<String>) {
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
16 parseArguments(args)
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
17 if (commandLine.hasOption(MergeSubcommand.HELP)) {
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
18 return
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
19 }
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
20 db = Database.default
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
21 doMerge()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
22 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
23
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
24 private fun parseArguments(args: Array<String>) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
25 val options = Options().apply {
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
26 addOption("v", MergeSubcommand.VERBOSE, false, "Verbose mode, print what we are doing.")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
27 addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
28 addOption("h", MergeSubcommand.HELP, false, "Print this help message.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
29 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
30 try {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
31 commandLine = DefaultParser().parse(options, args)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
32 } catch (e: ParseException) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
33 throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
34 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
35 if (commandLine.hasOption(MergeSubcommand.HELP)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
36 HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options)
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
37 return
11
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.isEmpty()) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
40 throw SubcommandException(message = "expecting other database name", status = 2)
11
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 if (commandLine.args.size > 1) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 18
diff changeset
43 throw SubcommandException(message = "unexpected trailing arguments", status = 2)
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
44 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
45 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
46
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
47 private fun doMerge() {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
48 val otherFile = commandLine.args[0]
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
49 val otherDb = Database.open(
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
50 fileName = otherFile,
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
51 passwordPrompt = "Key for ${see(otherFile)}: ", create = false
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
52 )
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
53 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
54 val results = stmt.executeQuery()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
55 while (results.next()) {
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
56 val otherEntry = makeEntry(otherDb, results)
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
57 vprint("read ${see(otherEntry.name)}…")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
58 val thisEntry = getEntry(db, otherEntry.name)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
59 if (thisEntry == null) {
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
60 vprintln(" missing, inserting it")
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
61 otherEntry.insert(db)
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
62 } else {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
63 doCompare(thisEntry, otherEntry)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
64 thisEntry.password.clear()
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 otherEntry.password.clear()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
67 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
68 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 }
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
70
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
71 private fun makeEntry(dbParam: Database, results: ResultSet) = Entry(
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
72 name = results.getDecryptedString(1, dbParam.encryption)!!,
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
73 username = results.getDecryptedString(2, dbParam.encryption)!!,
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
74 password = results.getDecrypted(3, dbParam.encryption)!!,
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
75 notes = results.getDecryptedString(4, dbParam.encryption),
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
76 created = results.getDate(5),
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
77 modified = results.getDate(6),
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
78 accessed = results.getDate(7)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
79 )
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
80
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
81 private fun getEntry(dbParam: Database, name: String): Entry? {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
82 dbParam.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { stmt ->
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
83 stmt.setLong(1, dbParam.makeKey(name))
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
84 val results = stmt.executeQuery()
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
85 return if (results.next()) makeEntry(dbParam, results) else null
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
86 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
87 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
88
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
89 private fun doCompare(thisEntry: Entry, otherEntry: Entry) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
90 if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) {
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
91 vprintln(" newer, updating it")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
92 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
93 it.setEncryptedString(1, otherEntry.name, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
94 it.setEncryptedString(2, otherEntry.username, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
95 it.setEncrypted(3, otherEntry.password, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
96 it.setEncryptedString(4, otherEntry.notes, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
97 it.setLong(5, otherEntry.modifiedOrCreated.time)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
98 it.setLong(6, db.makeKey(thisEntry.name))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
99 it.executeUpdate()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
100 }
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
101 } else {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
102 vprintln(" older or update denied, ignoring it")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
103 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
104 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
105
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
106 private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean =
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
107 commandLine.hasOption(FORCE) || askUserIfOkToOverwrite(thisEntry, otherEntry)
18
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
108
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
109 private fun vprint(message: String) {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
110 if (commandLine.hasOption(VERBOSE)) {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
111 print(message)
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
112 }
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
113 }
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
114
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
115 private fun vprintln(message: String) {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
116 if (commandLine.hasOption(VERBOSE)) {
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
117 println(message)
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
118 }
8f3ddebb4295 Was using wrong db object to decrypt, fixed.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
119 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents:
diff changeset
120 }