Mercurial > cgi-bin > hgweb.cgi > PassMan
annotate src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 26:69526ae8c8de
Allow list --name to be abbreviated -n, since it is so commonly used.
author | David Barts <n5jrn@me.com> |
---|---|
date | Fri, 05 Jul 2024 10:11:31 -0700 |
parents | ea65ab890f66 |
children |
rev | line source |
---|---|
8 | 1 package name.blackcap.passman |
2 | |
11 | 3 import org.apache.commons.cli.* |
4 import java.sql.ResultSet | |
5 | |
8 | 6 class MergeSubcommand(): Subcommand() { |
11 | 7 private companion object { |
8 const val FORCE = "force" | |
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 | 11 } |
12 private lateinit var commandLine: CommandLine | |
13 private lateinit var db: Database | |
14 | |
8 | 15 override fun run(args: Array<String>) { |
11 | 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 | 21 doMerge() |
22 } | |
23 | |
24 private fun parseArguments(args: Array<String>) { | |
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 | 27 addOption("f", MergeSubcommand.FORCE, false, "Do not ask before overwriting.") |
28 addOption("h", MergeSubcommand.HELP, false, "Print this help message.") | |
29 } | |
30 try { | |
31 commandLine = DefaultParser().parse(options, args) | |
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 | 34 } |
35 if (commandLine.hasOption(MergeSubcommand.HELP)) { | |
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 | 38 } |
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 | 41 } |
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 | 44 } |
45 } | |
46 | |
47 private fun doMerge() { | |
48 val otherFile = commandLine.args[0] | |
49 val otherDb = Database.open( | |
50 fileName = otherFile, | |
51 passwordPrompt = "Key for ${see(otherFile)}: ", create = false | |
52 ) | |
53 otherDb.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords").use { stmt -> | |
54 val results = stmt.executeQuery() | |
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 | 58 val thisEntry = getEntry(db, otherEntry.name) |
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 | 61 otherEntry.insert(db) |
11 | 62 } else { |
63 doCompare(thisEntry, otherEntry) | |
64 thisEntry.password.clear() | |
65 } | |
66 otherEntry.password.clear() | |
67 } | |
68 } | |
8 | 69 } |
11 | 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 | 76 created = results.getDate(5), |
77 modified = results.getDate(6), | |
78 accessed = results.getDate(7) | |
79 ) | |
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 | 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 | 86 } |
87 } | |
88 | |
89 private fun doCompare(thisEntry: Entry, otherEntry: Entry) { | |
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 | 92 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use { |
93 it.setEncryptedString(1, otherEntry.name, db.encryption) | |
94 it.setEncryptedString(2, otherEntry.username, db.encryption) | |
95 it.setEncrypted(3, otherEntry.password, db.encryption) | |
96 it.setEncryptedString(4, otherEntry.notes, db.encryption) | |
97 it.setLong(5, otherEntry.modifiedOrCreated.time) | |
98 it.setLong(6, db.makeKey(thisEntry.name)) | |
99 it.executeUpdate() | |
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 | 103 } |
104 } | |
105 | |
12 | 106 private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean = |
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 | 120 } |