From e3675b5394804cfa17634ef6ea0b016aec679731 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 14 Jun 2019 20:00:22 +0100 Subject: [PATCH] Add a debug flag and make debug message hidden by default (#60) * [cli] add a debug flag to control logging * [core] show entering a directory as a debug message --- CHANGELOG.org | 6 ++++ README.org | 1 + .../scala/net/kemitix/s3thorp/cli/Main.scala | 8 ++--- .../net/kemitix/s3thorp/cli/ParseArgs.scala | 3 ++ .../net/kemitix/s3thorp/cli/PrintLogger.scala | 6 ++-- .../net/kemitix/s3thorp/cli/Program.scala | 2 +- .../kemitix/s3thorp/cli/ParseArgsTest.scala | 30 +++++++++++++++++-- .../LocalFileStream.scala | 2 +- .../net/kemitix/s3thorp/domain/Config.scala | 1 + 9 files changed, 49 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index 2a00078..32bebdd 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [[https://keepachangelog.com/en/1.0.0/][Keep a Changelog]], and this project adheres to [[https://semver.org/spec/v2.0.0.html][Semantic Versioning]]. +* [0.5.0] - ????-??-?? + +** Added + + - ~-d~, ~--debug~ flag for log messages + * [0.4.0] - 2019-06-11 ** Added diff --git a/README.org b/README.org index fa3cb63..f2dcb46 100644 --- a/README.org +++ b/README.org @@ -21,6 +21,7 @@ hash of the file contents. -p, --prefix Prefix within the S3 Bucket -i, --include Include matching paths -x, --exclude Exclude matching paths + -d, --debug Enable debug logging -v, --verbose Verbosity level (1-5) #+end_example diff --git a/cli/src/main/scala/net/kemitix/s3thorp/cli/Main.scala b/cli/src/main/scala/net/kemitix/s3thorp/cli/Main.scala index bfa42fd..31a487f 100644 --- a/cli/src/main/scala/net/kemitix/s3thorp/cli/Main.scala +++ b/cli/src/main/scala/net/kemitix/s3thorp/cli/Main.scala @@ -12,14 +12,14 @@ object Main extends IOApp { Config(source = Paths.get(".").toFile) override def run(args: List[String]): IO[ExitCode] = { - val logger = new PrintLogger[IO](1) + val exitCaseLogger = new PrintLogger[IO](1, false) ParseArgs(args, defaultConfig) .map(Program[IO]) .getOrElse(IO(ExitCode.Error)) .guaranteeCase { - case Canceled => logger.warn("Interrupted") - case Error(e) => logger.error(e.getMessage) - case Completed => logger.info("Done") + case Canceled => exitCaseLogger.warn("Interrupted") + case Error(e) => exitCaseLogger.error(e.getMessage) + case Completed => exitCaseLogger.info("Done") } } diff --git a/cli/src/main/scala/net/kemitix/s3thorp/cli/ParseArgs.scala b/cli/src/main/scala/net/kemitix/s3thorp/cli/ParseArgs.scala index d993ad7..b3405bb 100644 --- a/cli/src/main/scala/net/kemitix/s3thorp/cli/ParseArgs.scala +++ b/cli/src/main/scala/net/kemitix/s3thorp/cli/ParseArgs.scala @@ -35,6 +35,9 @@ object ParseArgs { .unbounded() .action((str,c) => c.copy(filters = c.filters ++ str.map(Exclude))) .text("Exclude matching paths"), + opt[Unit]('d', "debug") + .action((_, c) => c.copy(debug = true)) + .text("Enable debug logging"), opt[Int]('v', "verbose") .validate(i => if (i >= 1 && i <= 5) Right(Unit) diff --git a/cli/src/main/scala/net/kemitix/s3thorp/cli/PrintLogger.scala b/cli/src/main/scala/net/kemitix/s3thorp/cli/PrintLogger.scala index 53ad895..7d229c2 100644 --- a/cli/src/main/scala/net/kemitix/s3thorp/cli/PrintLogger.scala +++ b/cli/src/main/scala/net/kemitix/s3thorp/cli/PrintLogger.scala @@ -3,9 +3,11 @@ package net.kemitix.s3thorp.cli import cats.Monad import net.kemitix.s3thorp.domain.Logger -class PrintLogger[M[_]: Monad](verbosity: Int) extends Logger[M] { +class PrintLogger[M[_]: Monad](verbosity: Int, isDebug: Boolean) extends Logger[M] { - override def debug(message: => String): M[Unit] = Monad[M].pure(println(s"[ DEBUG] $message")) + override def debug(message: => String): M[Unit] = + if (isDebug) Monad[M].pure(println(s"[ DEBUG] $message")) + else Monad[M].unit override def info(message: => String): M[Unit] = Monad[M].pure(println(s"[ INFO] $message")) diff --git a/cli/src/main/scala/net/kemitix/s3thorp/cli/Program.scala b/cli/src/main/scala/net/kemitix/s3thorp/cli/Program.scala index 88b1883..b5bf248 100644 --- a/cli/src/main/scala/net/kemitix/s3thorp/cli/Program.scala +++ b/cli/src/main/scala/net/kemitix/s3thorp/cli/Program.scala @@ -10,7 +10,7 @@ import net.kemitix.s3thorp.domain.{Config, Logger} object Program { def apply[M[_]: Monad](config: Config): M[ExitCode] = { - implicit val logger: Logger[M] = new PrintLogger[M](config.verbose) + implicit val logger: Logger[M] = new PrintLogger[M](config.verbose, config.debug) for { _ <- logger.info("S3Thorp - hashed sync for s3") _ <- Sync.run[M](config, S3ClientBuilder.defaultClient) diff --git a/cli/src/test/scala/net/kemitix/s3thorp/cli/ParseArgsTest.scala b/cli/src/test/scala/net/kemitix/s3thorp/cli/ParseArgsTest.scala index 7b3cd8b..142d5b5 100644 --- a/cli/src/test/scala/net/kemitix/s3thorp/cli/ParseArgsTest.scala +++ b/cli/src/test/scala/net/kemitix/s3thorp/cli/ParseArgsTest.scala @@ -12,9 +12,8 @@ class ParseArgsTest extends FunSpec { val defaultConfig: Config = Config(source = source) describe("parse - source") { - def invokeWithSource(path: String) = { + def invokeWithSource(path: String) = ParseArgs(List("--source", path, "--bucket", "bucket"), defaultConfig) - } describe("when source is a directory") { val result = invokeWithSource(pathTo(".")) @@ -45,6 +44,33 @@ class ParseArgsTest extends FunSpec { } } + describe("parse - debug") { + def invokeWithDebug(debug: String) = { + val strings = List("--source", pathTo("."), "--bucket", "bucket", debug) + .filter(_ != "") + ParseArgs(strings, defaultConfig).map(_.debug) + } + + describe("when no debug flag") { + val debugFlag = invokeWithDebug("") + it("debug should be false") { + assert(debugFlag.contains(false)) + } + } + describe("when long debug flag") { + val debugFlag = invokeWithDebug("--debug") + it("debug should be true") { + assert(debugFlag.contains(true)) + } + } + describe("when short debug flag") { + val debugFlag = invokeWithDebug("-d") + it("debug should be true") { + assert(debugFlag.contains(true)) + } + } + } + private def pathTo(value: String): String = Try(Resource(this, value)) .map(_.getCanonicalPath) diff --git a/core/src/main/scala/net.kemitix.s3thorp.core/LocalFileStream.scala b/core/src/main/scala/net.kemitix.s3thorp.core/LocalFileStream.scala index 8694536..fd434f7 100644 --- a/core/src/main/scala/net.kemitix.s3thorp.core/LocalFileStream.scala +++ b/core/src/main/scala/net.kemitix.s3thorp.core/LocalFileStream.scala @@ -41,7 +41,7 @@ object LocalFileStream { .flatMap(lfs => acc.map(s => s ++ lfs))) for { - _ <- logger.info(s"- Entering: $file") + _ <- logger.debug(s"- Entering: $file") fs <- dirPaths(file) lfs <- recurse(fs) _ <- logger.debug(s"- Leaving : $file") diff --git a/domain/src/main/scala/net/kemitix/s3thorp/domain/Config.scala b/domain/src/main/scala/net/kemitix/s3thorp/domain/Config.scala index 7081f4a..3a86e4d 100644 --- a/domain/src/main/scala/net/kemitix/s3thorp/domain/Config.scala +++ b/domain/src/main/scala/net/kemitix/s3thorp/domain/Config.scala @@ -9,6 +9,7 @@ final case class Config( filters: List[Filter] = List(), multiPartThreshold: Long = 1024 * 1024 * 5, maxRetries: Int = 3, + debug: Boolean = false, source: File ) { require(source.isDirectory, s"Source must be a directory: $source")