Mercurial > cgi-bin > hgweb.cgi > PassMan
diff 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 |
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/CreateSubcommand.kt Tue Sep 20 21:54:32 2022 -0700 +++ b/src/main/kotlin/name/blackcap/passman/CreateSubcommand.kt Fri Sep 23 20:59:52 2022 -0700 @@ -4,38 +4,46 @@ import kotlin.system.exitProcess class CreateSubcommand(): Subcommand() { + private companion object { + const val GENERATE = "generate" + const val HELP = "help" + const val LENGTH = "length" + const val SYMBOLS = "symbols" + const val VERBOSE = "verbose" + } private lateinit var commandLine: CommandLine override fun run(args: Array<String>) { val options = Options().apply { - addOption("g", "generate", false, "Use password generator.") - addOption("l", "length", true, "Length of generated password.") - addOption("s", "symbols", false, "Use symbol characters in generated password.") - addOption("v", "verbose", false, "Print the generated password.") + addOption("g", GENERATE, false, "Use password generator.") + addOption("h", HELP, false, "Print this help message.") + addOption("l", LENGTH, true, "Length of generated password.") + addOption("s", SYMBOLS, false, "Use symbol characters in generated password.") + addOption("v", VERBOSE, false, "Print the generated password.") } try { commandLine = DefaultParser().parse(options, args) } catch (e: ParseException) { die(e.message ?: "syntax error", 2) } - if (commandLine.hasOption("help")) { - HelpFormatter().printHelp("$SHORTNAME createJv", options) + if (commandLine.hasOption(HELP)) { + HelpFormatter().printHelp("$SHORTNAME create", options) exitProcess(0) } checkArguments() val db = Database.open() - val entry = if (commandLine.hasOption("generate")) { - val rawLength = commandLine.getOptionValue("length") + val entry = if (commandLine.hasOption(GENERATE)) { + val rawLength = commandLine.getOptionValue(LENGTH) val length = try { rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH } catch (e: NumberFormatException) { die("${see(rawLength)} - invalid length") -1 /* will never happen */ } - val symbols = commandLine.hasOption("symbols") - val verbose = commandLine.hasOption("verbose") - Entry.withGeneratedPassword(length, symbols, verbose) + Entry.withGeneratedPassword(length, + commandLine.hasOption(SYMBOLS), + commandLine.hasOption(VERBOSE)) } else { Entry.withPromptedPassword() } @@ -52,7 +60,7 @@ } try { - if (entry.notes.isBlank()) { + if (entry.notes.isNullOrBlank()) { db.connection.prepareStatement("insert into passwords (id, name, username, password, created) values (?, ?, ?, ?, ?)") .use { it.setLong(1, id) @@ -81,10 +89,10 @@ private fun checkArguments(): Unit { var bad = false - if (!commandLine.hasOption("generate")) { - for (option in listOf<String>("length", "symbols", "verbose")) { + if (!commandLine.hasOption(GENERATE)) { + for (option in listOf<String>(LENGTH, SYMBOLS, VERBOSE)) { if (commandLine.hasOption(option)) { - error("--$option requires --generate") + error("--$option requires --$GENERATE") bad = true } }