Skip to content

Commit d48bf52

Browse files
committed
Added features command. Closes #25
1 parent f3b1a07 commit d48bf52

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ libraryDependencies += "au.com.bytecode" % "opencsv" % "2.4"
5858
libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.0" % "test"
5959
libraryDependencies += "joda-time" % "joda-time" % "2.10.5"
6060

61+
libraryDependencies += "de.upb.cs.swt.delphi" %% "delphi-core" % "0.9.2"
62+
libraryDependencies += "de.upb.cs.swt.delphi" %% "delphi-client" % "0.9.2"
63+
6164
libraryDependencies ++= Seq(
6265
"com.softwaremill.sttp" %% "core" % "1.7.2",
6366
"com.softwaremill.sttp" %% "spray-json" % "1.7.2"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package de.upb.cs.swt.delphi.cli
1818

1919
import de.upb.cs.swt.delphi.cli.artifacts.{RetrieveResult, SearchResult}
20+
import de.upb.cs.swt.delphi.client.FieldDefinition
2021

2122
class ConsoleOutput(config: Config) {
2223

@@ -45,6 +46,7 @@ class ConsoleOutput(config: Config) {
4546
}
4647
}
4748
case retrieveResults : Seq[RetrieveResult] if retrieveResults.head.isInstanceOf[RetrieveResult] => ResultBeautifier.beautifyRetrieveResults(retrieveResults)
49+
case featureResults : Seq[FieldDefinition] if featureResults.head.isInstanceOf[FieldDefinition] => ResultBeautifier.beautifyFeatures(featureResults)
4850
case _ => value.toString
4951
}
5052
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ object DelphiCLI {
5050
case "test" => TestCommand.execute
5151
case "retrieve" => RetrieveCommand.execute
5252
case "search" => SearchCommand.execute
53+
case "features" => FeaturesCommand.execute
5354
case "" => cliParser.showUsage()
5455
case x => config.consoleOutput.outputError(s"Unknown command: $x")
5556
}
@@ -79,6 +80,9 @@ object DelphiCLI {
7980

8081
cmd("test").action((_, c) => c.copy(mode = "test"))
8182

83+
cmd("features").action((_, c) => c.copy(mode = "features"))
84+
.text("Retrieve the current list of features.")
85+
8286
cmd("retrieve").action((s, c) => c.copy(mode = "retrieve"))
8387
.text("Retrieve a project's description, specified by ID.")
8488
.children(

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package de.upb.cs.swt.delphi.cli
1818

1919
import de.upb.cs.swt.delphi.cli.artifacts.{RetrieveResult, SearchResult}
20+
import de.upb.cs.swt.delphi.client.FieldDefinition
2021
import de.vandermeer.asciitable.{AsciiTable, CWC_LongestLine}
2122
import de.vandermeer.asciithemes.{TA_Grid, TA_GridThemes}
2223
import de.vandermeer.skb.interfaces.transformers.textformat.TextAlignment
@@ -53,13 +54,13 @@ object ResultBeautifier {
5354
at.getRenderer.setCWC(new CWC_LongestLine)
5455
at.setPaddingLeft(1)
5556
at.setPaddingRight(1)
57+
5658
at.getContext.setFrameTopMargin(1)
5759
at.getContext.setFrameBottomMargin(1)
5860
at.getContext().setGridTheme(TA_GridThemes.INSIDE)
5961

6062

6163
at.render()
62-
//CustomAsciiTable.make(table)
6364
}
6465
}
6566

@@ -112,4 +113,36 @@ object ResultBeautifier {
112113

113114
}.fold("")(_ + _)
114115
}
116+
def beautifyFeatures(results : Seq[FieldDefinition]) : String = {
117+
118+
if (results.size == 0) {
119+
""
120+
} else {
121+
val tableHeader = Seq ("Name", "Description")
122+
val tableBody = results.sortBy(f => f.name).map(f => Seq(f.name, f.description))
123+
val table = tableBody.+:(tableHeader)
124+
125+
val at = new AsciiTable()
126+
127+
at.setTextAlignment(TextAlignment.JUSTIFIED_LEFT)
128+
at.getContext().setWidth(80)
129+
130+
// add header
131+
at.addRule()
132+
at.addRow(table.head.asJavaCollection)
133+
at.addRule()
134+
135+
// add body
136+
table.tail.foreach { row: Iterable[String] => at.addRow(row.asJavaCollection) }
137+
138+
at.setPaddingLeft(1)
139+
at.setPaddingRight(1)
140+
at.getContext.setFrameTopMargin(1)
141+
at.getContext.setFrameBottomMargin(1)
142+
//at.getContext().setGridTheme(TA_GridThemes.)
143+
at.addRule()
144+
at.render()
145+
}
146+
}
147+
115148
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ trait Command {
4545
val oldPath = serverUrl.path
4646
val reqUrl = serverUrl.path(oldPath ++ paths).params(parameters)
4747
val request = sttp.get(reqUrl)
48-
config.consoleOutput.outputInformation(s"Sending request ${request.uri}")
48+
//config.consoleOutput.outputInformation(s"Sending request ${request.uri}")
4949
val response = request.send()
5050
response.body match {
5151
case Left(value) =>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2020 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package de.upb.cs.swt.delphi.cli.commands
18+
import com.softwaremill.sttp.{Id, SttpBackend}
19+
import de.upb.cs.swt.delphi.cli.Config
20+
import de.upb.cs.swt.delphi.client.FieldDefinition
21+
import de.upb.cs.swt.delphi.client.FieldDefinitionJson._
22+
import spray.json._
23+
24+
object FeaturesCommand extends Command {
25+
override def execute(implicit config: Config, backend: SttpBackend[Id, Nothing]): Unit = {
26+
val result = executeGet(Seq("features"))
27+
28+
result.map(features => {
29+
if (config.raw) {
30+
reportResult.apply(features)
31+
}
32+
if (!config.raw || !config.csv.equals("")) {
33+
val featureList = features.parseJson.convertTo[JsArray].elements.map(e => e.convertTo[FieldDefinition])
34+
reportResult.apply(featureList)
35+
36+
if (!config.csv.equals("")) {
37+
exportResult.apply(featureList)
38+
information.apply("Results written to file '" + config.csv + "'")
39+
}
40+
}
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)