annotate src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt @ 24:2188b2f13326

Make some minor tweaks to the help system.
author David Barts <n5jrn@me.com>
date Wed, 03 Jul 2024 17:14:22 -0700
parents ea65ab890f66
children 131e39d96862
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.*
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
4
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 class ReadSubcommand(): Subcommand() {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
6 private companion object {
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
7 const val CLIPBOARD = "clipboard"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
8 const val HELP = "help"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
9 const val LONG = "long"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
10 const val ALT_SB = "\u001b[?1049h"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
11 const val NORM_SB = "\u001b[?1049l"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
12 const val CLEAR = "\u001b[H\u001b[2J"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
13 }
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 private lateinit var commandLine: CommandLine
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
15
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 override fun run(args: Array<String>) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 val options = Options().apply {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
18 addOption("c", CLIPBOARD, false, "Copy username and password into clipboard.")
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
19 addOption("h", HELP, false, "Print this help message.")
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
20 addOption("l", LONG, false, "Long format listing.")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 try {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 commandLine = DefaultParser().parse(options, args)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 } catch (e: ParseException) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
25 throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
27 if (commandLine.hasOption(HELP)) {
11
c69665ff37d0 Add merge subcommand (untested).
David Barts <n5jrn@me.com>
parents: 9
diff changeset
28 HelpFormatter().printHelp("$SHORTNAME read [options] name", options)
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
29 return
6
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
30 }
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
31 if (commandLine.args.isEmpty()) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
32 throw SubcommandException(message = "expecting site name", status = 2)
6
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
33 }
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
34 if (commandLine.args.size > 1) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
35 throw SubcommandException(message = "unexpected trailing arguments", status = 2)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 val nameIn = commandLine.args[0];
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
38 val db = Database.default
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
39 val entry = Entry.fromDatabase(db, nameIn)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
40 if (entry == null) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 12
diff changeset
41 throw SubcommandException(message = "no record matches ${see(nameIn)}")
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
42 }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
43 try {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
44 print(ALT_SB + CLEAR)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
45 val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null }
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
46 if (commandLine.hasOption(LONG)) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
47 entry.printLong(redaction)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
48 } else {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
49 entry.print(redaction)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 }
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
51 if (commandLine.hasOption(CLIPBOARD)) {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
52 writeToClipboard(entry.password)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 }
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
54 name.blackcap.passman.readLine("Press ENTER to continue: ")
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
55 } finally {
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
56 print(CLEAR + NORM_SB)
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
57 entry.password.clear()
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
59
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 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
61 it.setLong(1, System.currentTimeMillis())
12
a38a2a1036c3 Add import subcommand.
David Barts <n5jrn@me.com>
parents: 11
diff changeset
62 it.setLong(2, db.makeKey(nameIn))
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 it.execute()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 }
3
eafa3779aef8 More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
66 }