comparison src/main/kotlin/name/blackcap/passman/CreateSubcommand.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 302d224bbd57
children 07406c4af4a5
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 kotlin.system.exitProcess
5 4
6 class CreateSubcommand(): Subcommand() { 5 class CreateSubcommand(): Subcommand() {
7 private companion object { 6 private companion object {
8 const val GENERATE = "generate" 7 const val GENERATE = "generate"
9 const val HELP = "help" 8 const val HELP = "help"
11 const val SYMBOLS = "symbols" 10 const val SYMBOLS = "symbols"
12 const val VERBOSE = "verbose" 11 const val VERBOSE = "verbose"
13 } 12 }
14 private lateinit var commandLine: CommandLine 13 private lateinit var commandLine: CommandLine
15 14
16 override fun run(args: Array<String>) { 15 override fun run(args: Array<String>): {
17 val options = Options().apply { 16 val options = Options().apply {
18 addOption("g", GENERATE, false, "Use password generator.") 17 addOption("g", GENERATE, false, "Use password generator.")
19 addOption("h", HELP, false, "Print this help message.") 18 addOption("h", HELP, false, "Print this help message.")
20 addOption("l", LENGTH, true, "Length of generated password (default $DEFAULT_GENERATED_LENGTH).") 19 addOption("l", LENGTH, true, "Length of generated password (default $DEFAULT_GENERATED_LENGTH).")
21 addOption("s", SYMBOLS, false, "Use symbol characters in generated password.") 20 addOption("s", SYMBOLS, false, "Use symbol characters in generated password.")
22 addOption("v", VERBOSE, false, "Print the generated password.") 21 addOption("v", VERBOSE, false, "Print the generated password.")
23 } 22 }
24 try { 23 try {
25 commandLine = DefaultParser().parse(options, args) 24 commandLine = DefaultParser().parse(options, args)
26 } catch (e: ParseException) { 25 } catch (e: ParseException) {
27 die(e.message ?: "syntax error", 2) 26 throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
28 } 27 }
29 if (commandLine.hasOption(HELP)) { 28 if (commandLine.hasOption(HELP)) {
30 HelpFormatter().printHelp("$SHORTNAME create [options]", options) 29 HelpFormatter().printHelp("$SHORTNAME create [options]", options)
31 exitProcess(0) 30 return
32 } 31 }
33 checkArguments() 32 checkArguments()
34 val db = Database.open() 33 val db = Database.default
35 34
36 val entry = if (commandLine.hasOption(GENERATE)) { 35 val entry = if (commandLine.hasOption(GENERATE)) {
37 val rawLength = commandLine.getOptionValue(LENGTH) 36 val rawLength = commandLine.getOptionValue(LENGTH)
38 val length = try { 37 val length = try {
39 rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH 38 rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH
40 } catch (e: NumberFormatException) { 39 } catch (e: NumberFormatException) {
41 -1 40 -1
42 } 41 }
43 if (length < MIN_GENERATED_LENGTH) { 42 if (length < MIN_GENERATED_LENGTH) {
44 die("${see(rawLength)} - invalid length") 43 throw SubcommandException(message = "${see(rawLength)} - invalid length")
45 } 44 }
46 Entry.withGeneratedPassword(length, 45 Entry.withGeneratedPassword(length,
47 commandLine.hasOption(SYMBOLS), 46 commandLine.hasOption(SYMBOLS),
48 commandLine.hasOption(VERBOSE)) 47 commandLine.hasOption(VERBOSE))
49 } else { 48 } else {
55 it.setLong(1, id) 54 it.setLong(1, id)
56 val result = it.executeQuery() 55 val result = it.executeQuery()
57 result.next() 56 result.next()
58 val count = result.getInt(1) 57 val count = result.getInt(1)
59 if (count > 0) { 58 if (count > 0) {
60 die("record matching ${see(entry.name)} already exists") 59 throw SubcommandException(message = "record matching ${see(entry.name)} already exists")
61 } 60 }
62 } 61 }
63 62
64 try { 63 try {
65 if (entry.notes.isNullOrBlank()) { 64 if (entry.notes.isNullOrBlank()) {
98 bad = true 97 bad = true
99 } 98 }
100 } 99 }
101 } 100 }
102 if (bad) { 101 if (bad) {
103 exitProcess(2); 102 throw SubcommandException(status = 2)
104 } 103 }
105 if (commandLine.args.isNotEmpty()) { 104 if (commandLine.args.isNotEmpty()) {
106 die("unexpected trailing arguments", 2) 105 throw SubcommandException(message = "unexpected trailing arguments", status = 2)
107 } 106 }
108 } 107 }
109 } 108 }