annotate src/main/kotlin/name/blackcap/passman/Main.kt @ 27:3a3067ba673b

Add idle-time detection to interactive mode, clean up imports.
author David Barts <n5jrn@me.com>
date Sat, 27 Jul 2024 09:50:54 -0700
parents 2188b2f13326
children 287eadf5ab30
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
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 import java.util.*
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 fun main(args: Array<String>) {
24
2188b2f13326 Make some minor tweaks to the help system.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
7 val NOPASSWORD = setOf<String>("help", "quit")
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 if (args.isEmpty()) {
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
9 openDatabase()
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
10 runInteractive()
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
11 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
12 val subcommand = args[0]
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
13 val scArgs = args.sliceArray(1 until args.size)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
14 if (subcommand !in NOPASSWORD) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
15 openDatabase()
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
16 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
17 runNonInteractive(subcommand, scArgs)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
18 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
19
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
20 fun openDatabase() {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
21 try {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
22 Database.default = Database.open()
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
23 } catch (e: DatabaseException) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
24 handleMessagedException(e)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 exitProcess(2)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 }
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
27 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
28
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
29 fun runNonInteractive(subcommand: String, scArgs: Array<String>): Unit {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
30 try {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
31 runSubcommand(subcommand, scArgs)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
32 } catch (e: SubcommandException) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
33 handleSubcommandException(e)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
34 exitProcess(e.status)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
35 } catch (e: MessagedException) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
36 handleMessagedException(e)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
37 exitProcess(1)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
38 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
39 exitProcess(0)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
40 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
41
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
42 fun runInteractive() {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
43 val DISALLOWED = setOf<String>("password")
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
44 val QUIT = setOf<String>("exit", "quit")
27
3a3067ba673b Add idle-time detection to interactive mode, clean up imports.
David Barts <n5jrn@me.com>
parents: 24
diff changeset
45 val MAX_TIME_MILLIS = 10L * 60L * 1000L
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
46 var lastStatus = 0
24
2188b2f13326 Make some minor tweaks to the help system.
David Barts <n5jrn@me.com>
parents: 23
diff changeset
47 println("This is PassMan interactive mode. Type help for help.")
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
48 while (true) {
27
3a3067ba673b Add idle-time detection to interactive mode, clean up imports.
David Barts <n5jrn@me.com>
parents: 24
diff changeset
49 val beforeRead = System.currentTimeMillis()
23
af86b8e0b88c Clean up exit output.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
50 val rawLine = System.console()?.readLine("passman> ")
af86b8e0b88c Clean up exit output.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
51 if (rawLine == null) {
af86b8e0b88c Clean up exit output.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
52 println() // ensure shell prompt comes out on a line of its own
af86b8e0b88c Clean up exit output.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
53 break
af86b8e0b88c Clean up exit output.
David Barts <n5jrn@me.com>
parents: 22
diff changeset
54 }
27
3a3067ba673b Add idle-time detection to interactive mode, clean up imports.
David Barts <n5jrn@me.com>
parents: 24
diff changeset
55 if (System.currentTimeMillis() - beforeRead > MAX_TIME_MILLIS) {
3a3067ba673b Add idle-time detection to interactive mode, clean up imports.
David Barts <n5jrn@me.com>
parents: 24
diff changeset
56 error("time limit exceeded, goodbye!")
3a3067ba673b Add idle-time detection to interactive mode, clean up imports.
David Barts <n5jrn@me.com>
parents: 24
diff changeset
57 lastStatus = 1
3a3067ba673b Add idle-time detection to interactive mode, clean up imports.
David Barts <n5jrn@me.com>
parents: 24
diff changeset
58 break
3a3067ba673b Add idle-time detection to interactive mode, clean up imports.
David Barts <n5jrn@me.com>
parents: 24
diff changeset
59 }
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
60 val s = Shplitter()
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
61 s.feed(rawLine)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
62 if (!s.complete) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
63 error("unterminated quoting")
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
64 lastStatus = 1
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
65 continue
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
66 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
67 val line = s.split().toList()
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
68 if (line.isEmpty()) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
69 continue
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
70 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
71 val subcommand = line.first()
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
72 val scArgs = line.drop(1).toTypedArray()
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
73 if (subcommand in QUIT) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
74 break
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
75 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
76 if (subcommand in DISALLOWED) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
77 error("$subcommand subcommand not allowed in interactive mode")
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
78 lastStatus = 2
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
79 continue
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
80 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
81 try {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
82 runSubcommand(subcommand, scArgs)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
83 lastStatus = 0
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
84 } catch(e: SubcommandException) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
85 handleSubcommandException(e)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
86 lastStatus = e.status
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
87 } catch(e: MessagedException) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
88 handleMessagedException(e)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
89 lastStatus = 1
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
90 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
91 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
92 exitProcess(lastStatus)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
94
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 fun runSubcommand(name: String, args: Array<String>): Unit {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 val instance = getInstanceForClass(getClassForSubcommand(name))
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 if (instance == null) {
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
98 throw SubcommandException(message = "${see(name)} - unknown subcommand", status = 2)
0
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
99 } else {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 instance.run(args)
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 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
103
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
104 fun getClassForSubcommand(name: String): Class<Subcommand>? = try {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
105 val shortName = name.replace('-', '_')
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 .lowercase()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
107 .replaceFirstChar { it.titlecase(Locale.getDefault()) }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
108 Class.forName("$MAIN_PACKAGE.${shortName}Subcommand") as? Class<Subcommand>
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
109 } catch (e: ClassNotFoundException) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
110 null
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
111 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
112
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
113 fun getInstanceForClass(klass: Class<Subcommand>?) = try {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
114 klass?.getDeclaredConstructor()?.newInstance()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
115 } catch (e: ReflectiveOperationException) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
116 null
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
117 }
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
118
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
119 fun handleMessagedException(e: MessagedException) = error(e.message)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
120
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
121 fun handleSubcommandException(e: SubcommandException): Unit {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
122 if (e.message != null) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
123 error(e.message!!)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
124 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
125 }