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"
This commit is contained in:
Paul Campbell 2019-06-30 22:01:02 +01:00 committed by GitHub
parent 561966218c
commit 5df752047f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 8 deletions

View file

@ -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

View file

@ -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
}
}

View file

@ -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"),

View file

@ -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]] = {

View file

@ -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)
}

View file

@ -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

View file

@ -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")