Mercurial > cgi-bin > hgweb.cgi > PassMan
annotate src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt @ 19:7d80cbcb67bb
add shlex-style splitter and tests
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 30 Jun 2024 20:37:36 -0700 |
parents | a38a2a1036c3 |
children | ea65ab890f66 |
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 | 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 } | |
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 | 19 addOption("c", CLIPBOARD, false, "Copy username and password into clipboard.") |
20 addOption("h", HELP, false, "Print this help message.") | |
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 | 28 if (commandLine.hasOption(HELP)) { |
11 | 29 HelpFormatter().printHelp("$SHORTNAME read [options] name", options) |
6
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()) { |
9 | 33 die("expecting site name", 2) |
6
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) { |
9 | 36 die("unexpected trailing arguments", 2) |
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() |
12 | 40 val entry = Entry.fromDatabase(db, nameIn) |
41 if (entry == null) { | |
42 die("no record matches ${see(nameIn)}") | |
43 return // Kotlin is too stupid to realize we never get here | |
44 } | |
45 try { | |
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) | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
52 } |
12 | 53 if (commandLine.hasOption(CLIPBOARD)) { |
54 writeToClipboard(entry.password) | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
55 } |
12 | 56 name.blackcap.passman.readLine("Press ENTER to continue: ") |
57 } finally { | |
58 print(CLEAR + NORM_SB) | |
59 entry.password.clear() | |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
60 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
61 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
62 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
|
63 it.setLong(1, System.currentTimeMillis()) |
12 | 64 it.setLong(2, db.makeKey(nameIn)) |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
65 it.execute() |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
66 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
67 } |
3
eafa3779aef8
More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
68 } |