diff src/main/kotlin/name/blackcap/passman/ImportSubcommand.kt @ 13:302d224bbd57

Improve help messages and csv error reportage.
author David Barts <n5jrn@me.com>
date Tue, 24 Jan 2023 20:13:13 -0800
parents a38a2a1036c3
children 4dae7a15ee48
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/ImportSubcommand.kt	Sun Jan 22 09:22:53 2023 -0800
+++ b/src/main/kotlin/name/blackcap/passman/ImportSubcommand.kt	Tue Jan 24 20:13:13 2023 -0800
@@ -2,6 +2,7 @@
 
 import com.opencsv.CSVParserBuilder
 import com.opencsv.CSVReaderBuilder
+import com.opencsv.exceptions.CsvException
 import org.apache.commons.cli.*
 import java.io.FileReader
 import java.io.IOException
@@ -33,6 +34,8 @@
     private var quote = '"'
     private var separator = ','
 
+    private var line = 0
+
     override fun run(args: Array<String>) {
         parseArguments(args)
         db = Database.open()
@@ -40,6 +43,9 @@
             doImport()
         } catch (e: IOException) {
             die(e.message ?: "I/O error")
+        } catch (e: CsvException) {
+            val message = e.message ?: "CSV error"
+            die("line $line, $message")
         }
     }
 
@@ -96,9 +102,12 @@
 
         csvReader.use {
             if (commandLine.hasOption(ImportSubcommand.SKIP)) {
+                line++
                 it.skip(1)
             }
+
             it.iterator().forEach { fields ->
+                line++
                 val importedEntry = fromCsv(fields)
                 val thisEntry = Entry.fromDatabase(db, importedEntry.name)
                 try {
@@ -118,7 +127,7 @@
 
     private fun fromCsv(fields: Array<String>): Entry {
         if (fields.size != NFIELDS) {
-            die("expected $NFIELDS fields but got ${fields.size}")
+            die("line $line, expected $NFIELDS fields but got ${fields.size}")
         }
         return Entry(
             name = fields[0],
@@ -148,4 +157,4 @@
 
     private fun saysNull(string: String) = string.lowercase() == "null"
 
-}
\ No newline at end of file
+}