From 5a74c504da44a9dbea11da2129c2a72be8767d97 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Thu, 14 Mar 2024 13:35:40 +0100 Subject: Add check specific PR WIP --- .../at/hannibal2/skyhanni/changelog/Arguments.kt | 69 ++++++++++++++++++++++ .../skyhanni/changelog/CheckSpecificPR.kt | 13 ++++ 2 files changed, 82 insertions(+) create mode 100644 src/main/kotlin/at/hannibal2/skyhanni/changelog/Arguments.kt create mode 100644 src/main/kotlin/at/hannibal2/skyhanni/changelog/CheckSpecificPR.kt diff --git a/src/main/kotlin/at/hannibal2/skyhanni/changelog/Arguments.kt b/src/main/kotlin/at/hannibal2/skyhanni/changelog/Arguments.kt new file mode 100644 index 0000000..503de42 --- /dev/null +++ b/src/main/kotlin/at/hannibal2/skyhanni/changelog/Arguments.kt @@ -0,0 +1,69 @@ +package at.hannibal2.skyhanni.changelog + +class Arguments { + private val flags = mutableMapOf() + + sealed interface Flag + data class BooleanFlag(var set: Boolean) : Flag + data class StringFlag(var value: String?) : Flag + + + fun registerFlag(name: String, flag: T): T { + require(name.matches(Regex("^[a-z][a-z\\-]*$")) && !name.startsWith("no-")) + flags[name] = flag + return flag + } + + fun string(name: String): StringFlag { + return registerFlag(name, StringFlag(null)) + } + + fun bool(name: String, default: Boolean): BooleanFlag { + return registerFlag(name, BooleanFlag(default)) + } + + fun parseFlags(args: Array) { + var i = 0 + while (i in args.indices) { + val flag = args[i] + if (!flag.startsWith("--")) { + printHelp("Named argument provided: $flag") + } + val flagName = flag.substring(2) + if (flagName.startsWith("no-")) { + val boolFlag = flags[flagName.substring(3)] as? BooleanFlag + boolFlag ?: printHelp("Unknown flag $flag") + boolFlag.set = false + } else { + val flagObject = flags[flagName] + when (flagObject) { + is BooleanFlag -> flagObject.set = true + is StringFlag -> { + i++ + if(i !in args.indices) { printHelp("No argument provided for flag $flag") } + flagObject.value = args[i] + } + + null -> printHelp("Unknown flag $flag") + } + } + + i++ + } + } + + fun printHelp(errorMessage: String?): Nothing { + var help = "\n\n" + if (errorMessage != null) { + help += "ERROR: $errorMessage\n\n" + } + help += "Available flags:\n" + flags.forEach { (name, flag) -> + help += when (flag) { + is BooleanFlag -> " --$name, --no-$name\n" + is StringFlag -> " --$name \n" + } + } + error(help) + } +} \ No newline at end of file diff --git a/src/main/kotlin/at/hannibal2/skyhanni/changelog/CheckSpecificPR.kt b/src/main/kotlin/at/hannibal2/skyhanni/changelog/CheckSpecificPR.kt new file mode 100644 index 0000000..5a7ca3c --- /dev/null +++ b/src/main/kotlin/at/hannibal2/skyhanni/changelog/CheckSpecificPR.kt @@ -0,0 +1,13 @@ +package at.hannibal2.skyhanni.changelog + +object CheckSpecificPR { + @JvmStatic + fun main(args: Array) { + val arguments = Arguments() + val prNumArg = arguments.string("pr-num") + arguments.parseFlags(args) + val prNum = prNumArg.value?.toIntOrNull() ?: arguments.printHelp("--pr-num is required") + println("Investigating PR: $prNum") + // TODO: actually test specific PR + } +} \ No newline at end of file -- cgit