annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 package name.blackcap.passman
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
2
6
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
3 import org.apache.commons.cli.*
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
4 import kotlin.system.exitProcess
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
5
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 class ReadSubcommand(): Subcommand() {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
7 private companion object {
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
8 const val CLIPBOARD = "clipboard"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
9 const val HELP = "help"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
10 const val LONG = "long"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
11 const val ALT_SB = "\u001b[?1049h"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
12 const val NORM_SB = "\u001b[?1049l"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
13 const val CLEAR = "\u001b[H\u001b[2J"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
14 }
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 private lateinit var commandLine: CommandLine
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
16
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 override fun run(args: Array<String>) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 val options = Options().apply {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
19 addOption("c", CLIPBOARD, false, "Copy username and password into clipboard.")
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
20 addOption("h", HELP, false, "Print this help message.")
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
21 addOption("l", LONG, false, "Long format listing.")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 try {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 commandLine = DefaultParser().parse(options, args)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 } catch (e: ParseException) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 die(e.message ?: "syntax error", 2)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
28 if (commandLine.hasOption(HELP)) {
6
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
29 HelpFormatter().printHelp("$SHORTNAME read", options)
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
30 exitProcess(0)
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
31 }
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
32 if (commandLine.args.isEmpty()) {
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
33 error("expecting site name")
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
34 }
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
35 if (commandLine.args.size > 1) {
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
36 error("unexpected trailing arguments")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 val nameIn = commandLine.args[0];
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 val db = Database.open()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 val id = db.makeKey(nameIn)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
41
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 db.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 it.setLong(1, id)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 val result = it.executeQuery()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 if (!result.next()) {
3
eafa3779aef8 More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
46 die("no record matches ${see(nameIn)}")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 val entry = Entry(
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
49 name = result.getDecryptedString(1, db.encryption)!!,
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
50 username = result.getDecryptedString(2, db.encryption)!!,
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
51 password = result.getDecrypted(3, db.encryption)!!,
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 notes = result.getDecryptedString(4, db.encryption),
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 created = result.getDate(5),
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 modified = result.getDate(6),
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 accessed = result.getDate(7)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 )
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 try {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
58 print(ALT_SB + CLEAR)
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
59 val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null }
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
60 if (commandLine.hasOption(LONG)) {
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 entry.printLong(redaction)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 } else {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 entry.print(redaction)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
65 if (commandLine.hasOption(CLIPBOARD)) {
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 writeToClipboard(entry.password)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
68 name.blackcap.passman.readLine("Press ENTER to continue: ")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 } finally {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
70 print(CLEAR + NORM_SB)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 entry.password.clear()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
74
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 db.connection.prepareStatement("update passwords set accessed = ? where id = ?").use {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 it.setLong(1, System.currentTimeMillis())
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 it.setLong(2, id)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 it.execute()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 }
3
eafa3779aef8 More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
81 }