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(applicationSettings)
.settings(commandLineParsing) .settings(commandLineParsing)
.settings(testDependencies) .settings(testDependencies)
.enablePlugins(BuildInfoPlugin)
.settings(
buildInfoKeys := Seq[BuildInfoKey](name, version),
buildInfoPackage := "thorp"
)
.settings(Seq( .settings(Seq(
assemblyOption in assembly := ( assemblyOption in assembly := (
assemblyOption in assembly).value assemblyOption in assembly).value

View file

@ -13,7 +13,7 @@ object Main extends IOApp {
.guaranteeCase { .guaranteeCase {
case Canceled => exitCaseLogger.warn("Interrupted") case Canceled => exitCaseLogger.warn("Interrupted")
case Error(e) => exitCaseLogger.error(e.getMessage) 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( OParser.sequence(
programName("thorp"), programName("thorp"),
head("thorp"), head("thorp"),
opt[Unit]('V', "version")
.action((_, cos) => ConfigOption.Version :: cos)
.text("Show version"),
opt[String]('s', "source") opt[String]('s', "source")
.action((str, cos) => ConfigOption.Source(Paths.get(str)) :: cos) .action((str, cos) => ConfigOption.Source(Paths.get(str)) :: cos)
.text("Source directory to sync to destination"), .text("Source directory to sync to destination"),

View file

@ -11,6 +11,10 @@ trait Program {
def apply(cliOptions: Seq[ConfigOption]): IO[ExitCode] = { def apply(cliOptions: Seq[ConfigOption]): IO[ExitCode] = {
implicit val logger: Logger = new PrintLogger() implicit val logger: Logger = new PrintLogger()
if (ConfigQuery.showVersion(cliOptions)) IO {
println(s"Thorp v${thorp.BuildInfo.version}")
ExitCode.Success
} else
for { for {
storageService <- defaultStorageService storageService <- defaultStorageService
actions <- Synchronise(storageService, defaultHashService, cliOptions).valueOrF(handleErrors) actions <- Synchronise(storageService, defaultHashService, cliOptions).valueOrF(handleErrors)

View file

@ -10,6 +10,9 @@ sealed trait ConfigOption {
} }
object ConfigOption { object ConfigOption {
case object Version extends ConfigOption {
override def update(config: Config): Config = config
}
case class Source(path: Path) extends ConfigOption { case class Source(path: Path) extends ConfigOption {
override def update(config: Config): Config = config.copy(source = path.toFile) override def update(config: Config): Config = config.copy(source = path.toFile)
} }

View file

@ -2,6 +2,12 @@ package net.kemitix.thorp.core
trait ConfigQuery { trait ConfigQuery {
def showVersion(configOptions: Seq[ConfigOption]): Boolean =
configOptions.exists {
case ConfigOption.Version => true
case _ => false
}
def ignoreUserOptions(configOptions: Seq[ConfigOption]): Boolean = def ignoreUserOptions(configOptions: Seq[ConfigOption]): Boolean =
configOptions.exists { configOptions.exists {
case ConfigOption.IgnoreUserOptions => true case ConfigOption.IgnoreUserOptions => true

View file

@ -1,2 +1,3 @@
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.3.2") addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.3.2")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.2.6") addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.2.6")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")