Tidy Main by extracting Program (#58)

* [cli] ParseArgs now return an Option

* [cli] Program extracted from Main
This commit is contained in:
Paul Campbell 2019-06-14 13:02:38 +01:00 committed by GitHub
parent fffe156cc1
commit 69c09d0091
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 37 deletions

View file

@ -1,13 +1,9 @@
package net.kemitix.s3thorp.cli
import java.io.File
import java.nio.file.Paths
import cats.effect.ExitCase.{Canceled, Completed, Error}
import cats.effect.{ExitCode, IO, IOApp}
import net.kemitix.s3thorp.core.MD5HashGenerator.md5File
import net.kemitix.s3thorp.aws.lib.S3ClientBuilder
import net.kemitix.s3thorp.core.Sync
import net.kemitix.s3thorp.domain.Config
object Main extends IOApp {
@ -15,28 +11,11 @@ object Main extends IOApp {
val defaultConfig: Config =
Config(source = Paths.get(".").toFile)
def program(args: List[String]): IO[ExitCode] =
for {
config <- ParseArgs(args, defaultConfig)
logger = new Logger[IO](config.verbose)
info = (l: Int) => (m: String) => logger.info(l)(m)
_ <- logger.info(1)("S3Thorp - hashed sync for s3")
_ <- Sync.run[IO](
config,
S3ClientBuilder.defaultClient,
hashGenerator(info),
info,
w => logger.warn(w))
} yield ExitCode.Success
private def hashGenerator(info: Int => String => IO[Unit]) = {
implicit val logInfo: Int => String => IO[Unit] = info
file: File => md5File[IO](file)
}
override def run(args: List[String]): IO[ExitCode] = {
val logger = new Logger[IO](1)
program(args)
ParseArgs(args, defaultConfig)
.map(Program[IO])
.getOrElse(IO(ExitCode.Error))
.guaranteeCase {
case Canceled => logger.warn("Interrupted")
case Error(e) => logger.error(e.getMessage)

View file

@ -2,19 +2,16 @@ package net.kemitix.s3thorp.cli
import java.nio.file.Paths
import cats.effect.IO
import net.kemitix.s3thorp._
import net.kemitix.s3thorp.domain.Filter.{Exclude, Include}
import net.kemitix.s3thorp.domain.{Bucket, Config, RemoteKey}
import scopt.OParser
import scopt.OParser.{builder, parse, sequence}
object ParseArgs {
val configParser: OParser[Unit, Config] = {
val parserBuilder = builder[Config]
val parserBuilder = OParser.builder[Config]
import parserBuilder._
sequence(
OParser.sequence(
programName("s3thorp"),
head("s3thorp"),
opt[String]('s', "source")
@ -45,10 +42,7 @@ object ParseArgs {
)
}
def apply(args: List[String], defaultConfig: Config): IO[Config] =
parse(configParser, args, defaultConfig) match {
case Some(config) => IO.pure(config)
case _ => IO.raiseError(new IllegalArgumentException)
}
def apply(args: List[String], defaultConfig: Config): Option[Config] =
OParser.parse(configParser, args, defaultConfig)
}

View file

@ -0,0 +1,30 @@
package net.kemitix.s3thorp.cli
import java.io.File
import cats.Monad
import cats.implicits._
import cats.effect.ExitCode
import net.kemitix.s3thorp.aws.lib.S3ClientBuilder
import net.kemitix.s3thorp.core.MD5HashGenerator.md5File
import net.kemitix.s3thorp.core.{MD5HashGenerator, Sync}
import net.kemitix.s3thorp.domain.Config
object Program {
def apply[M[_]: Monad](config: Config): M[ExitCode] = {
val logger = new Logger[M](config.verbose)
val info = (l: Int) => (m: String) => logger.info(l) (m)
val warn = (w: String) => logger.warn(w)
for {
_ <- info(1)("S3Thorp - hashed sync for s3")
_ <- Sync.run[M](config, S3ClientBuilder.defaultClient, hashGenerator(info), info, warn)
} yield ExitCode.Success
}
private def hashGenerator[M[_]: Monad](info: Int => String => M[Unit]) = {
implicit val logInfo: Int => String => M[Unit] = info
file: File => md5File[M](file)
}
}