diff 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
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/CreateSubcommand.kt	Sun Jun 30 22:28:52 2024 -0700
+++ b/src/main/kotlin/name/blackcap/passman/CreateSubcommand.kt	Tue Jul 02 11:27:39 2024 -0700
@@ -1,7 +1,6 @@
 package name.blackcap.passman
 
 import org.apache.commons.cli.*
-import kotlin.system.exitProcess
 
 class CreateSubcommand(): Subcommand() {
     private companion object {
@@ -13,7 +12,7 @@
     }
     private lateinit var commandLine: CommandLine
 
-    override fun run(args: Array<String>) {
+    override fun run(args: Array<String>): {
         val options = Options().apply {
             addOption("g", GENERATE, false, "Use password generator.")
             addOption("h", HELP, false, "Print this help message.")
@@ -24,14 +23,14 @@
         try {
             commandLine = DefaultParser().parse(options, args)
         } catch (e: ParseException) {
-            die(e.message ?: "syntax error", 2)
+            throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
         }
         if (commandLine.hasOption(HELP)) {
             HelpFormatter().printHelp("$SHORTNAME create [options]", options)
-            exitProcess(0)
+            return
         }
         checkArguments()
-        val db = Database.open()
+        val db = Database.default
 
         val entry = if (commandLine.hasOption(GENERATE)) {
             val rawLength = commandLine.getOptionValue(LENGTH)
@@ -41,7 +40,7 @@
                 -1
             }
             if (length < MIN_GENERATED_LENGTH) {
-                die("${see(rawLength)} - invalid length")
+                throw SubcommandException(message = "${see(rawLength)} - invalid length")
             }
             Entry.withGeneratedPassword(length,
                 commandLine.hasOption(SYMBOLS),
@@ -57,7 +56,7 @@
             result.next()
             val count = result.getInt(1)
             if (count > 0) {
-                die("record matching ${see(entry.name)} already exists")
+                throw SubcommandException(message = "record matching ${see(entry.name)} already exists")
             }
         }
 
@@ -100,10 +99,10 @@
             }
         }
         if (bad) {
-            exitProcess(2);
+            throw SubcommandException(status = 2)
         }
         if (commandLine.args.isNotEmpty()) {
-            die("unexpected trailing arguments", 2)
+            throw SubcommandException(message = "unexpected trailing arguments", status = 2)
         }
     }
 }