annotate src/main/kotlin/name/blackcap/passman/RenameSubcommand.kt @ 27:3a3067ba673b

Add idle-time detection to interactive mode, clean up imports.
author David Barts <n5jrn@me.com>
date Sat, 27 Jul 2024 09:50:54 -0700
parents ea65ab890f66
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
1 package name.blackcap.passman
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
2
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
3 import org.apache.commons.cli.*
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
4
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
5 class RenameSubcommand(): Subcommand() {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
6 private companion object {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
7 const val FORCE = "force"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
8 const val HELP = "help"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
9 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
10 private lateinit var commandLine: CommandLine
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
11 private lateinit var source: String
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
12 private lateinit var destination: String
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
13 private lateinit var db: Database
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
14
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
15 override fun run(args: Array<String>) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
16 parseArguments(args)
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
17 if (commandLine.hasOption(HELP)) {
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
18 return
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
19 }
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
20 db = Database.default
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
21 renameIt()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
22 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
23
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
24 private fun parseArguments(args: Array<String>) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
25 val options = Options().apply {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
26 addOption("f", FORCE, false, "If destination exists exists, force overwrite.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
27 addOption("h", HELP, false, "Print this help message.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
28 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
29 try {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
30 commandLine = DefaultParser().parse(options, args)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
31 } catch (e: ParseException) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
32 throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
33 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
34 if (commandLine.hasOption(HELP)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
35 HelpFormatter().printHelp("$SHORTNAME rename [options] source destination", options)
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
36 return
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
37 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
38 if (commandLine.args.size < 2) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
39 throw SubcommandException(message = "expecting source and destination", status = 2)
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
40 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
41 if (commandLine.args.size > 2) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
42 throw SubcommandException(message = "unexpected trailing arguments", status = 2)
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
43 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
44 source = commandLine.args[0]
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
45 destination = commandLine.args[1]
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
46 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
47
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
48 private fun renameIt(): Unit {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
49 val sid = db.makeKey(source)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
50 val did = db.makeKey(destination)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
51
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
52 if(!recordExists(sid)) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
53 throw SubcommandException(message = "no record matches ${see(source)}")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
54 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
55 if (recordExists(did)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
56 if (commandLine.hasOption(FORCE)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
57 deleteRecord(did)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
58 } else {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 15
diff changeset
59 throw SubcommandException(message = "record matching ${see(destination)} already exists")
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
60 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
61 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
62
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
63 db.connection.prepareStatement("select username, password, notes, created, modified, accessed from passwords where id = ?").use { sourceStmt ->
14
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
64 sourceStmt.setLong(1, sid)
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
65 val result = sourceStmt.executeQuery()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
66 result.next()
14
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
67 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)").use {
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
68 it.setLong(1, did)
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
69 it.setEncryptedString(2, destination, db.encryption)
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
70 it.setBytes(3, result.getBytes(1))
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
71 it.setBytes(4, result.getBytes(2))
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
72 it.setBytesOrNull(5, result.getBytes(3))
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
73 it.setLong(6, result.getLong(4))
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
74 it.setLong(7, System.currentTimeMillis())
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
75 it.setDateOrNull(8, result.getLong(6))
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
76 it.executeUpdate()
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
77 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
78 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
79
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
80 deleteRecord(sid)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
81 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
82
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
83 private fun recordExists(id: Long): Boolean {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
84 db.connection.prepareStatement("select count(*) from passwords where id = ?").use {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
85 it.setLong(1, id)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
86 val result = it.executeQuery()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
87 result.next()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
88 return result.getInt(1) > 0
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
89 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
90 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
91
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
92 private fun deleteRecord(id: Long): Unit {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
93 db.connection.prepareStatement("delete from passwords where id = ?").use {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
94 it.setLong(1, id);
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
95 it.executeUpdate()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
96 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
97 }
14
4dae7a15ee48 Fix bugs found in additional round of testing.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
98 }