Mercurial > cgi-bin > hgweb.cgi > PassMan
annotate src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt @ 26:69526ae8c8de
Allow list --name to be abbreviated -n, since it is so commonly used.
author | David Barts <n5jrn@me.com> |
---|---|
date | Fri, 05 Jul 2024 10:11:31 -0700 |
parents | 131e39d96862 |
children |
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 | 6 private companion object { |
7 const val CLIPBOARD = "clipboard" | |
8 const val HELP = "help" | |
9 const val LONG = "long" | |
10 const val ALT_SB = "\u001b[?1049h" | |
11 const val NORM_SB = "\u001b[?1049l" | |
12 const val CLEAR = "\u001b[H\u001b[2J" | |
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 { |
25
131e39d96862
Fix erroneous help message in read subcommand.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
18 addOption("c", CLIPBOARD, false, "Copy password into clipboard.") |
8 | 19 addOption("h", HELP, false, "Print this help message.") |
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 | 27 if (commandLine.hasOption(HELP)) { |
11 | 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 | 39 val entry = Entry.fromDatabase(db, nameIn) |
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 | 42 } |
43 try { | |
44 print(ALT_SB + CLEAR) | |
45 val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null } | |
46 if (commandLine.hasOption(LONG)) { | |
47 entry.printLong(redaction) | |
48 } else { | |
49 entry.print(redaction) | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
50 } |
12 | 51 if (commandLine.hasOption(CLIPBOARD)) { |
52 writeToClipboard(entry.password) | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 } |
12 | 54 name.blackcap.passman.readLine("Press ENTER to continue: ") |
55 } finally { | |
56 print(CLEAR + NORM_SB) | |
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 | 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 } |