annotate src/main/kotlin/name/blackcap/passman/Main.kt @ 28:287eadf5ab30 default tip

Check for timeouts inside subcommands while in interactive mode as well.
author David Barts <n5jrn@me.com>
date Wed, 31 Jul 2024 11:21:18 -0700
parents 3a3067ba673b
children
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 }
28
287eadf5ab30 Check for timeouts inside subcommands while in interactive mode as well.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
81 val beforeRun = System.currentTimeMillis()
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
82 try {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
83 runSubcommand(subcommand, scArgs)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
84 lastStatus = 0
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
85 } catch(e: SubcommandException) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
86 handleSubcommandException(e)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
87 lastStatus = e.status
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
88 } catch(e: MessagedException) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
89 handleMessagedException(e)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
90 lastStatus = 1
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
91 }
28
287eadf5ab30 Check for timeouts inside subcommands while in interactive mode as well.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
92 if (System.currentTimeMillis() - beforeRun > MAX_TIME_MILLIS) {
287eadf5ab30 Check for timeouts inside subcommands while in interactive mode as well.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
93 error("time limit exceeded, goodbye!")
287eadf5ab30 Check for timeouts inside subcommands while in interactive mode as well.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
94 lastStatus = 1
287eadf5ab30 Check for timeouts inside subcommands while in interactive mode as well.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
95 break
287eadf5ab30 Check for timeouts inside subcommands while in interactive mode as well.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
96 }
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
97 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
98 exitProcess(lastStatus)
0
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 fun runSubcommand(name: String, args: Array<String>): Unit {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 val instance = getInstanceForClass(getClassForSubcommand(name))
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
103 if (instance == null) {
22
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
104 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
105 } else {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 instance.run(args)
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
107 }
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
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
110 fun getClassForSubcommand(name: String): Class<Subcommand>? = try {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
111 val shortName = name.replace('-', '_')
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
112 .lowercase()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
113 .replaceFirstChar { it.titlecase(Locale.getDefault()) }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
114 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
115 } catch (e: ClassNotFoundException) {
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 }
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
118
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
119 fun getInstanceForClass(klass: Class<Subcommand>?) = try {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
120 klass?.getDeclaredConstructor()?.newInstance()
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
121 } catch (e: ReflectiveOperationException) {
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
122 null
a6cfdffcaa94 Initial commit, incomplete but it runs sorta.
David Barts <n5jrn@me.com>
parents:
diff changeset
123 }
22
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 fun handleMessagedException(e: MessagedException) = error(e.message)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
126
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
127 fun handleSubcommandException(e: SubcommandException): Unit {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
128 if (e.message != null) {
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
129 error(e.message!!)
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
130 }
07406c4af4a5 More interactive mode stuff.
David Barts <n5jrn@me.com>
parents: 21
diff changeset
131 }