From e3b0260b6dd035da4c0bcdf8baaac51914be34bb Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 1 Aug 2019 22:39:52 +0100 Subject: [PATCH] Refactor ThorpArchive.logEvent(StorageQueueEvent) (#141) * [core] ThorpArchive Refactoring * [core] ThorpArchive Refactoring * [core] ThorpArchive refactored * [core] ThorpArchive convert output to ConsoleOut.WithBatchMode messages * [console] Console rename putMessageLnB to overload putMessageLn --- build.sbt | 7 +-- .../net/kemitix/thorp/console/Console.scala | 5 ++ .../kemitix/thorp/console/ConsoleOut.scala | 49 +++++++++++++++ .../net/kemitix/thorp/core/ThorpArchive.scala | 63 +++++-------------- 4 files changed, 72 insertions(+), 52 deletions(-) diff --git a/build.sbt b/build.sbt index abf65be..d3c0a2b 100644 --- a/build.sbt +++ b/build.sbt @@ -57,9 +57,8 @@ val zioDependencies = Seq( ) ) -// cli -> thorp-lib -> storage-aws -> core -> storage-api -> console -> domain -// storage-api -> config -> domain -// config -> filesystem +// cli -> thorp-lib -> storage-aws -> core -> storage-api -> console -> config -> domain +// storage-api -> config -> filesystem lazy val thorp = (project in file(".")) .settings(commonSettings) @@ -114,7 +113,7 @@ lazy val console = (project in file("console")) .settings(commonSettings) .settings(zioDependencies) .settings(assemblyJarName in assembly := "console.jar") - .dependsOn(domain) + .dependsOn(config) lazy val config = (project in file("config")) .settings(commonSettings) diff --git a/console/src/main/scala/net/kemitix/thorp/console/Console.scala b/console/src/main/scala/net/kemitix/thorp/console/Console.scala index a692214..3d365f9 100644 --- a/console/src/main/scala/net/kemitix/thorp/console/Console.scala +++ b/console/src/main/scala/net/kemitix/thorp/console/Console.scala @@ -3,6 +3,7 @@ package net.kemitix.thorp.console import java.io.PrintStream import java.util.concurrent.atomic.AtomicReference +import net.kemitix.thorp.config.Config import zio.{UIO, ZIO} import scala.{Console => SConsole} @@ -60,4 +61,8 @@ object Console { final def putMessageLn(line: ConsoleOut): ZIO[Console, Nothing, Unit] = ZIO.accessM(_.console putStrLn line) + final def putMessageLn( + line: ConsoleOut.WithBatchMode): ZIO[Console with Config, Nothing, Unit] = + ZIO.accessM(line() >>= _.console.putStrLn) + } diff --git a/console/src/main/scala/net/kemitix/thorp/console/ConsoleOut.scala b/console/src/main/scala/net/kemitix/thorp/console/ConsoleOut.scala index 81f9689..dfb3935 100644 --- a/console/src/main/scala/net/kemitix/thorp/console/ConsoleOut.scala +++ b/console/src/main/scala/net/kemitix/thorp/console/ConsoleOut.scala @@ -1,11 +1,28 @@ package net.kemitix.thorp.console +import net.kemitix.thorp.config.Config +import net.kemitix.thorp.domain.StorageQueueEvent.Action +import net.kemitix.thorp.domain.Terminal._ import net.kemitix.thorp.domain.{Bucket, RemoteKey, Sources} +import zio.{UIO, ZIO} + +import scala.io.AnsiColor._ sealed trait ConsoleOut { def en: String } + object ConsoleOut { + + sealed trait WithBatchMode { + def en: String + def enBatch: String + def apply(): ZIO[Config, Nothing, String] = + Config.batchMode >>= selectLine + private def selectLine(batchMode: Boolean) = + if (batchMode) UIO(enBatch) else UIO(en) + } + case class ValidConfig( bucket: Bucket, prefix: RemoteKey, @@ -18,4 +35,36 @@ object ConsoleOut { s"Source: $sourcesList") .mkString(", ") } + + case class UploadComplete(remoteKey: RemoteKey) + extends ConsoleOut.WithBatchMode { + override def en: String = + s"${GREEN}Uploaded:$RESET ${remoteKey.key}$eraseToEndOfScreen" + override def enBatch: String = + s"Uploaded: ${remoteKey.key}" + } + + case class CopyComplete(sourceKey: RemoteKey, targetKey: RemoteKey) + extends ConsoleOut.WithBatchMode { + override def en: String = + s"${GREEN}Copied:$RESET ${sourceKey.key} => ${targetKey.key}$eraseToEndOfScreen" + override def enBatch: String = + s"Copied: ${sourceKey.key} => ${targetKey.key}" + } + + case class DeleteComplete(remoteKey: RemoteKey) + extends ConsoleOut.WithBatchMode { + override def en: String = + s"${GREEN}Deleted:$RESET ${remoteKey.key}$eraseToEndOfScreen" + override def enBatch: String = + s"Deleted: $remoteKey" + } + + case class ErrorQueueEventOccurred(action: Action, e: Throwable) + extends ConsoleOut.WithBatchMode { + override def en: String = + s"${action.name} failed: ${action.keys}: ${e.getMessage}" + override def enBatch: String = + s"${RED}ERROR:$RESET ${action.name} ${action.keys}: ${e.getMessage}$eraseToEndOfScreen" + } } diff --git a/core/src/main/scala/net/kemitix/thorp/core/ThorpArchive.scala b/core/src/main/scala/net/kemitix/thorp/core/ThorpArchive.scala index f2adf35..f415cb7 100644 --- a/core/src/main/scala/net/kemitix/thorp/core/ThorpArchive.scala +++ b/core/src/main/scala/net/kemitix/thorp/core/ThorpArchive.scala @@ -1,21 +1,17 @@ package net.kemitix.thorp.core import net.kemitix.thorp.config.Config +import net.kemitix.thorp.console.ConsoleOut.{ + CopyComplete, + DeleteComplete, + ErrorQueueEventOccurred, + UploadComplete +} import net.kemitix.thorp.console._ import net.kemitix.thorp.domain.StorageQueueEvent -import net.kemitix.thorp.domain.StorageQueueEvent.{ - CopyQueueEvent, - DeleteQueueEvent, - DoNothingQueueEvent, - ErrorQueueEvent, - ShutdownQueueEvent, - UploadQueueEvent -} -import net.kemitix.thorp.domain.Terminal._ +import net.kemitix.thorp.domain.StorageQueueEvent._ import net.kemitix.thorp.storage.api.Storage -import zio.TaskR - -import scala.io.AnsiColor._ +import zio.{TaskR, ZIO} trait ThorpArchive { @@ -26,47 +22,18 @@ trait ThorpArchive { ): TaskR[Storage with Console with Config, StorageQueueEvent] def logEvent( - event: StorageQueueEvent - ): TaskR[Console with Config, StorageQueueEvent] = + event: StorageQueueEvent): TaskR[Console with Config, StorageQueueEvent] = event match { case UploadQueueEvent(remoteKey, _) => - for { - batchMode <- Config.batchMode - _ <- TaskR.when(batchMode)( - Console.putStrLn(s"Uploaded: ${remoteKey.key}")) - _ <- TaskR.when(!batchMode)( - Console.putStrLn( - s"${GREEN}Uploaded:$RESET ${remoteKey.key}$eraseToEndOfScreen")) - } yield event + ZIO(event) <* Console.putMessageLn(UploadComplete(remoteKey)) case CopyQueueEvent(sourceKey, targetKey) => - for { - batchMode <- Config.batchMode - _ <- TaskR.when(batchMode)( - Console.putStrLn(s"Copied: ${sourceKey.key} => ${targetKey.key}")) - _ <- TaskR.when(!batchMode)( - Console.putStrLn( - s"${GREEN}Copied:$RESET ${sourceKey.key} => ${targetKey.key}$eraseToEndOfScreen") - ) - } yield event + ZIO(event) <* Console.putMessageLn(CopyComplete(sourceKey, targetKey)) case DeleteQueueEvent(remoteKey) => - for { - batchMode <- Config.batchMode - _ <- TaskR.when(batchMode)(Console.putStrLn(s"Deleted: $remoteKey")) - _ <- TaskR.when(!batchMode)( - Console.putStrLn( - s"${GREEN}Deleted:$RESET ${remoteKey.key}$eraseToEndOfScreen")) - } yield event + ZIO(event) <* Console.putMessageLn(DeleteComplete(remoteKey)) case ErrorQueueEvent(action, _, e) => - for { - batchMode <- Config.batchMode - _ <- TaskR.when(batchMode)( - Console.putStrLn( - s"${action.name} failed: ${action.keys}: ${e.getMessage}")) - _ <- TaskR.when(!batchMode)(Console.putStrLn( - s"${RED}ERROR:$RESET ${action.name} ${action.keys}: ${e.getMessage}$eraseToEndOfScreen")) - } yield event - case DoNothingQueueEvent(_) => TaskR(event) - case ShutdownQueueEvent() => TaskR(event) + ZIO(event) <* Console.putMessageLn(ErrorQueueEventOccurred(action, e)) + case DoNothingQueueEvent(_) => ZIO(event) + case ShutdownQueueEvent() => ZIO(event) } }