From 5df752047f9f2987dc42d89bb1715c740d35a5df Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 30 Jun 2019 22:01:02 +0100 Subject: [PATCH] Add a version command-line option (#99) * [sbt] make name and version available within Scala Available as `thorp.BuildInfo._` * [core] ConfigOption Add Version option * [cli] ParseArgs add command-line options `-V` and `--version` * [core] ConfigQuery add showVersion query * [cli] Program shows version then exit * [cli] Main don't show "Done" --- build.sbt | 5 +++++ .../scala/net/kemitix/thorp/cli/Main.scala | 2 +- .../net/kemitix/thorp/cli/ParseArgs.scala | 3 +++ .../scala/net/kemitix/thorp/cli/Program.scala | 18 +++++++++++------- .../net/kemitix/thorp/core/ConfigOption.scala | 3 +++ .../net/kemitix/thorp/core/ConfigQuery.scala | 6 ++++++ project/plugins.sbt | 1 + 7 files changed, 30 insertions(+), 8 deletions(-) diff --git a/build.sbt b/build.sbt index e8d0759..32476db 100644 --- a/build.sbt +++ b/build.sbt @@ -67,6 +67,11 @@ lazy val cli = (project in file("cli")) .settings(applicationSettings) .settings(commandLineParsing) .settings(testDependencies) + .enablePlugins(BuildInfoPlugin) + .settings( + buildInfoKeys := Seq[BuildInfoKey](name, version), + buildInfoPackage := "thorp" + ) .settings(Seq( assemblyOption in assembly := ( assemblyOption in assembly).value diff --git a/cli/src/main/scala/net/kemitix/thorp/cli/Main.scala b/cli/src/main/scala/net/kemitix/thorp/cli/Main.scala index 890512a..af4dba3 100644 --- a/cli/src/main/scala/net/kemitix/thorp/cli/Main.scala +++ b/cli/src/main/scala/net/kemitix/thorp/cli/Main.scala @@ -13,7 +13,7 @@ object Main extends IOApp { .guaranteeCase { case Canceled => exitCaseLogger.warn("Interrupted") case Error(e) => exitCaseLogger.error(e.getMessage) - case Completed => exitCaseLogger.info("Done") + case Completed => IO.unit } } diff --git a/cli/src/main/scala/net/kemitix/thorp/cli/ParseArgs.scala b/cli/src/main/scala/net/kemitix/thorp/cli/ParseArgs.scala index f465451..843ca27 100644 --- a/cli/src/main/scala/net/kemitix/thorp/cli/ParseArgs.scala +++ b/cli/src/main/scala/net/kemitix/thorp/cli/ParseArgs.scala @@ -13,6 +13,9 @@ object ParseArgs { OParser.sequence( programName("thorp"), head("thorp"), + opt[Unit]('V', "version") + .action((_, cos) => ConfigOption.Version :: cos) + .text("Show version"), opt[String]('s', "source") .action((str, cos) => ConfigOption.Source(Paths.get(str)) :: cos) .text("Source directory to sync to destination"), diff --git a/cli/src/main/scala/net/kemitix/thorp/cli/Program.scala b/cli/src/main/scala/net/kemitix/thorp/cli/Program.scala index 43ebc27..529cfd4 100644 --- a/cli/src/main/scala/net/kemitix/thorp/cli/Program.scala +++ b/cli/src/main/scala/net/kemitix/thorp/cli/Program.scala @@ -11,13 +11,17 @@ trait Program { def apply(cliOptions: Seq[ConfigOption]): IO[ExitCode] = { implicit val logger: Logger = new PrintLogger() - for { - storageService <- defaultStorageService - actions <- Synchronise(storageService, defaultHashService, cliOptions).valueOrF(handleErrors) - events <- handleActions(UnversionedMirrorArchive.default(storageService), actions) - _ <- storageService.shutdown - _ <- SyncLogging.logRunFinished(events) - } yield ExitCode.Success + if (ConfigQuery.showVersion(cliOptions)) IO { + println(s"Thorp v${thorp.BuildInfo.version}") + ExitCode.Success + } else + for { + storageService <- defaultStorageService + actions <- Synchronise(storageService, defaultHashService, cliOptions).valueOrF(handleErrors) + events <- handleActions(UnversionedMirrorArchive.default(storageService), actions) + _ <- storageService.shutdown + _ <- SyncLogging.logRunFinished(events) + } yield ExitCode.Success } private def handleErrors(implicit logger: Logger): List[String] => IO[Stream[Action]] = { diff --git a/core/src/main/scala/net/kemitix/thorp/core/ConfigOption.scala b/core/src/main/scala/net/kemitix/thorp/core/ConfigOption.scala index b860a5f..87aea8f 100644 --- a/core/src/main/scala/net/kemitix/thorp/core/ConfigOption.scala +++ b/core/src/main/scala/net/kemitix/thorp/core/ConfigOption.scala @@ -10,6 +10,9 @@ sealed trait ConfigOption { } object ConfigOption { + case object Version extends ConfigOption { + override def update(config: Config): Config = config + } case class Source(path: Path) extends ConfigOption { override def update(config: Config): Config = config.copy(source = path.toFile) } diff --git a/core/src/main/scala/net/kemitix/thorp/core/ConfigQuery.scala b/core/src/main/scala/net/kemitix/thorp/core/ConfigQuery.scala index ba4765b..685b05c 100644 --- a/core/src/main/scala/net/kemitix/thorp/core/ConfigQuery.scala +++ b/core/src/main/scala/net/kemitix/thorp/core/ConfigQuery.scala @@ -2,6 +2,12 @@ package net.kemitix.thorp.core trait ConfigQuery { + def showVersion(configOptions: Seq[ConfigOption]): Boolean = + configOptions.exists { + case ConfigOption.Version => true + case _ => false + } + def ignoreUserOptions(configOptions: Seq[ConfigOption]): Boolean = configOptions.exists { case ConfigOption.IgnoreUserOptions => true diff --git a/project/plugins.sbt b/project/plugins.sbt index ae2407b..5a47dba 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,2 +1,3 @@ addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.3.2") addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.2.6") +addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0") \ No newline at end of file