annotate src/main/kotlin/name/blackcap/passman/CreateSubcommand.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 302d224bbd57
children ea65ab890f66
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.")
13
302d224bbd57 Improve help messages and csv error reportage.
David Barts <n5jrn@me.com>
parents: 10
diff changeset
20 addOption("l", LENGTH, true, "Length of generated password (default $DEFAULT_GENERATED_LENGTH).")
8
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)) {
9
72619175004e Fix issues found in testing.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
30 HelpFormatter().printHelp("$SHORTNAME create [options]", 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) {
10
cbe4c797c9a6 Fix bug in parsing --length
David Barts <n5jrn@me.com>
parents: 9
diff changeset
41 -1
cbe4c797c9a6 Fix bug in parsing --length
David Barts <n5jrn@me.com>
parents: 9
diff changeset
42 }
cbe4c797c9a6 Fix bug in parsing --length
David Barts <n5jrn@me.com>
parents: 9
diff changeset
43 if (length < MIN_GENERATED_LENGTH) {
3
eafa3779aef8 More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
44 die("${see(rawLength)} - invalid length")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 }
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
46 Entry.withGeneratedPassword(length,
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
47 commandLine.hasOption(SYMBOLS),
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
48 commandLine.hasOption(VERBOSE))
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 } else {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 Entry.withPromptedPassword()
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 val id = db.makeKey(entry.name)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
53
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 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
55 it.setLong(1, id)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 val result = it.executeQuery()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 result.next()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 val count = result.getInt(1)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 if (count > 0) {
3
eafa3779aef8 More bug fixes, quote strings in diagnostics.
David Barts <n5jrn@me.com>
parents: 0
diff changeset
60 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
61 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 }
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 try {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
65 if (entry.notes.isNullOrBlank()) {
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 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
67 .use {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 it.setLong(1, id)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 it.setEncryptedString(2, entry.name, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 it.setEncryptedString(3, entry.username, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 it.setEncrypted(4, entry.password, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 it.setLong(5, System.currentTimeMillis())
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 it.execute()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 } else {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 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
77 .use {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 it.setLong(1, db.makeKey(entry.name))
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 it.setEncryptedString(2, entry.name, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 it.setEncryptedString(3, entry.username, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 it.setEncrypted(4, entry.password, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 it.setEncryptedString(5, entry.notes, db.encryption)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 it.setLong(6, System.currentTimeMillis())
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 it.execute()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 } finally {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 entry.password.clear()
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 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
91
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 private fun checkArguments(): Unit {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 var bad = false
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
94 if (!commandLine.hasOption(GENERATE)) {
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
95 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
96 if (commandLine.hasOption(option)) {
8
698c4a3d758d Some code clean-up.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
97 error("--$option requires --$GENERATE")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 bad = true
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 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 if (bad) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
103 exitProcess(2);
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
104 }
9
72619175004e Fix issues found in testing.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
105 if (commandLine.args.isNotEmpty()) {
72619175004e Fix issues found in testing.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
106 die("unexpected trailing arguments", 2)
72619175004e Fix issues found in testing.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
107 }
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
108 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
109 }