comparison src/main/kotlin/name/blackcap/passman/UpdateSubcommand.kt @ 2:3c792ad36b3d

Can now update a password and read it back.
author David Barts <n5jrn@me.com>
date Sun, 11 Sep 2022 18:24:55 -0700
parents a6cfdffcaa94
children eafa3779aef8
comparison
equal deleted inserted replaced
1:8aacca5c2d53 2:3c792ad36b3d
95 } 95 }
96 } 96 }
97 97
98 private fun update(): Unit { 98 private fun update(): Unit {
99 updateOne("username") 99 updateOne("username")
100 updateOne("password") 100 if (generate) {
101 generatePassword()
102 } else {
103 updateOne("password")
104 }
101 updateOne("notes") 105 updateOne("notes")
102 if (fieldValues.isEmpty()) { 106 if (fieldValues.isEmpty()) {
103 error("no values changed") 107 error("no values changed")
104 return 108 return
105 } 109 }
106 110
107 db.connection.prepareStatement("update passwords set updated = ?, $fields where id = ?").use { stmt -> 111 db.connection.prepareStatement("update passwords set modified = ?, $fields where id = ?").use { stmt ->
108 stmt.setLong(1, System.currentTimeMillis()) 112 stmt.setLong(1, System.currentTimeMillis())
109 fieldValues.indices.forEach { fieldIndex -> 113 fieldValues.indices.forEach { fieldIndex ->
110 val fieldValue = fieldValues[fieldIndex] 114 val fieldValue = fieldValues[fieldIndex]
111 val columnIndex = fieldIndex + 2 115 val columnIndex = fieldIndex + 2
112 when (fieldValue) { 116 when (fieldValue) {
125 val prompt = name.replaceFirstChar { it.titlecase(Locale.getDefault()) } + ": " 129 val prompt = name.replaceFirstChar { it.titlecase(Locale.getDefault()) } + ": "
126 val value: Any? = if (name in SENSITIVE_FIELDS) { 130 val value: Any? = if (name in SENSITIVE_FIELDS) {
127 getPassword(prompt, verify = true) 131 getPassword(prompt, verify = true)
128 } else { 132 } else {
129 val rawValue = readLine(prompt) 133 val rawValue = readLine(prompt)
130 if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) { null } else { rawValue } 134 if (name in NULLABLE_FIELDS && rawValue == NULL_SPECIFIED) {
135 null
136 } else {
137 rawValue
138 }
131 } 139 }
132 140
133 val noChange = when (value) { 141 val noChange = when (value) {
134 is String -> value.isEmpty() 142 is String -> value.isEmpty()
135 is CharArray -> value.isEmpty() 143 is CharArray -> value.isEmpty()
137 } 145 }
138 if (noChange) { 146 if (noChange) {
139 return 147 return
140 } 148 }
141 149
150 addOne(name, value)
151 }
152
153 private fun addOne(name: String, value: Any?) {
142 if (fields.isNotEmpty()) { 154 if (fields.isNotEmpty()) {
143 fields.append(", ") 155 fields.append(", ")
144 } 156 }
145 fields.append(name) 157 fields.append(name)
146 fields.append(" = ?") 158 fields.append(" = ?")
147 fieldValues.add(value) 159 fieldValues.add(value)
148 } 160 }
149 161
162 private fun generatePassword(): Unit {
163 val newPassword = generate(length, allowSymbols)
164 if (verbose) {
165 printPassword(newPassword)
166 }
167 addOne("password", newPassword)
168 }
169
150 } 170 }