Mercurial > cgi-bin > hgweb.cgi > PassMan
annotate src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt @ 6:711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
author | David Barts <n5jrn@me.com> |
---|---|
date | Tue, 20 Sep 2022 20:52:21 -0700 |
parents | eafa3779aef8 |
children | 698c4a3d758d |
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() { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
7 private lateinit var commandLine: CommandLine |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
9 override fun run(args: Array<String>) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
10 val options = Options().apply { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
11 addOption("c", "clipboard", false, "Copy username and password into clipboard.") |
6
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
12 addOption("h", "help", false, "Print this help message.") |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
13 addOption("l", "long", false, "Long format listing.") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
14 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
15 try { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
16 commandLine = DefaultParser().parse(options, args) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 } catch (e: ParseException) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 die(e.message ?: "syntax error", 2) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
19 } |
6
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
20 if (commandLine.hasOption("help")) { |
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
21 HelpFormatter().printHelp("$SHORTNAME read", options) |
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
22 exitProcess(0) |
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
23 } |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
24 val clipboard = commandLine.hasOption("clipboard") |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
25 val long = commandLine.hasOption("long") |
6
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
26 if (commandLine.args.isEmpty()) { |
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
27 error("expecting site name") |
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
28 } |
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
29 if (commandLine.args.size > 1) { |
711cc42e96d7
Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
30 error("unexpected trailing arguments") |
0
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
31 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
32 val nameIn = commandLine.args[0]; |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
33 val db = Database.open() |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
34 val id = db.makeKey(nameIn) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
35 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
36 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
|
37 it.setLong(1, id) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
38 val result = it.executeQuery() |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
39 if (!result.next()) { |
3
eafa3779aef8
More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
40 die("no record matches ${see(nameIn)}") |
0
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 val entry = Entry( |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
43 name = result.getDecryptedString(1, db.encryption), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
44 username = result.getDecryptedString(2, db.encryption), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
45 password = result.getDecrypted(3, db.encryption), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
46 notes = result.getDecryptedString(4, db.encryption), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
47 created = result.getDate(5), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
48 modified = result.getDate(6), |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 accessed = result.getDate(7) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
50 ) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
51 try { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
52 val redaction = if (clipboard) { "(in clipboard)" } else { null } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 if (long) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
54 entry.printLong(redaction) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
55 } else { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
56 entry.print(redaction) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
57 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
58 if (clipboard) { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
59 writeToClipboard(entry.password) |
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 } finally { |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
62 entry.password.clear() |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
63 } |
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 |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
66 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
|
67 it.setLong(1, System.currentTimeMillis()) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
68 it.setLong(2, id) |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
69 it.execute() |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
70 } |
a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
71 } |
3
eafa3779aef8
More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
72 } |