diff src/main/kotlin/name/blackcap/passman/UpdateSubcommand.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/UpdateSubcommand.kt	Tue Sep 20 21:54:32 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt	Fri Sep 23 20:59:52 2022 -0700
@@ -12,13 +12,15 @@
     private lateinit var nameIn: String
     private var id by Delegates.notNull<Long>()
     private var length by Delegates.notNull<Int>()
-    private var generate by Delegates.notNull<Boolean>()
-    private var allowSymbols by Delegates.notNull<Boolean>()
-    private var verbose by Delegates.notNull<Boolean>()
     private val fields = StringBuilder()
     private val fieldValues = mutableListOf<Any?>()
 
     private companion object {
+        const val GENERATE = "generate"
+        const val HELP = "help"
+        const val LENGTH = "length"
+        const val SYMBOLS = "symbols"
+        const val VERBOSE = "verbose"
         const val NULL_SPECIFIED = "."
         val NULLABLE_FIELDS = setOf<String>("notes")
         val SENSITIVE_FIELDS = setOf<String>("password")
@@ -27,23 +29,27 @@
     override fun run(args: Array<String>) {
         parseArguments(args)
         checkDatabase()
-        update()
+        try {
+            update()
+        } finally {
+            cleanUp()
+        }
     }
 
     private fun parseArguments(args: Array<String>) {
         val options = Options().apply {
-            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.")
+            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")) {
+        if (commandLine.hasOption(HELP)) {
             HelpFormatter().printHelp("$SHORTNAME update", options)
             exitProcess(0)
         }
@@ -51,25 +57,22 @@
         db = Database.open()
         nameIn = commandLine.args[0]
         id = db.makeKey(nameIn)
-        length = commandLine.getOptionValue("length").let { rawLength ->
+        length = commandLine.getOptionValue(LENGTH)?.let { rawLength ->
             try {
-                rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH
+                rawLength.toInt()
             } catch (e: NumberFormatException) {
                 die("${see(rawLength)} - invalid length")
                 -1  /* will never happen */
             }
-        }
-        generate = commandLine.hasOption("generate")
-        allowSymbols = commandLine.hasOption("symbols")
-        verbose = commandLine.hasOption("verbose")
+        } ?: DEFAULT_GENERATED_LENGTH
     }
 
     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
                 }
             }
@@ -99,7 +102,7 @@
 
     private fun update(): Unit {
         updateOne("username")
-        if (generate) {
+        if (commandLine.hasOption(GENERATE)) {
             generatePassword()
         } else {
             updateOne("password")
@@ -127,6 +130,10 @@
         }
     }
 
+    private fun cleanUp(): Unit {
+        fieldValues.forEach { if (it is CharArray) { it.clear() } }
+    }
+
     private fun updateOne(name: String): Unit {
         val prompt = name.replaceFirstChar { it.titlecase(Locale.getDefault()) } + ": "
         val value: Any? = if (name in SENSITIVE_FIELDS) {
@@ -162,8 +169,8 @@
     }
 
     private fun generatePassword(): Unit {
-        val newPassword = generate(length, allowSymbols)
-        if (verbose) {
+        val newPassword = generate(length, commandLine.hasOption(SYMBOLS))
+        if (commandLine.hasOption(VERBOSE)) {
             printPassword(newPassword)
         }
         addOne("password", newPassword)