Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. def run(args: List[String]): IO[ExitCode] = {
  2. run0(args) >> IO.pure(ExitCode.Success)
  3. }
  4.  
  5. def run0(args: List[String]): IO[RunResult] = {
  6. for {
  7. _ <- IO { println("Running with args: " + args.mkString(",")) }
  8. debug = args.contains("debug")
  9. runResult <- if (debug) debugLoop else IO.pure(Run)
  10. } yield runResult
  11. }
  12.  
  13. def debugLoop: IO[Debug.type] =
  14. for {
  15. _ <- IO(println("Debug mode: exit application be pressing ENTER."))
  16. _ <- IO.shift(BlockingFileIO) // readLine might block for a long time so we shift to another thread
  17. input <- IO(StdIn.readLine()) // let it run until user presses return
  18. _ <- IO.shift(ExecutionContext.global) // shift back to main thread
  19. _ <- if (input == "b") {
  20. // do some debug relevant stuff
  21. IO(Unit) >> debugLoop
  22. } else {
  23. shutDown()
  24. }
  25. } yield Debug
  26.  
  27. // shuts down everything
  28. def shutDown(): IO[Unit] = ???
  29. }
  30.  
  31. import org.scalatest.FlatSpec
  32.  
  33. class MainSpec extends FlatSpec {
  34.  
  35. "Main" should "enter the debug loop if args contain 'debug'" in {
  36. val program: IO[RunResult] = Main.run0("debug" :: Nil)
  37. program match {
  38. case Debug => // do stuff
  39. case Run => // other stuff
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement