Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison 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 |
comparison
equal
deleted
inserted
replaced
12:a38a2a1036c3 | 13:302d224bbd57 |
---|---|
1 package name.blackcap.passman | 1 package name.blackcap.passman |
2 | 2 |
3 import com.opencsv.CSVParserBuilder | 3 import com.opencsv.CSVParserBuilder |
4 import com.opencsv.CSVReaderBuilder | 4 import com.opencsv.CSVReaderBuilder |
5 import com.opencsv.exceptions.CsvException | |
5 import org.apache.commons.cli.* | 6 import org.apache.commons.cli.* |
6 import java.io.FileReader | 7 import java.io.FileReader |
7 import java.io.IOException | 8 import java.io.IOException |
8 import java.text.SimpleDateFormat | 9 import java.text.SimpleDateFormat |
9 import java.util.* | 10 import java.util.* |
31 /* default option values */ | 32 /* default option values */ |
32 private var escape = '\\' | 33 private var escape = '\\' |
33 private var quote = '"' | 34 private var quote = '"' |
34 private var separator = ',' | 35 private var separator = ',' |
35 | 36 |
37 private var line = 0 | |
38 | |
36 override fun run(args: Array<String>) { | 39 override fun run(args: Array<String>) { |
37 parseArguments(args) | 40 parseArguments(args) |
38 db = Database.open() | 41 db = Database.open() |
39 try { | 42 try { |
40 doImport() | 43 doImport() |
41 } catch (e: IOException) { | 44 } catch (e: IOException) { |
42 die(e.message ?: "I/O error") | 45 die(e.message ?: "I/O error") |
46 } catch (e: CsvException) { | |
47 val message = e.message ?: "CSV error" | |
48 die("line $line, $message") | |
43 } | 49 } |
44 } | 50 } |
45 | 51 |
46 private fun parseArguments(args: Array<String>) { | 52 private fun parseArguments(args: Array<String>) { |
47 val options = Options().apply { | 53 val options = Options().apply { |
94 .withCSVParser(csvParser) | 100 .withCSVParser(csvParser) |
95 .build() | 101 .build() |
96 | 102 |
97 csvReader.use { | 103 csvReader.use { |
98 if (commandLine.hasOption(ImportSubcommand.SKIP)) { | 104 if (commandLine.hasOption(ImportSubcommand.SKIP)) { |
105 line++ | |
99 it.skip(1) | 106 it.skip(1) |
100 } | 107 } |
108 | |
101 it.iterator().forEach { fields -> | 109 it.iterator().forEach { fields -> |
110 line++ | |
102 val importedEntry = fromCsv(fields) | 111 val importedEntry = fromCsv(fields) |
103 val thisEntry = Entry.fromDatabase(db, importedEntry.name) | 112 val thisEntry = Entry.fromDatabase(db, importedEntry.name) |
104 try { | 113 try { |
105 if (okToChange(thisEntry, importedEntry)) { | 114 if (okToChange(thisEntry, importedEntry)) { |
106 if (thisEntry == null) { | 115 if (thisEntry == null) { |
116 } | 125 } |
117 } | 126 } |
118 | 127 |
119 private fun fromCsv(fields: Array<String>): Entry { | 128 private fun fromCsv(fields: Array<String>): Entry { |
120 if (fields.size != NFIELDS) { | 129 if (fields.size != NFIELDS) { |
121 die("expected $NFIELDS fields but got ${fields.size}") | 130 die("line $line, expected $NFIELDS fields but got ${fields.size}") |
122 } | 131 } |
123 return Entry( | 132 return Entry( |
124 name = fields[0], | 133 name = fields[0], |
125 username = fields[1], | 134 username = fields[1], |
126 password = fields[2].toCharArray(), | 135 password = fields[2].toCharArray(), |