comparison src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 12:a38a2a1036c3

Add import subcommand.
author David Barts <n5jrn@me.com>
date Sun, 22 Jan 2023 09:22:53 -0800
parents c69665ff37d0
children 8f3ddebb4295
comparison
equal deleted inserted replaced
11:c69665ff37d0 12:a38a2a1036c3
1 package name.blackcap.passman 1 package name.blackcap.passman
2 2
3 import org.apache.commons.cli.* 3 import org.apache.commons.cli.*
4 import java.sql.ResultSet 4 import java.sql.ResultSet
5 import java.util.*
6 import kotlin.system.exitProcess 5 import kotlin.system.exitProcess
7 6
8 class MergeSubcommand(): Subcommand() { 7 class MergeSubcommand(): Subcommand() {
9 private companion object { 8 private companion object {
10 const val FORCE = "force" 9 const val FORCE = "force"
51 val results = stmt.executeQuery() 50 val results = stmt.executeQuery()
52 while (results.next()) { 51 while (results.next()) {
53 val otherEntry = makeEntry(results) 52 val otherEntry = makeEntry(results)
54 val thisEntry = getEntry(db, otherEntry.name) 53 val thisEntry = getEntry(db, otherEntry.name)
55 if (thisEntry == null) { 54 if (thisEntry == null) {
56 doInsert(otherEntry) 55 otherEntry.insert(db)
57 } else { 56 } else {
58 doCompare(thisEntry, otherEntry) 57 doCompare(thisEntry, otherEntry)
59 thisEntry.password.clear() 58 thisEntry.password.clear()
60 } 59 }
61 otherEntry.password.clear() 60 otherEntry.password.clear()
79 val results = stmt.executeQuery() 78 val results = stmt.executeQuery()
80 return if (results.next()) makeEntry(results) else null 79 return if (results.next()) makeEntry(results) else null
81 } 80 }
82 } 81 }
83 82
84 private fun doInsert(entry: Entry) {
85 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)")
86 .use {
87 it.setLong(1, db.makeKey(entry.name))
88 it.setEncryptedString(2, entry.name, db.encryption)
89 it.setEncryptedString(3, entry.username, db.encryption)
90 it.setEncrypted(4, entry.password, db.encryption)
91 it.setEncryptedString(5, entry.notes, db.encryption)
92 it.setLongOrNull(6, entry.created?.time)
93 it.setLongOrNull(7, entry.modified?.time)
94 it.setLongOrNull(8, entry.accessed?.time)
95 it.executeUpdate()
96 }
97 }
98
99 private fun doCompare(thisEntry: Entry, otherEntry: Entry) { 83 private fun doCompare(thisEntry: Entry, otherEntry: Entry) {
100 if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) { 84 if (otherEntry.modifiedOrCreated.after(thisEntry.modifiedOrCreated) && okToChange(thisEntry, otherEntry)) {
101 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use { 85 db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, modified = ? where id = ?").use {
102 it.setEncryptedString(1, otherEntry.name, db.encryption) 86 it.setEncryptedString(1, otherEntry.name, db.encryption)
103 it.setEncryptedString(2, otherEntry.username, db.encryption) 87 it.setEncryptedString(2, otherEntry.username, db.encryption)
108 it.executeUpdate() 92 it.executeUpdate()
109 } 93 }
110 } 94 }
111 } 95 }
112 96
113 private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean { 97 private fun okToChange(thisEntry: Entry, otherEntry: Entry): Boolean =
114 if (commandLine.hasOption(MergeSubcommand.FORCE)) { 98 commandLine.hasOption(FORCE) || askUserIfOkToOverwrite(thisEntry, otherEntry)
115 return true
116 }
117 val REDACTED = "(redacted)"
118 println("EXISTING ENTRY:")
119 thisEntry.printLong(REDACTED)
120 println()
121 println("NEWER ENTRY:")
122 otherEntry.printLong(REDACTED)
123 println()
124 val answer = name.blackcap.passman.readLine("OK to overwrite existing entry? ")
125 println()
126 return answer.trimStart().firstOrNull()?.uppercaseChar() in setOf('T', 'Y')
127 }
128
129 } 99 }