Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt @ 8:698c4a3d758d
Some code clean-up.
author | David Barts <n5jrn@me.com> |
---|---|
date | Fri, 23 Sep 2022 20:59:52 -0700 |
parents | 711cc42e96d7 |
children | 72619175004e |
comparison
equal
deleted
inserted
replaced
7:f245b9a53495 | 8:698c4a3d758d |
---|---|
2 | 2 |
3 import org.apache.commons.cli.* | 3 import org.apache.commons.cli.* |
4 import kotlin.system.exitProcess | 4 import kotlin.system.exitProcess |
5 | 5 |
6 class ReadSubcommand(): Subcommand() { | 6 class ReadSubcommand(): Subcommand() { |
7 private companion object { | |
8 const val CLIPBOARD = "clipboard" | |
9 const val HELP = "help" | |
10 const val LONG = "long" | |
11 const val ALT_SB = "\u001b[?1049h" | |
12 const val NORM_SB = "\u001b[?1049l" | |
13 const val CLEAR = "\u001b[H\u001b[2J" | |
14 } | |
7 private lateinit var commandLine: CommandLine | 15 private lateinit var commandLine: CommandLine |
8 | 16 |
9 override fun run(args: Array<String>) { | 17 override fun run(args: Array<String>) { |
10 val options = Options().apply { | 18 val options = Options().apply { |
11 addOption("c", "clipboard", false, "Copy username and password into clipboard.") | 19 addOption("c", CLIPBOARD, false, "Copy username and password into clipboard.") |
12 addOption("h", "help", false, "Print this help message.") | 20 addOption("h", HELP, false, "Print this help message.") |
13 addOption("l", "long", false, "Long format listing.") | 21 addOption("l", LONG, false, "Long format listing.") |
14 } | 22 } |
15 try { | 23 try { |
16 commandLine = DefaultParser().parse(options, args) | 24 commandLine = DefaultParser().parse(options, args) |
17 } catch (e: ParseException) { | 25 } catch (e: ParseException) { |
18 die(e.message ?: "syntax error", 2) | 26 die(e.message ?: "syntax error", 2) |
19 } | 27 } |
20 if (commandLine.hasOption("help")) { | 28 if (commandLine.hasOption(HELP)) { |
21 HelpFormatter().printHelp("$SHORTNAME read", options) | 29 HelpFormatter().printHelp("$SHORTNAME read", options) |
22 exitProcess(0) | 30 exitProcess(0) |
23 } | 31 } |
24 val clipboard = commandLine.hasOption("clipboard") | |
25 val long = commandLine.hasOption("long") | |
26 if (commandLine.args.isEmpty()) { | 32 if (commandLine.args.isEmpty()) { |
27 error("expecting site name") | 33 error("expecting site name") |
28 } | 34 } |
29 if (commandLine.args.size > 1) { | 35 if (commandLine.args.size > 1) { |
30 error("unexpected trailing arguments") | 36 error("unexpected trailing arguments") |
38 val result = it.executeQuery() | 44 val result = it.executeQuery() |
39 if (!result.next()) { | 45 if (!result.next()) { |
40 die("no record matches ${see(nameIn)}") | 46 die("no record matches ${see(nameIn)}") |
41 } | 47 } |
42 val entry = Entry( | 48 val entry = Entry( |
43 name = result.getDecryptedString(1, db.encryption), | 49 name = result.getDecryptedString(1, db.encryption)!!, |
44 username = result.getDecryptedString(2, db.encryption), | 50 username = result.getDecryptedString(2, db.encryption)!!, |
45 password = result.getDecrypted(3, db.encryption), | 51 password = result.getDecrypted(3, db.encryption)!!, |
46 notes = result.getDecryptedString(4, db.encryption), | 52 notes = result.getDecryptedString(4, db.encryption), |
47 created = result.getDate(5), | 53 created = result.getDate(5), |
48 modified = result.getDate(6), | 54 modified = result.getDate(6), |
49 accessed = result.getDate(7) | 55 accessed = result.getDate(7) |
50 ) | 56 ) |
51 try { | 57 try { |
52 val redaction = if (clipboard) { "(in clipboard)" } else { null } | 58 print(ALT_SB + CLEAR) |
53 if (long) { | 59 val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null } |
60 if (commandLine.hasOption(LONG)) { | |
54 entry.printLong(redaction) | 61 entry.printLong(redaction) |
55 } else { | 62 } else { |
56 entry.print(redaction) | 63 entry.print(redaction) |
57 } | 64 } |
58 if (clipboard) { | 65 if (commandLine.hasOption(CLIPBOARD)) { |
59 writeToClipboard(entry.password) | 66 writeToClipboard(entry.password) |
60 } | 67 } |
68 name.blackcap.passman.readLine("Press ENTER to continue: ") | |
61 } finally { | 69 } finally { |
70 print(CLEAR + NORM_SB) | |
62 entry.password.clear() | 71 entry.password.clear() |
63 } | 72 } |
64 } | 73 } |
65 | 74 |
66 db.connection.prepareStatement("update passwords set accessed = ? where id = ?").use { | 75 db.connection.prepareStatement("update passwords set accessed = ? where id = ?").use { |