From d73f42e9ea419c031d5a5049ca80ed8e9cc6fb12 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 6 May 2019 11:28:44 +0100 Subject: [PATCH] [parseArgs] extract to object --- src/main/scala/net/kemitix/s3thorp/Main.scala | 27 +--------------- .../scala/net/kemitix/s3thorp/ParseArgs.scala | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 src/main/scala/net/kemitix/s3thorp/ParseArgs.scala diff --git a/src/main/scala/net/kemitix/s3thorp/Main.scala b/src/main/scala/net/kemitix/s3thorp/Main.scala index 16371ff..c429d18 100644 --- a/src/main/scala/net/kemitix/s3thorp/Main.scala +++ b/src/main/scala/net/kemitix/s3thorp/Main.scala @@ -5,31 +5,6 @@ import cats.effect.{ExitCode, IO, IOApp} object Main extends IOApp { - def parseArgs(args: List[String]): IO[Config] = { - import scopt.OParser - val builder = OParser.builder[Config] - val configParser: OParser[Unit, Config] = { - import builder._ - OParser.sequence( - programName("S3Thorp"), - head("s3thorp", "0.1.0"), - opt[String]('s', "source") - .action((str, c) => c.copy(source = str)), - opt[String]('b', "bucket") - .action((str, c) => c.copy(bucket = str)) - .text("S3 bucket name"), - opt[String]('p', "prefix") - .action((str, c) => c.copy(prefix = str)) - .text("Prefix within the S3 Bucket") - ) - } - val defaultConfig = Config("def-bucket", "def-prefix", "def-source") - OParser.parse(configParser, args, defaultConfig) match { - case Some(config) => IO.pure(config) - case _ => IO.raiseError(new IllegalArgumentException) - } - } - def putStrLn(value: String) = IO { println(value) } def sync(c: Config): IO[Unit] = @@ -40,7 +15,7 @@ object Main extends IOApp { def program(args: List[String]): IO[ExitCode] = for { - a <- parseArgs(args) + a <- ParseArgs(args) _ <- sync(a) } yield ExitCode.Success diff --git a/src/main/scala/net/kemitix/s3thorp/ParseArgs.scala b/src/main/scala/net/kemitix/s3thorp/ParseArgs.scala new file mode 100644 index 0000000..dac5509 --- /dev/null +++ b/src/main/scala/net/kemitix/s3thorp/ParseArgs.scala @@ -0,0 +1,32 @@ +package net.kemitix.s3thorp + +import cats.effect.IO + +object ParseArgs { + + def apply(args: List[String]): IO[Config] = { + import scopt.OParser + val builder = OParser.builder[Config] + val configParser: OParser[Unit, Config] = { + import builder._ + OParser.sequence( + programName("S3Thorp"), + head("s3thorp", "0.1.0"), + opt[String]('s', "source") + .action((str, c) => c.copy(source = str)), + opt[String]('b', "bucket") + .action((str, c) => c.copy(bucket = str)) + .text("S3 bucket name"), + opt[String]('p', "prefix") + .action((str, c) => c.copy(prefix = str)) + .text("Prefix within the S3 Bucket") + ) + } + val defaultConfig = Config("def-bucket", "def-prefix", "def-source") + OParser.parse(configParser, args, defaultConfig) match { + case Some(config) => IO.pure(config) + case _ => IO.raiseError(new IllegalArgumentException) + } + } + +}