comparison src/main/kotlin/name/blackcap/passman/ImportSubcommand.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 7a74ae668665
children
comparison
equal deleted inserted replaced
20:4391afcf6bd0 21:ea65ab890f66
20 private lateinit var csvFile: String 20 private lateinit var csvFile: String
21 private var line = 0 21 private var line = 0
22 22
23 override fun run(args: Array<String>) { 23 override fun run(args: Array<String>) {
24 parseArguments(args) 24 parseArguments(args)
25 db = Database.open() 25 db = Database.default
26 try { 26 try {
27 doImport() 27 doImport()
28 } catch (e: IOException) { 28 } catch (e: IOException) {
29 die(e.message ?: "I/O error") 29 throw SubcommandException(message = e.message ?: "I/O error", cause = e)
30 } catch (e: CsvException) { 30 } catch (e: CsvException) {
31 val message = e.message ?: "CSV error" 31 val baseMessage = e.message ?: "CSV error"
32 die("line $line, $message") 32 throw SubcommandException(message = "line $line, $baseMessage", cause = e)
33 } 33 }
34 } 34 }
35 35
36 private fun parseArguments(args: Array<String>) { 36 private fun parseArguments(args: Array<String>) {
37 val params = parseInto("import", args, options) 37 val params = parseInto("import", args, options)
38 when (params.size) { 38 when (params.size) {
39 0 -> die("expecting CSV file name", 2) 39 0 -> throw SubcommandException(message = "expecting CSV file name", status = 2)
40 1 -> csvFile = params[0] 40 1 -> csvFile = params[0]
41 else -> die("unexpected trailing arguments", 2) 41 else -> throw SubcommandException(message = "unexpected trailing arguments", status = 2)
42 } 42 }
43 csvDateFormat = SimpleDateFormat(options.format).apply { 43 csvDateFormat = SimpleDateFormat(options.format).apply {
44 timeZone = TimeZone.getTimeZone(options.zone) 44 timeZone = TimeZone.getTimeZone(options.zone)
45 isLenient = false 45 isLenient = false
46 } 46 }
83 } 83 }
84 } 84 }
85 85
86 private fun fromCsv(fields: Array<String>): Entry { 86 private fun fromCsv(fields: Array<String>): Entry {
87 if (fields.size != NFIELDS) { 87 if (fields.size != NFIELDS) {
88 die("line $line, expected $NFIELDS fields but got ${fields.size}") 88 throw SubcommandException(message = "line $line, expected $NFIELDS fields but got ${fields.size}")
89 } 89 }
90 return Entry( 90 return Entry(
91 name = fields[0], 91 name = fields[0],
92 username = fields[1], 92 username = fields[1],
93 password = fields[2].toCharArray(), 93 password = fields[2].toCharArray(),
103 return null 103 return null
104 } 104 }
105 try { 105 try {
106 return csvDateFormat.parse(unparsed) 106 return csvDateFormat.parse(unparsed)
107 } catch (e: ParseException) { 107 } catch (e: ParseException) {
108 die("${see(unparsed)} - invalid date/time string") 108 throw SubcommandException(message = "${see(unparsed)} - invalid date/time string", cause = e)
109 throw e /* kotlin is too stupid to realize this never happens */
110 } 109 }
111 } 110 }
112 111
113 private fun okToChange(thisEntry: Entry?, otherEntry: Entry): Boolean = 112 private fun okToChange(thisEntry: Entry?, otherEntry: Entry): Boolean =
114 thisEntry == null || options.force || askUserIfOkToOverwrite(thisEntry, otherEntry) 113 thisEntry == null || options.force || askUserIfOkToOverwrite(thisEntry, otherEntry)