refactor(command): refactor exit logic
This commit is contained in:
@@ -5,15 +5,21 @@ import command.CommandManager
|
||||
import io.ConsoleHandler
|
||||
|
||||
class AppExecutor {
|
||||
var interactiveMode = true
|
||||
|
||||
fun exec() {
|
||||
val io = ConsoleHandler()
|
||||
val manager = CommandManager()
|
||||
|
||||
AppInitializer().setup(manager, io)
|
||||
AppInitializer().setup(manager, io, this)
|
||||
|
||||
while (true) {
|
||||
while (interactiveMode) {
|
||||
val input = readln()
|
||||
manager.initCommand(input, io)
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
interactiveMode = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
package app
|
||||
|
||||
import command.commands.*
|
||||
import command.CommandManager
|
||||
import manager.CollectionManager
|
||||
import command.commands.Add
|
||||
import command.commands.Exit
|
||||
import io.IOHandler
|
||||
import manager.CollectionManager
|
||||
|
||||
class AppInitializer {
|
||||
fun setup(
|
||||
manager: CommandManager,
|
||||
io: IOHandler,
|
||||
app: AppExecutor,
|
||||
) {
|
||||
val collectionManager = CollectionManager(io)
|
||||
|
||||
manager.register(Add(io, collectionManager))
|
||||
manager.register(Exit(io))
|
||||
manager.register(Exit(io) { app.stop() })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package command
|
||||
|
||||
import command.commands.*
|
||||
import io.IOHandler
|
||||
|
||||
class CommandManager {
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package command.commands
|
||||
|
||||
import app.AppExecutor
|
||||
import command.Command
|
||||
import io.IOHandler
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
class Exit(
|
||||
private val io: IOHandler,
|
||||
private val stop: () -> Unit,
|
||||
) : Command {
|
||||
override val name = "exit"
|
||||
override val description = "exit"
|
||||
override val description = "stop app execution"
|
||||
|
||||
override fun execute(args: String?) {
|
||||
io.println("завершение процесса")
|
||||
exitProcess(0)
|
||||
stop()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user