Mercurial > cgi-bin > hgweb.cgi > PassMan
diff src/main/kotlin/name/blackcap/passman/Main.kt @ 0:a6cfdffcaa94
Initial commit, incomplete but it runs sorta.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 11 Sep 2022 16:11:37 -0700 |
parents | |
children | eafa3779aef8 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/kotlin/name/blackcap/passman/Main.kt Sun Sep 11 16:11:37 2022 -0700 @@ -0,0 +1,45 @@ +package name.blackcap.passman + +import java.io.BufferedReader +import java.io.InputStreamReader +import java.util.* +import java.util.stream.Collectors +import kotlin.reflect.jvm.javaMethod +import kotlin.reflect.jvm.kotlinFunction +import kotlin.system.exitProcess + +fun main(args: Array<String>) { + if (args.isEmpty()) { + error("expecting subcommand") + exitProcess(2) + } + val subcommand = args[0]; + val scArgs = args.sliceArray(1 until args.size) + runSubcommand(subcommand, scArgs) +} + +fun runSubcommand(name: String, args: Array<String>): Unit { + val instance = getInstanceForClass(getClassForSubcommand(name)) + if (instance == null) { + die("$name - unknown subcommand", 2) + } else { + instance.run(args) + } +} + +fun getClassForSubcommand(name: String): Class<Subcommand>? = try { + val shortName = name.replace('-', '_') + .lowercase() + .replaceFirstChar { it.titlecase(Locale.getDefault()) } + Class.forName("$MAIN_PACKAGE.${shortName}Subcommand") as? Class<Subcommand> + /* val ret = Class.forName("$MAIN_PACKAGE.$shortName") + if (ret.isInstance(Subcommand::class.java)) { ret as Class<Subcommand> } else { null } */ +} catch (e: ClassNotFoundException) { + null +} + +fun getInstanceForClass(klass: Class<Subcommand>?) = try { + klass?.getDeclaredConstructor()?.newInstance() +} catch (e: ReflectiveOperationException) { + null +}