Tidy Main by extracting Program (#58)
* [cli] ParseArgs now return an Option * [cli] Program extracted from Main
This commit is contained in:
parent
fffe156cc1
commit
69c09d0091
3 changed files with 40 additions and 37 deletions
|
@ -1,13 +1,9 @@
|
||||||
package net.kemitix.s3thorp.cli
|
package net.kemitix.s3thorp.cli
|
||||||
|
|
||||||
import java.io.File
|
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
|
|
||||||
import cats.effect.ExitCase.{Canceled, Completed, Error}
|
import cats.effect.ExitCase.{Canceled, Completed, Error}
|
||||||
import cats.effect.{ExitCode, IO, IOApp}
|
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
|
import net.kemitix.s3thorp.domain.Config
|
||||||
|
|
||||||
object Main extends IOApp {
|
object Main extends IOApp {
|
||||||
|
@ -15,28 +11,11 @@ object Main extends IOApp {
|
||||||
val defaultConfig: Config =
|
val defaultConfig: Config =
|
||||||
Config(source = Paths.get(".").toFile)
|
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] = {
|
override def run(args: List[String]): IO[ExitCode] = {
|
||||||
val logger = new Logger[IO](1)
|
val logger = new Logger[IO](1)
|
||||||
program(args)
|
ParseArgs(args, defaultConfig)
|
||||||
|
.map(Program[IO])
|
||||||
|
.getOrElse(IO(ExitCode.Error))
|
||||||
.guaranteeCase {
|
.guaranteeCase {
|
||||||
case Canceled => logger.warn("Interrupted")
|
case Canceled => logger.warn("Interrupted")
|
||||||
case Error(e) => logger.error(e.getMessage)
|
case Error(e) => logger.error(e.getMessage)
|
||||||
|
|
|
@ -2,19 +2,16 @@ package net.kemitix.s3thorp.cli
|
||||||
|
|
||||||
import java.nio.file.Paths
|
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.Filter.{Exclude, Include}
|
||||||
import net.kemitix.s3thorp.domain.{Bucket, Config, RemoteKey}
|
import net.kemitix.s3thorp.domain.{Bucket, Config, RemoteKey}
|
||||||
import scopt.OParser
|
import scopt.OParser
|
||||||
import scopt.OParser.{builder, parse, sequence}
|
|
||||||
|
|
||||||
object ParseArgs {
|
object ParseArgs {
|
||||||
|
|
||||||
val configParser: OParser[Unit, Config] = {
|
val configParser: OParser[Unit, Config] = {
|
||||||
val parserBuilder = builder[Config]
|
val parserBuilder = OParser.builder[Config]
|
||||||
import parserBuilder._
|
import parserBuilder._
|
||||||
sequence(
|
OParser.sequence(
|
||||||
programName("s3thorp"),
|
programName("s3thorp"),
|
||||||
head("s3thorp"),
|
head("s3thorp"),
|
||||||
opt[String]('s', "source")
|
opt[String]('s', "source")
|
||||||
|
@ -45,10 +42,7 @@ object ParseArgs {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
def apply(args: List[String], defaultConfig: Config): IO[Config] =
|
def apply(args: List[String], defaultConfig: Config): Option[Config] =
|
||||||
parse(configParser, args, defaultConfig) match {
|
OParser.parse(configParser, args, defaultConfig)
|
||||||
case Some(config) => IO.pure(config)
|
|
||||||
case _ => IO.raiseError(new IllegalArgumentException)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
30
cli/src/main/scala/net/kemitix/s3thorp/cli/Program.scala
Normal file
30
cli/src/main/scala/net/kemitix/s3thorp/cli/Program.scala
Normal 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue