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

Add import subcommand.
author David Barts <n5jrn@me.com>
date Sun, 22 Jan 2023 09:22:53 -0800
parents c69665ff37d0
children ea65ab890f66
comparison
equal deleted inserted replaced
11:c69665ff37d0 12:a38a2a1036c3
35 if (commandLine.args.size > 1) { 35 if (commandLine.args.size > 1) {
36 die("unexpected trailing arguments", 2) 36 die("unexpected trailing arguments", 2)
37 } 37 }
38 val nameIn = commandLine.args[0]; 38 val nameIn = commandLine.args[0];
39 val db = Database.open() 39 val db = Database.open()
40 val id = db.makeKey(nameIn) 40 val entry = Entry.fromDatabase(db, nameIn)
41 41 if (entry == null) {
42 db.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use { 42 die("no record matches ${see(nameIn)}")
43 it.setLong(1, id) 43 return // Kotlin is too stupid to realize we never get here
44 val result = it.executeQuery() 44 }
45 if (!result.next()) { 45 try {
46 die("no record matches ${see(nameIn)}") 46 print(ALT_SB + CLEAR)
47 val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null }
48 if (commandLine.hasOption(LONG)) {
49 entry.printLong(redaction)
50 } else {
51 entry.print(redaction)
47 } 52 }
48 val entry = Entry( 53 if (commandLine.hasOption(CLIPBOARD)) {
49 name = result.getDecryptedString(1, db.encryption)!!, 54 writeToClipboard(entry.password)
50 username = result.getDecryptedString(2, db.encryption)!!,
51 password = result.getDecrypted(3, db.encryption)!!,
52 notes = result.getDecryptedString(4, db.encryption),
53 created = result.getDate(5),
54 modified = result.getDate(6),
55 accessed = result.getDate(7)
56 )
57 try {
58 print(ALT_SB + CLEAR)
59 val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null }
60 if (commandLine.hasOption(LONG)) {
61 entry.printLong(redaction)
62 } else {
63 entry.print(redaction)
64 }
65 if (commandLine.hasOption(CLIPBOARD)) {
66 writeToClipboard(entry.password)
67 }
68 name.blackcap.passman.readLine("Press ENTER to continue: ")
69 } finally {
70 print(CLEAR + NORM_SB)
71 entry.password.clear()
72 } 55 }
56 name.blackcap.passman.readLine("Press ENTER to continue: ")
57 } finally {
58 print(CLEAR + NORM_SB)
59 entry.password.clear()
73 } 60 }
74 61
75 db.connection.prepareStatement("update passwords set accessed = ? where id = ?").use { 62 db.connection.prepareStatement("update passwords set accessed = ? where id = ?").use {
76 it.setLong(1, System.currentTimeMillis()) 63 it.setLong(1, System.currentTimeMillis())
77 it.setLong(2, id) 64 it.setLong(2, db.makeKey(nameIn))
78 it.execute() 65 it.execute()
79 } 66 }
80 } 67 }
81 } 68 }