Skip to content

Commit 430b2b8

Browse files
committed
Added a project frame for the command line interface.
It parses the command line arguments, and currently accepts a test command and a server option (which is required, but defaults to the environment variable DELPHI_SERVER). Closes #1
1 parent a4f5620 commit 430b2b8

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

build.sbt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name := "delphi-cli"
2+
3+
version := "1.0.0-SNAPSHOT"
4+
5+
scalaVersion := "2.12.4"
6+
7+
libraryDependencies += "com.github.scopt" %% "scopt" % "3.7.0"
8+
libraryDependencies ++= Seq(
9+
"com.typesafe.akka" %% "akka-http-core" % "10.0.11"
10+
)
11+
12+
lazy val root = (project in file(".")).
13+
enablePlugins(JavaAppPackaging).
14+
enablePlugins(DockerPlugin).
15+
enablePlugins(BuildInfoPlugin).
16+
settings(
17+
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),
18+
buildInfoPackage := "de.upb.cs.swt.delphi.cli"
19+
)
20+

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 0.13.17

project/plugins.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.7.0")
2+
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.2")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package de.upb.cs.swt.delphi.cli
2+
3+
/**
4+
* Represents the implementation of a command of the CLI
5+
*/
6+
trait Command {
7+
8+
/**
9+
* Executes the command implementation
10+
* @param config The current configuration for the command
11+
*/
12+
def execute(config: Config) : Unit
13+
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package de.upb.cs.swt.delphi.cli
2+
3+
/**
4+
* Represents a configuration for the Delphi CLI
5+
* @param server A server base uri (Defaults to env variable DELPHI_SERVER)
6+
* @param verbose Marker if logging should be verbose
7+
* @param mode The command to be run
8+
*/
9+
case class Config (server : String = sys.env.getOrElse("DELPHI_SERVER", ""), verbose: Boolean = false, mode : String = "") {
10+
11+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package de.upb.cs.swt.delphi.cli
2+
3+
4+
/**
5+
* The application class for the Delphi command line interface
6+
*/
7+
object DelphiCLI extends App {
8+
9+
val cliParser = {
10+
new scopt.OptionParser[Config]("delphi-cli") {
11+
head("Delphi Command Line Tool", s"(${BuildInfo.version})")
12+
13+
14+
version("version").text("Prints the version of the command line tool.")
15+
16+
help("help").text("Prints this help text.")
17+
override def showUsageOnError = true
18+
19+
opt[String]("server").action( (x,c) => c.copy(server = x)).text("The url to the Delphi server")
20+
checkConfig(c => if (c.server.isEmpty()) failure("Option server is required.") else success)
21+
22+
cmd("test").action((_,c) => c.copy(mode = "test"))
23+
//cmd("search")
24+
//cmd("retrieve")
25+
}
26+
}
27+
28+
29+
cliParser.parse(args, Config()) match {
30+
case Some(config) =>
31+
cliParser.showHeader()
32+
config.mode match {
33+
case "test" => TestCommand.execute(config)
34+
case _ => println("Unknown command")
35+
}
36+
37+
case None => println("nope")
38+
}
39+
40+
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package de.upb.cs.swt.delphi.cli
2+
3+
import akka.actor.ActorSystem
4+
import akka.http.scaladsl.Http
5+
import akka.http.scaladsl.model.{HttpRequest, Uri}
6+
import akka.stream.{ActorMaterializer, ActorMaterializerSettings}
7+
import akka.util.ByteString
8+
9+
import scala.util.{Failure, Success}
10+
11+
/**
12+
* The implementation of the test command.
13+
* Tries to connect to the Delphi server and reports on the results of the version call.
14+
*/
15+
object TestCommand extends Command {
16+
override def execute(config: Config): Unit = {
17+
18+
implicit val system = ActorSystem()
19+
implicit val executionContext = system.dispatcher
20+
implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(system))
21+
22+
val uri = Uri(config.server)
23+
println(s"Contacting server ${uri}...")
24+
val resp = Http().singleRequest(HttpRequest(uri = uri.withPath(uri.path + "/version")))
25+
26+
resp.onComplete {
27+
case Success(res) =>
28+
res.status match {
29+
case x if x.isSuccess() => {
30+
res.entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body =>
31+
println("Successfully contacted Delphi server. ")
32+
println("Server version: " + body.utf8String)
33+
} }
34+
case _ => {
35+
println(s"Could not validate server ${config.server}. Error: ${res.status}.")
36+
}
37+
}
38+
system.terminate()
39+
case Failure(_) => {
40+
println(s"Could not reach server ${config.server}.")
41+
42+
system.terminate()
43+
}
44+
}
45+
46+
}
47+
}

0 commit comments

Comments
 (0)