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
This commit is contained in:
Paul Campbell 2019-08-01 22:39:52 +01:00 committed by GitHub
parent adbf8af2f3
commit e3b0260b6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 52 deletions

View file

@ -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)

View file

@ -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)
}

View file

@ -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"
}
}

View file

@ -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)
}
}