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

Add merge subcommand (untested).
author David Barts <n5jrn@me.com>
date Sat, 21 Jan 2023 15:39:42 -0800
parents
children 4dae7a15ee48
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 import kotlin.system.exitProcess
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
5
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
6 class RenameSubcommand(): Subcommand() {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
7 private companion object {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
8 const val FORCE = "force"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
9 const val HELP = "help"
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
10 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
11 private lateinit var commandLine: CommandLine
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
12 private lateinit var source: String
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
13 private lateinit var destination: String
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
14 private lateinit var db: Database
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
15
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
16 override fun run(args: Array<String>) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
17 parseArguments(args)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
18 db = Database.open()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
19 renameIt()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
20 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
21
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
22 private fun parseArguments(args: Array<String>) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
23 val options = Options().apply {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
24 addOption("f", FORCE, false, "If destination exists exists, force overwrite.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
25 addOption("h", HELP, false, "Print this help message.")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
26 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
27 try {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
28 commandLine = DefaultParser().parse(options, args)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
29 } catch (e: ParseException) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
30 die(e.message ?: "syntax error", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
31 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
32 if (commandLine.hasOption(HELP)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
33 HelpFormatter().printHelp("$SHORTNAME rename [options] source destination", options)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
34 exitProcess(0)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
35 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
36 if (commandLine.args.size < 2) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
37 die("expecting source and destination", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
38 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
39 if (commandLine.args.size > 2) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
40 die("unexpected trailing arguments", 2)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
41 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
42 source = commandLine.args[0]
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
43 destination = commandLine.args[1]
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
44 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
45
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
46 private fun renameIt(): Unit {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
47 val sid = db.makeKey(source)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
48 val did = db.makeKey(destination)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
49
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
50 if(!recordExists(sid)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
51 die("no record matches ${see(source)}")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
52 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
53 if (recordExists(did)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
54 if (commandLine.hasOption(FORCE)) {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
55 deleteRecord(did)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
56 } else {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
57 die("record matching ${see(destination)} already exists")
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
58 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
59 }
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 db.connection.prepareStatement("select username, password, notes, created, modified, accessed from passwords where id = ?").use { sourceStmt ->
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
62 sourceStmt.setLong(1, did)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
63 val result = sourceStmt.executeQuery()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
64 result.next()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
65 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)").run {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
66 setLong(1, did)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
67 setEncryptedString(2, destination, db.encryption)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
68 setBytes(3, result.getBytes(1))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
69 setBytes(4, result.getBytes(2))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
70 setBytesOrNull(5, result.getBytes(3))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
71 setLong(6, result.getLong(4))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
72 setLong(7, System.currentTimeMillis())
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
73 setLongOrNull(8, result.getLong(6))
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
74 executeUpdate()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
75 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
76 }
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 deleteRecord(sid)
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
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
81 private fun recordExists(id: Long): Boolean {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
82 db.connection.prepareStatement("select count(*) from passwords where id = ?").use {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
83 it.setLong(1, id)
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
84 val result = it.executeQuery()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
85 result.next()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
86 return result.getInt(1) > 0
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
87 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
88 }
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 private fun deleteRecord(id: Long): Unit {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
91 db.connection.prepareStatement("delete from passwords where id = ?").use {
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
92 it.setLong(1, id);
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
93 it.executeUpdate()
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
94 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
95 }
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents:
diff changeset
96 }