annotate src/main/kotlin/name/blackcap/passman/CreateSubcommand.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.*
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 import kotlin.system.exitProcess
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 CreateSubcommand(): 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 GENERATE = "generate"
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 LENGTH = "length"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
11 const val SYMBOLS = "symbols"
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
12 const val VERBOSE = "verbose"
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("g", GENERATE, false, "Use password generator.")
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", LENGTH, true, "Length of generated password.")
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
21 addOption("s", SYMBOLS, false, "Use symbol characters in generated password.")
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
22 addOption("v", VERBOSE, false, "Print the generated password.")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 try {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 commandLine = DefaultParser().parse(options, args)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 } catch (e: ParseException) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 die(e.message ?: "syntax error", 2)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
29 if (commandLine.hasOption(HELP)) {
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
30 HelpFormatter().printHelp("$SHORTNAME create", options)
6
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
31 exitProcess(0)
711cc42e96d7 Got the list subcommand working, but needs efficiency improvements.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
32 }
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 checkArguments()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 val db = Database.open()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
35
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
36 val entry = if (commandLine.hasOption(GENERATE)) {
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
37 val rawLength = commandLine.getOptionValue(LENGTH)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 val length = try {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 rawLength?.toInt() ?: DEFAULT_GENERATED_LENGTH
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 } catch (e: NumberFormatException) {
3
eafa3779aef8 More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
41 die("${see(rawLength)} - invalid length")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 -1 /* will never happen */
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
44 Entry.withGeneratedPassword(length,
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
45 commandLine.hasOption(SYMBOLS),
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
46 commandLine.hasOption(VERBOSE))
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 } else {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 Entry.withPromptedPassword()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 val id = db.makeKey(entry.name)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
51
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 db.connection.prepareStatement("select count(*) from passwords where id = ?").use {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 it.setLong(1, id)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 val result = it.executeQuery()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 result.next()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 val count = result.getInt(1)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 if (count > 0) {
3
eafa3779aef8 More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
58 die("record matching ${see(entry.name)} already exists")
0
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 }
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 try {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
63 if (entry.notes.isNullOrBlank()) {
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 db.connection.prepareStatement("insert into passwords (id, name, username, password, created) values (?, ?, ?, ?, ?)")
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 .use {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 it.setLong(1, id)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 it.setEncryptedString(2, entry.name, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 it.setEncryptedString(3, entry.username, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 it.setEncrypted(4, entry.password, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 it.setLong(5, System.currentTimeMillis())
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 it.execute()
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 } else {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created) values (?, ?, ?, ?, ?, ?)")
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 .use {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 it.setLong(1, db.makeKey(entry.name))
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 it.setEncryptedString(2, entry.name, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 it.setEncryptedString(3, entry.username, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 it.setEncrypted(4, entry.password, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 it.setEncryptedString(5, entry.notes, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 it.setLong(6, System.currentTimeMillis())
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 it.execute()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 } finally {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 entry.password.clear()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
89
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 private fun checkArguments(): Unit {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 var bad = false
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
92 if (!commandLine.hasOption(GENERATE)) {
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
93 for (option in listOf<String>(LENGTH, SYMBOLS, VERBOSE)) {
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 if (commandLine.hasOption(option)) {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
95 error("--$option requires --$GENERATE")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 bad = true
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
99 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 if (commandLine.args.isNotEmpty()) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 error("unexpected trailing arguments")
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
103 if (bad) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
104 exitProcess(2);
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
105 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
107 }