@@ -19,41 +19,60 @@ import java.io.{BufferedWriter, FileOutputStream, FileWriter}
1919import java .nio .file .{Files , Paths }
2020
2121import com .softwaremill .sttp ._
22- import de .upb .cs .swt .delphi .cli .artifacts .{QueryStorageMetadata , SearchResult }
22+ import de .upb .cs .swt .delphi .cli .artifacts .{QueryStorageMetadata , Result , RetrieveResult , SearchResult }
2323import org .joda .time .DateTime
2424import org .joda .time .format .DateTimeFormat
2525import spray .json ._
26-
2726import de .upb .cs .swt .delphi .cli .artifacts .StorageMetadataJson .queryStorageMetadataFormat
2827
2928class FileOutput (serverVersion : String = " UNKNOWN" )(implicit config: Config , backend : SttpBackend [Id , Nothing ]){
3029
31- def writeSearchResults (results : List [SearchResult ]) : Unit = {
30+ def writeQueryResults (results : List [SearchResult ]): Unit = {
3231 val metadata = buildQueryMetadata(results)
3332
3433 val folderPath = Paths .get(config.output, DateTimeFormat .forPattern(" YYYY-MM-dd_HH_mm_ss" ).print(metadata.timestamp))
3534 Files .createDirectory(folderPath)
3635
37- val metadataPath = Paths .get(folderPath.toString, " query-metadata.json" ).toString
36+ downloadResultFiles(results, metadata, folderPath.toString)
37+ }
38+
39+ def writeRetrieveResults (results : Seq [RetrieveResult ]): Unit = {
40+ val metadata = buildRetrieveMetadata(results)
41+ val first = results.head
42+
43+ val timestamp = DateTimeFormat .forPattern(" YYYY-MM-dd_HH_mm_ss" ).print(metadata.timestamp)
44+ val folderPath = Paths .get(config.output, s " ${first.metadata.artifactId}- ${first.metadata.version}- $timestamp" )
45+ Files .createDirectory(folderPath)
46+
47+ downloadResultFiles(results, metadata, folderPath.toString)
48+ }
49+
50+
51+ private def downloadResultFiles (results : Seq [Result ], metadata : QueryStorageMetadata , folderPath : String ) : Unit = {
52+ // Write Metadata first
53+ val metadataPath = Paths .get(folderPath, " query-metadata.json" ).toString
3854 val writer = new BufferedWriter (new FileWriter (metadataPath))
3955 writer.write(metadata.toJson.prettyPrint)
4056 writer.close()
4157
4258 val outputMode = config.outputMode.getOrElse(OutputMode .PomOnly )
43- var progressCnt = 0f
4459
4560 info()
46- info(f " Output Mode is ${outputMode.toString}, destination is ${folderPath.toString}" )
61+ outputMode match {
62+ case OutputMode .PomOnly => info(f " All associated POM files will be stored in $folderPath" )
63+ case OutputMode .JarOnly => info(f " All associated JAR files will be stored in $folderPath" )
64+ case _ => info(f " All associated JAR and POM files will be stored in $folderPath" )
65+ }
66+ var progressCnt = 0f
67+
4768 info()
4869 print(" Downloading files: 00 %" )
4970
5071 results
5172 .map(r => r.toMavenRelativeUrl() + s " / ${r.metadata.artifactId}- ${r.metadata.version}" )
5273 .map(relUrl => " https://repo1.maven.org/maven2/" + relUrl).foreach( urlWithoutExtension => {
5374
54- print(" \b\b\b\b " )
55- val progressValue = (100f * progressCnt ).toInt / results.size
56- print(s " ${if (progressValue < 10 ) f " 0 $progressValue" else progressValue} % " )
75+ writeProgressValue((100f * progressCnt ).toInt / results.size)
5776 progressCnt += 1
5877
5978 var artifactsToRetrieve = Seq [String ]()
@@ -65,15 +84,13 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
6584 }
6685 artifactsToRetrieve.foreach( url => {
6786 sttp.get(uri " $url" ).response(asByteArray).send().body match {
68- case Right (value) =>
69- new FileOutputStream (Paths .get(folderPath.toString, url.splitAt(url.lastIndexOf('/' ))._2).toString)
70- .write(value)
71- case Left (value) =>
72- error(f " Failed to download artifact from $url, got: $value" )
87+ case Right (value) => new FileOutputStream (Paths .get(folderPath, url.splitAt(url.lastIndexOf('/' ))._2).toString)
88+ .write(value)
89+ case Left (value) => error(f " Failed to download artifact from $url, got: $value" )
7390 }
7491 })
7592 })
76- print( " \b\b\b\b 100 % " )
93+ writeProgressValue( 100 )
7794 info()
7895 info()
7996 info(f " Successfully wrote results to $folderPath. " )
@@ -82,7 +99,10 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
8299 private def info (value : String = " " ): Unit = config.consoleOutput.outputInformation(value)
83100 private def error (value : String = " " ): Unit = config.consoleOutput.outputError(value)
84101
85-
102+ private def writeProgressValue (progressValue : Int ): Unit = {
103+ print(" \b\b\b\b " )
104+ print(s " ${if (progressValue < 10 ) f " 0 $progressValue" else progressValue} % " )
105+ }
86106 private def buildQueryMetadata (results : List [SearchResult ]) =
87107 QueryStorageMetadata ( query = config.query,
88108 results = results,
@@ -93,4 +113,15 @@ class FileOutput (serverVersion: String = "UNKNOWN")(implicit config:Config, ba
93113 resultLimit = config.limit.getOrElse(50 ),
94114 outputMode = config.outputMode.getOrElse(OutputMode .PomOnly ).toString
95115 )
116+
117+ private def buildRetrieveMetadata (results : Seq [RetrieveResult ]) =
118+ QueryStorageMetadata (query = f " Retrieve ${config.id}" ,
119+ results = results,
120+ serverVersion = serverVersion,
121+ serverUrl = config.server,
122+ clientVersion = BuildInfo .version,
123+ timestamp = DateTime .now(),
124+ resultLimit = 1 ,
125+ outputMode = config.outputMode.getOrElse(OutputMode .PomOnly ).toString
126+ )
96127}
0 commit comments