comparison src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt @ 9:72619175004e

Fix issues found in testing.
author David Barts <n5jrn@me.com>
date Sat, 01 Oct 2022 09:57:23 -0700
parents 698c4a3d758d
children cbe4c797c9a6
comparison
equal deleted inserted replaced
8:698c4a3d758d 9:72619175004e
48 commandLine = DefaultParser().parse(options, args) 48 commandLine = DefaultParser().parse(options, args)
49 } catch (e: ParseException) { 49 } catch (e: ParseException) {
50 die(e.message ?: "syntax error", 2) 50 die(e.message ?: "syntax error", 2)
51 } 51 }
52 if (commandLine.hasOption(HELP)) { 52 if (commandLine.hasOption(HELP)) {
53 HelpFormatter().printHelp("$SHORTNAME update", options) 53 HelpFormatter().printHelp("$SHORTNAME update [options] name", options)
54 exitProcess(0) 54 exitProcess(0)
55 } 55 }
56 checkArguments() 56 checkArguments()
57 db = Database.open() 57 db = Database.open()
58 nameIn = commandLine.args[0] 58 nameIn = commandLine.args[0]
75 error("--$option requires --$GENERATE") 75 error("--$option requires --$GENERATE")
76 bad = true 76 bad = true
77 } 77 }
78 } 78 }
79 } 79 }
80 if (bad) {
81 exitProcess(2);
82 }
80 if (commandLine.args.isEmpty()) { 83 if (commandLine.args.isEmpty()) {
81 error("expecting site name") 84 die("expecting site name", 2)
82 } 85 }
83 if (commandLine.args.size > 1) { 86 if (commandLine.args.size > 1) {
84 error("unexpected trailing arguments") 87 die("unexpected trailing arguments", 2)
85 }
86 if (bad) {
87 exitProcess(2);
88 } 88 }
89 } 89 }
90 90
91 private fun checkDatabase(): Unit { 91 private fun checkDatabase(): Unit {
92 db.connection.prepareStatement("select count(*) from passwords where id = ?").use { 92 db.connection.prepareStatement("select count(*) from passwords where id = ?").use {
129 stmt.execute() 129 stmt.execute()
130 } 130 }
131 } 131 }
132 132
133 private fun cleanUp(): Unit { 133 private fun cleanUp(): Unit {
134 fieldValues.forEach { if (it is CharArray) { it.clear() } } 134 fieldValues.forEach {
135 if (it is CharArray) {
136 it.clear()
137 }
138 }
135 } 139 }
136 140
137 private fun updateOne(name: String): Unit { 141 private fun updateOne(name: String): Unit {
138 val prompt = name.replaceFirstChar { it.titlecase(Locale.getDefault()) } + ": " 142 val prompt = name.replaceFirstChar { it.titlecase(Locale.getDefault()) } + ": "
139 val value: Any? = if (name in SENSITIVE_FIELDS) { 143 val value: Any? = if (name in SENSITIVE_FIELDS) {
140 getPassword(prompt, verify = true) 144 updatePassword()
141 } else { 145 } else {
142 val rawValue = readLine(prompt) 146 val rawValue = readLine(prompt)
143 if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) { 147 if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) {
144 null 148 null
145 } else { 149 } else {
174 printPassword(newPassword) 178 printPassword(newPassword)
175 } 179 }
176 addOne("password", newPassword) 180 addOne("password", newPassword)
177 } 181 }
178 182
183 private fun updatePassword(): CharArray {
184 while (true) {
185 val pw1 = getPassword("Password: ")
186 if (pw1.isEmpty()) {
187 return pw1
188 }
189 val pw2 = getPassword("Verification: ")
190 if (pw1 contentEquals pw2) {
191 return pw1
192 }
193 error("mismatch, try again")
194 }
195 }
179 } 196 }