comparison src/main/kotlin/name/blackcap/passman/RenameSubcommand.kt @ 21:ea65ab890f66

More work to support interactive feature.
author David Barts <n5jrn@me.com>
date Tue, 02 Jul 2024 11:27:39 -0700
parents 0fc90892a3ae
children
comparison
equal deleted inserted replaced
20:4391afcf6bd0 21:ea65ab890f66
1 package name.blackcap.passman 1 package name.blackcap.passman
2 2
3 import org.apache.commons.cli.* 3 import org.apache.commons.cli.*
4 import java.sql.PreparedStatement
5 import java.sql.Types
6 import kotlin.system.exitProcess
7 4
8 class RenameSubcommand(): Subcommand() { 5 class RenameSubcommand(): Subcommand() {
9 private companion object { 6 private companion object {
10 const val FORCE = "force" 7 const val FORCE = "force"
11 const val HELP = "help" 8 const val HELP = "help"
15 private lateinit var destination: String 12 private lateinit var destination: String
16 private lateinit var db: Database 13 private lateinit var db: Database
17 14
18 override fun run(args: Array<String>) { 15 override fun run(args: Array<String>) {
19 parseArguments(args) 16 parseArguments(args)
20 db = Database.open() 17 if (commandLine.hasOption(HELP)) {
18 return
19 }
20 db = Database.default
21 renameIt() 21 renameIt()
22 } 22 }
23 23
24 private fun parseArguments(args: Array<String>) { 24 private fun parseArguments(args: Array<String>) {
25 val options = Options().apply { 25 val options = Options().apply {
27 addOption("h", HELP, false, "Print this help message.") 27 addOption("h", HELP, false, "Print this help message.")
28 } 28 }
29 try { 29 try {
30 commandLine = DefaultParser().parse(options, args) 30 commandLine = DefaultParser().parse(options, args)
31 } catch (e: ParseException) { 31 } catch (e: ParseException) {
32 die(e.message ?: "syntax error", 2) 32 throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
33 } 33 }
34 if (commandLine.hasOption(HELP)) { 34 if (commandLine.hasOption(HELP)) {
35 HelpFormatter().printHelp("$SHORTNAME rename [options] source destination", options) 35 HelpFormatter().printHelp("$SHORTNAME rename [options] source destination", options)
36 exitProcess(0) 36 return
37 } 37 }
38 if (commandLine.args.size < 2) { 38 if (commandLine.args.size < 2) {
39 die("expecting source and destination", 2) 39 throw SubcommandException(message = "expecting source and destination", status = 2)
40 } 40 }
41 if (commandLine.args.size > 2) { 41 if (commandLine.args.size > 2) {
42 die("unexpected trailing arguments", 2) 42 throw SubcommandException(message = "unexpected trailing arguments", status = 2)
43 } 43 }
44 source = commandLine.args[0] 44 source = commandLine.args[0]
45 destination = commandLine.args[1] 45 destination = commandLine.args[1]
46 } 46 }
47 47
48 private fun renameIt(): Unit { 48 private fun renameIt(): Unit {
49 val sid = db.makeKey(source) 49 val sid = db.makeKey(source)
50 val did = db.makeKey(destination) 50 val did = db.makeKey(destination)
51 51
52 if(!recordExists(sid)) { 52 if(!recordExists(sid)) {
53 die("no record matches ${see(source)}") 53 throw SubcommandException(message = "no record matches ${see(source)}")
54 } 54 }
55 if (recordExists(did)) { 55 if (recordExists(did)) {
56 if (commandLine.hasOption(FORCE)) { 56 if (commandLine.hasOption(FORCE)) {
57 deleteRecord(did) 57 deleteRecord(did)
58 } else { 58 } else {
59 die("record matching ${see(destination)} already exists") 59 throw SubcommandException(message = "record matching ${see(destination)} already exists")
60 } 60 }
61 } 61 }
62 62
63 db.connection.prepareStatement("select username, password, notes, created, modified, accessed from passwords where id = ?").use { sourceStmt -> 63 db.connection.prepareStatement("select username, password, notes, created, modified, accessed from passwords where id = ?").use { sourceStmt ->
64 sourceStmt.setLong(1, sid) 64 sourceStmt.setLong(1, sid)