Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison src/main/kotlin/name/blackcap/passman/CreateSubcommand.kt @ 8:698c4a3d758d
Some code clean-up.
author | David Barts <n5jrn@me.com> |
---|---|
date | Fri, 23 Sep 2022 20:59:52 -0700 |
parents | 711cc42e96d7 |
children | 72619175004e |
comparison
equal
deleted
inserted
replaced
7:f245b9a53495 | 8:698c4a3d758d |
---|---|
2 | 2 |
3 import org.apache.commons.cli.* | 3 import org.apache.commons.cli.* |
4 import kotlin.system.exitProcess | 4 import kotlin.system.exitProcess |
5 | 5 |
6 class CreateSubcommand(): Subcommand() { | 6 class CreateSubcommand(): Subcommand() { |
7 private companion object { | |
8 const val GENERATE = "generate" | |
9 const val HELP = "help" | |
10 const val LENGTH = "length" | |
11 const val SYMBOLS = "symbols" | |
12 const val VERBOSE = "verbose" | |
13 } | |
7 private lateinit var commandLine: CommandLine | 14 private lateinit var commandLine: CommandLine |
8 | 15 |
9 override fun run(args: Array<String>) { | 16 override fun run(args: Array<String>) { |
10 val options = Options().apply { | 17 val options = Options().apply { |
11 addOption("g", "generate", false, "Use password generator.") | 18 addOption("g", GENERATE, false, "Use password generator.") |
12 addOption("l", "length", true, "Length of generated password.") | 19 addOption("h", HELP, false, "Print this help message.") |
13 addOption("s", "symbols", false, "Use symbol characters in generated password.") | 20 addOption("l", LENGTH, true, "Length of generated password.") |
14 addOption("v", "verbose", false, "Print the generated password.") | 21 addOption("s", SYMBOLS, false, "Use symbol characters in generated password.") |
22 addOption("v", VERBOSE, false, "Print the generated password.") | |
15 } | 23 } |
16 try { | 24 try { |
17 commandLine = DefaultParser().parse(options, args) | 25 commandLine = DefaultParser().parse(options, args) |
18 } catch (e: ParseException) { | 26 } catch (e: ParseException) { |
19 die(e.message ?: "syntax error", 2) | 27 die(e.message ?: "syntax error", 2) |
20 } | 28 } |
21 if (commandLine.hasOption("help")) { | 29 if (commandLine.hasOption(HELP)) { |
22 HelpFormatter().printHelp("$SHORTNAME createJv", options) | 30 HelpFormatter().printHelp("$SHORTNAME create", options) |
23 exitProcess(0) | 31 exitProcess(0) |
24 } | 32 } |
25 checkArguments() | 33 checkArguments() |
26 val db = Database.open() | 34 val db = Database.open() |
27 | 35 |
28 val entry = if (commandLine.hasOption("generate")) { | 36 val entry = if (commandLine.hasOption(GENERATE)) { |
29 val rawLength = commandLine.getOptionValue("length") | 37 val rawLength = commandLine.getOptionValue(LENGTH) |
30 val length = try { | 38 val length = try { |
31 rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH | 39 rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH |
32 } catch (e: NumberFormatException) { | 40 } catch (e: NumberFormatException) { |
33 die("${see(rawLength)} - invalid length") | 41 die("${see(rawLength)} - invalid length") |
34 -1 /* will never happen */ | 42 -1 /* will never happen */ |
35 } | 43 } |
36 val symbols = commandLine.hasOption("symbols") | 44 Entry.withGeneratedPassword(length, |
37 val verbose = commandLine.hasOption("verbose") | 45 commandLine.hasOption(SYMBOLS), |
38 Entry.withGeneratedPassword(length, symbols, verbose) | 46 commandLine.hasOption(VERBOSE)) |
39 } else { | 47 } else { |
40 Entry.withPromptedPassword() | 48 Entry.withPromptedPassword() |
41 } | 49 } |
42 val id = db.makeKey(entry.name) | 50 val id = db.makeKey(entry.name) |
43 | 51 |
50 die("record matching ${see(entry.name)} already exists") | 58 die("record matching ${see(entry.name)} already exists") |
51 } | 59 } |
52 } | 60 } |
53 | 61 |
54 try { | 62 try { |
55 if (entry.notes.isBlank()) { | 63 if (entry.notes.isNullOrBlank()) { |
56 db.connection.prepareStatement("insert into passwords (id, name, username, password, created) values (?, ?, ?, ?, ?)") | 64 db.connection.prepareStatement("insert into passwords (id, name, username, password, created) values (?, ?, ?, ?, ?)") |
57 .use { | 65 .use { |
58 it.setLong(1, id) | 66 it.setLong(1, id) |
59 it.setEncryptedString(2, entry.name, db.encryption) | 67 it.setEncryptedString(2, entry.name, db.encryption) |
60 it.setEncryptedString(3, entry.username, db.encryption) | 68 it.setEncryptedString(3, entry.username, db.encryption) |
79 } | 87 } |
80 } | 88 } |
81 | 89 |
82 private fun checkArguments(): Unit { | 90 private fun checkArguments(): Unit { |
83 var bad = false | 91 var bad = false |
84 if (!commandLine.hasOption("generate")) { | 92 if (!commandLine.hasOption(GENERATE)) { |
85 for (option in listOf<String>("length", "symbols", "verbose")) { | 93 for (option in listOf<String>(LENGTH, SYMBOLS, VERBOSE)) { |
86 if (commandLine.hasOption(option)) { | 94 if (commandLine.hasOption(option)) { |
87 error("--$option requires --generate") | 95 error("--$option requires --$GENERATE") |
88 bad = true | 96 bad = true |
89 } | 97 } |
90 } | 98 } |
91 } | 99 } |
92 if (commandLine.args.isNotEmpty()) { | 100 if (commandLine.args.isNotEmpty()) { |