Skip to content

Commit 36bc9cf

Browse files
authored
Merge pull request #16 from almacken/feature/retrieveCommand
Added Retrieve Command
2 parents 72fc490 + 8696070 commit 36bc9cf

File tree

7 files changed

+96
-49
lines changed

7 files changed

+96
-49
lines changed

src/main/scala/de/upb/cs/swt/delphi/cli/Command.scala

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/scala/de/upb/cs/swt/delphi/cli/Config.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package de.upb.cs.swt.delphi.cli
66
* @param verbose Marker if logging should be verbose
77
* @param mode The command to be run
88
*/
9-
case class Config (server : String = sys.env.getOrElse("DELPHI_SERVER", "https://delphi.cs.uni-paderborn.de/api/"), verbose: Boolean = false, mode : String = "") {
9+
case class Config (server : String = sys.env.getOrElse("DELPHI_SERVER", "https://delphi.cs.uni-paderborn.de/api/"),
10+
verbose: Boolean = false, mode : String = "", args : List[String] = List(), opts : List[String] = List()) {
1011

1112
}

src/main/scala/de/upb/cs/swt/delphi/cli/DelphiCLI.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.upb.cs.swt.delphi.cli
22

3+
import de.upb.cs.swt.delphi.cli.commands.{RetrieveCommand, TestCommand}
4+
35

46
/**
57
* The application class for the Delphi command line interface
@@ -20,8 +22,16 @@ object DelphiCLI extends App {
2022
checkConfig(c => if (c.server.isEmpty()) failure("Option server is required.") else success)
2123

2224
cmd("test").action((_,c) => c.copy(mode = "test"))
25+
26+
cmd("retrieve").action((s,c) => c.copy(mode = "retrieve"))
27+
.text("Retrieve a project's description, specified by ID.")
28+
.children(
29+
arg[String]("ID").action((x, c) => c.copy(args = List(x))).text("The ID of the project to retrieve"),
30+
opt[Unit]('f', "file").action((_, c) => c.copy(opts = List("file"))).text("Use to load the ID from file, " +
31+
"with the filepath given in place of the ID")
32+
)
33+
2334
//cmd("search")
24-
//cmd("retrieve")
2535
}
2636
}
2737

@@ -31,6 +41,7 @@ object DelphiCLI extends App {
3141
cliParser.showHeader()
3242
config.mode match {
3343
case "test" => TestCommand.execute(config)
44+
case "retrieve" => RetrieveCommand.execute(config)
3445
case _ => println("Unknown command")
3546
}
3647

src/main/scala/de/upb/cs/swt/delphi/cli/TestCommand.scala

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package de.upb.cs.swt.delphi.cli.commands
2+
3+
import akka.http.scaladsl.model.Uri
4+
import de.upb.cs.swt.delphi.cli.{BlockingHttpClient, Config}
5+
6+
import scala.util.{Failure, Success}
7+
8+
/**
9+
* Represents the implementation of a command of the CLI
10+
*/
11+
trait Command {
12+
13+
/**
14+
* Executes the command implementation
15+
* @param config The current configuration for the command
16+
*/
17+
def execute(config: Config): Unit
18+
19+
/**
20+
* Implements a common request type using currying to avoid code duplication
21+
* @param target The endpoint to perform a Get request on
22+
* @param onSuccess The function to perform on the response (eg. printing it)
23+
* @param config The current configuration for the command
24+
*/
25+
protected def executeGet(target: String, onSuccess: String => Unit)(config: Config) : Unit = {
26+
27+
val uri = Uri(config.server)
28+
println(s"Contacting server ${uri}...")
29+
val resp = BlockingHttpClient.doGet(uri.withPath(uri.path + target))
30+
31+
resp match {
32+
case Success(res) => onSuccess(res)
33+
case Failure(_) => println(s"Could not reach server ${config.server}.")
34+
}
35+
36+
}
37+
38+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package de.upb.cs.swt.delphi.cli.commands
2+
3+
import de.upb.cs.swt.delphi.cli.Config
4+
import io.Source
5+
6+
/**
7+
* The implementation of the retrieve command.
8+
* Retrieves the contents of the file at the endpoint specified by the config file, and prints them to stdout
9+
*/
10+
object RetrieveCommand extends Command {
11+
override def execute(config: Config): Unit = {
12+
//Checks whether the ID should be loaded from a file or not, and either returns the first line
13+
// of the given file if it is, or the specified ID otherwise
14+
def checkTarget: String = {
15+
if (config.opts.contains("file")) {
16+
val source = Source.fromFile(config.args.head)
17+
val target = source.getLines.next()
18+
source.close()
19+
target
20+
} else config.args.head
21+
}
22+
executeGet(
23+
"/retrieve/" + checkTarget,
24+
s => println(s)
25+
)(config)
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package de.upb.cs.swt.delphi.cli.commands
2+
3+
import de.upb.cs.swt.delphi.cli.Config
4+
5+
/**
6+
* The implementation of the test command.
7+
* Tries to connect to the Delphi server and reports on the results of the version call.
8+
*/
9+
object TestCommand extends Command {
10+
override def execute(config: Config): Unit = executeGet(
11+
"/version",
12+
s => {
13+
println("Successfully contacted Delphi server. ")
14+
println("Server version: " + s)
15+
}
16+
)(config)
17+
}

0 commit comments

Comments
 (0)