Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def run(args: List[String]): IO[ExitCode] = {
- run0(args) >> IO.pure(ExitCode.Success)
- }
- def run0(args: List[String]): IO[RunResult] = {
- for {
- _ <- IO { println("Running with args: " + args.mkString(",")) }
- debug = args.contains("debug")
- runResult <- if (debug) debugLoop else IO.pure(Run)
- } yield runResult
- }
- def debugLoop: IO[Debug.type] =
- for {
- _ <- IO(println("Debug mode: exit application be pressing ENTER."))
- _ <- IO.shift(BlockingFileIO) // readLine might block for a long time so we shift to another thread
- input <- IO(StdIn.readLine()) // let it run until user presses return
- _ <- IO.shift(ExecutionContext.global) // shift back to main thread
- _ <- if (input == "b") {
- // do some debug relevant stuff
- IO(Unit) >> debugLoop
- } else {
- shutDown()
- }
- } yield Debug
- // shuts down everything
- def shutDown(): IO[Unit] = ???
- }
- import org.scalatest.FlatSpec
- class MainSpec extends FlatSpec {
- "Main" should "enter the debug loop if args contain 'debug'" in {
- val program: IO[RunResult] = Main.run0("debug" :: Nil)
- program match {
- case Debug => // do stuff
- case Run => // other stuff
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement