annotate src/main/kotlin/name/blackcap/passman/ImportSubcommand.kt @ 12:a38a2a1036c3

Add import subcommand.
author David Barts <n5jrn@me.com>
date Sun, 22 Jan 2023 09:22:53 -0800
parents
children 302d224bbd57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 package name.blackcap.passman
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
2
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 import com.opencsv.CSVParserBuilder
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 import com.opencsv.CSVReaderBuilder
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 import org.apache.commons.cli.*
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import java.io.FileReader
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import java.io.IOException
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import java.text.SimpleDateFormat
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import java.util.*
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import kotlin.system.exitProcess
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
11
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 class ImportSubcommand(): Subcommand() {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 private companion object {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 val CSV_DATE_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").apply {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 timeZone = TimeZone.getTimeZone("UTC")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 isLenient = false
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 const val ESCAPE = "escape"
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 const val FORCE = "force"
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 const val HELP = "help"
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 const val IGNORE = "ignore"
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 const val QUOTE = "quote"
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 const val SEPARATOR = "separator"
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 const val SKIP = "skip"
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 const val NFIELDS = 7
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
26
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 private lateinit var commandLine: CommandLine
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 private lateinit var db: Database
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
30
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 /* default option values */
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 private var escape = '\\'
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 private var quote = '"'
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 private var separator = ','
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
35
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 override fun run(args: Array<String>) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 parseArguments(args)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 db = Database.open()
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 try {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 doImport()
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 } catch (e: IOException) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 die(e.message ?: "I/O error")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
45
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 private fun parseArguments(args: Array<String>) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 val options = Options().apply {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 addOption("e", ImportSubcommand.ESCAPE, true, "CSV escape character (default $escape).")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 addOption("f", ImportSubcommand.FORCE, false, "Do not ask before overwriting.")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 addOption("h", ImportSubcommand.HELP, false, "Print this help message.")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 addOption("i", ImportSubcommand.IGNORE, false, "Ignore white space before quoted strings.")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 addOption("q", ImportSubcommand.QUOTE, true, "CSV string-quoting character (default $quote).")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 addOption("s", ImportSubcommand.SEPARATOR, true, "CSV separator character (default $separator).")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 addOption("k", ImportSubcommand.SKIP, false, "Skip first line of input.")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 try {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 commandLine = DefaultParser().parse(options, args)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 } catch (e: ParseException) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 die(e.message ?: "syntax error", 2)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 if (commandLine.hasOption(ImportSubcommand.HELP)) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 HelpFormatter().printHelp("$SHORTNAME merge [options] csv_file", options)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 exitProcess(0)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 if (commandLine.args.isEmpty()) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 die("expecting other CSV file name", 2)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 if (commandLine.args.size > 1) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 die("unexpected trailing arguments", 2)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 escape = getOptionChar(ImportSubcommand.ESCAPE, escape)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 quote = getOptionChar(ImportSubcommand.QUOTE, quote)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 separator = getOptionChar(ImportSubcommand.SEPARATOR, separator)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
75
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 private fun getOptionChar(optionName: String, defaultValue: Char): Char {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 val optionValue = commandLine.getOptionValue(optionName) ?: return defaultValue
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 val ret = optionValue.firstOrNull()
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 if (ret == null) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 die("--$optionName value must not be empty")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 return ret!!
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
84
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 private fun doImport() {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 val csvParser = CSVParserBuilder()
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 .withEscapeChar(escape)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 .withQuoteChar(quote)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 .withSeparator(separator)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 .withIgnoreLeadingWhiteSpace(commandLine.hasOption(ImportSubcommand.IGNORE))
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 .build()
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
92
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 val csvReader = CSVReaderBuilder(FileReader(commandLine.args[0]))
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 .withCSVParser(csvParser)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 .build()
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
96
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 csvReader.use {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 if (commandLine.hasOption(ImportSubcommand.SKIP)) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
99 it.skip(1)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 it.iterator().forEach { fields ->
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 val importedEntry = fromCsv(fields)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
103 val thisEntry = Entry.fromDatabase(db, importedEntry.name)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
104 try {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
105 if (okToChange(thisEntry, importedEntry)) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 if (thisEntry == null) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
107 importedEntry.insert(db)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
108 } else {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
109 importedEntry.update(db)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
110 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
111 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
112 } finally {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
113 thisEntry?.password?.clear()
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
114 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
115 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
116 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
117 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
118
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
119 private fun fromCsv(fields: Array<String>): Entry {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
120 if (fields.size != NFIELDS) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
121 die("expected $NFIELDS fields but got ${fields.size}")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
122 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
123 return Entry(
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
124 name = fields[0],
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
125 username = fields[1],
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
126 password = fields[2].toCharArray(),
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
127 notes = if (saysNull(fields[3])) null else fields[3],
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
128 created = parseCsvTime(fields[4]) ?: Date(),
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
129 modified = parseCsvTime(fields[5]),
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
130 accessed = parseCsvTime(fields[6])
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
131 )
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
132 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
133
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
134 private fun parseCsvTime(unparsed: String): Date? {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
135 if (saysNull(unparsed)) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
136 return null
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
137 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
138 try {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
139 return CSV_DATE_FORMAT.parse(unparsed)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
140 } catch (e: ParseException) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
141 die("${see(unparsed)} - invalid date/time string")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
142 throw e /* kotlin is too stupid to realize this never happens */
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
143 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
144 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
145
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
146 private fun okToChange(thisEntry: Entry?, otherEntry: Entry): Boolean =
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
147 thisEntry == null || commandLine.hasOption(FORCE) || askUserIfOkToOverwrite(thisEntry, otherEntry)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
148
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
149 private fun saysNull(string: String) = string.lowercase() == "null"
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
150
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
151 }