Use lazy injection for openapi-generator CLI

This commit is contained in:
Niels van Velzen 2023-01-03 14:37:30 +01:00 committed by Max Rumpf
parent fcf130479a
commit fdb1d9ce1f
6 changed files with 24 additions and 19 deletions

View File

@ -2,11 +2,11 @@ package org.jellyfin.openapi
import org.jellyfin.openapi.cli.MainCommand
import org.jellyfin.openapi.hooks.hooksModule
import org.koin.dsl.koinApplication
import org.koin.core.context.startKoin
fun main(vararg args: String) {
// Start Koin
val koin = koinApplication { modules(mainModule, hooksModule) }.koin
val koin = startKoin { modules(mainModule, hooksModule) }.koin
// Invoke Clikt to run requested command
koin.get<MainCommand>().main(args)

View File

@ -69,7 +69,7 @@ val mainModule = module {
single { DescriptionBuilder(getAll()) }
// CLI
single { MainCommand(getAll(), getKoin()) }
single { GenerateCommand(get()) } bind CliktCommand::class
single { VerifyCommand(get()) } bind CliktCommand::class
single { MainCommand() }
single { GenerateCommand() } bind CliktCommand::class
single { VerifyCommand() } bind CliktCommand::class
}

View File

@ -0,0 +1,7 @@
package org.jellyfin.openapi.cli
import com.github.ajalt.clikt.core.CliktCommand
import org.koin.core.component.KoinComponent
@Suppress("UnnecessaryAbstractClass")
abstract class BaseCommand : CliktCommand(), KoinComponent

View File

@ -1,14 +1,14 @@
package org.jellyfin.openapi.cli
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file
import org.jellyfin.openapi.Generator
import org.koin.core.component.inject
class GenerateCommand : BaseCommand() {
private val generator by inject<Generator>()
class GenerateCommand(
private val generator: Generator,
) : CliktCommand() {
private val openApiFile by option(
"--openApiFile",
"-i",

View File

@ -5,12 +5,10 @@ import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import org.jellyfin.openapi.hooks.hooksModule
import org.koin.core.Koin
class MainCommand(
commands: List<CliktCommand>,
private val koin: Koin,
) : CliktCommand() {
class MainCommand : BaseCommand() {
private val commands by lazy { getKoin().getAll<CliktCommand>() }
private val noHooks by option(
"--no-hooks",
help = "Disable all hooks"
@ -21,6 +19,6 @@ class MainCommand(
}
override fun run() {
if (noHooks) koin.unloadModules(listOf(hooksModule))
if (noHooks) getKoin().unloadModules(listOf(hooksModule))
}
}

View File

@ -1,15 +1,15 @@
package org.jellyfin.openapi.cli
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.ProgramResult
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file
import org.jellyfin.openapi.Generator
import org.koin.core.component.inject
class VerifyCommand : BaseCommand() {
private val generator by inject<Generator>()
class VerifyCommand(
private val generator: Generator,
) : CliktCommand() {
private val openApiFile by option(
"--openApiFile",
"-i",