Skip to content

Commit 1a43a27

Browse files
committed
feat(get): added support for multiple URIs to a directory
1 parent 9386edd commit 1a43a27

File tree

3 files changed

+295
-143
lines changed

3 files changed

+295
-143
lines changed

src/main/java/me/itzg/helpers/get/GetCommand.java

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package me.itzg.helpers.get;
22

3+
import java.io.IOException;
34
import java.io.PrintWriter;
45
import java.net.URI;
56
import java.net.URISyntaxException;
67
import java.nio.file.Files;
78
import java.nio.file.Path;
9+
import java.util.List;
810
import java.util.concurrent.Callable;
911
import lombok.extern.slf4j.Slf4j;
1012
import org.apache.hc.client5.http.classic.methods.HttpGet;
@@ -30,6 +32,9 @@ public class GetCommand implements Callable<Integer> {
3032
@Option(names = "--output-filename", description = "Output the resulting filename")
3133
boolean outputFilename;
3234

35+
@Option(names = "--log-progress-each", description = "Output a log as each URI is being retrieved")
36+
boolean logProgressEach;
37+
3338
@Option(names = "--json-path",
3439
description = "Extract and output a JsonPath from the response")
3540
String jsonPath;
@@ -41,42 +46,40 @@ public class GetCommand implements Callable<Integer> {
4146
paramLabel = "FILE|DIR")
4247
Path outputFile;
4348

44-
@Parameters(arity = "1")
45-
URI uri;
49+
@Parameters(arity = "1..", split = ",", paramLabel = "URI",
50+
description = "The URI of the resource to retrieve. When the output is a directory,"
51+
+ " more than one URI can be requested.")
52+
List<URI> uris;
4653

4754
@Override
48-
public Integer call() throws Exception {
55+
public Integer call() {
4956
final LatchingUrisInterceptor interceptor = new LatchingUrisInterceptor();
5057

5158
try (CloseableHttpClient client = HttpClients.custom()
5259
.addExecInterceptorFirst("latchRequestUris", interceptor)
5360
.build()) {
5461

55-
final URI requestUri = this.uri.getPath().startsWith("//") ?
56-
alterUriPath(this.uri.getPath().substring(1)) : this.uri;
57-
58-
log.debug("GETing uri={}", requestUri);
59-
60-
final HttpGet request = new HttpGet(requestUri);
61-
6262
final PrintWriter stdout = spec.commandLine().getOut();
6363

64-
final HttpClientResponseHandler<String> handler;
6564
if (jsonPath != null) {
66-
handler = new JsonPathOutputHandler(stdout, jsonPath);
65+
assertSingleUri();
66+
processSingleUri(uris.get(0), client, stdout, new JsonPathOutputHandler(stdout, jsonPath));
6767
}
6868
else if (outputFile == null) {
69-
handler = new PrintWriterHandler(stdout);
69+
assertSingleUri();
70+
processSingleUri(uris.get(0), client, stdout, new PrintWriterHandler(stdout));
7071
} else if (Files.isDirectory(outputFile)) {
71-
handler = new OutputToDirectoryHandler(outputFile, interceptor);
72+
processUris(uris, client, stdout,
73+
interceptor::reset, new OutputToDirectoryHandler(outputFile, interceptor));
7274
} else {
73-
handler = new SaveToFileHandler(outputFile);
75+
assertSingleUri();
76+
processSingleUri(uris.get(0), client, stdout, new SaveToFileHandler(outputFile));
7477
}
7578

76-
final String filename = client.execute(request, handler);
77-
if (outputFilename) {
78-
stdout.println(filename);
79-
}
79+
} catch (IllegalArgumentException e) {
80+
log.error("Invalid usage: {}", e.getMessage());
81+
log.debug("Details", e);
82+
return ExitCode.USAGE;
8083
} catch (Exception e) {
8184
log.error("Failed to download: {}", e.getMessage());
8285
log.debug("Details", e);
@@ -86,7 +89,43 @@ else if (outputFile == null) {
8689
return ExitCode.OK;
8790
}
8891

89-
private URI alterUriPath(String path) throws URISyntaxException {
92+
private void processUris(List<URI> uris, CloseableHttpClient client, PrintWriter stdout,
93+
Runnable reset, HttpClientResponseHandler<String> handler) throws URISyntaxException, IOException {
94+
for (URI uri : uris) {
95+
processSingleUri(uri, client, stdout, handler);
96+
reset.run();
97+
}
98+
}
99+
100+
private void processSingleUri(URI uri, CloseableHttpClient client, PrintWriter stdout,
101+
HttpClientResponseHandler<String> handler) throws URISyntaxException, IOException {
102+
final URI requestUri = uri.getPath().startsWith("//") ?
103+
alterUriPath(uri, uri.getPath().substring(1)) : uri;
104+
105+
if (logProgressEach) {
106+
log.info("Getting {}", requestUri);
107+
}
108+
else {
109+
log.debug("GETing uri={}", requestUri);
110+
}
111+
112+
final HttpGet request = new HttpGet(requestUri);
113+
114+
final String filename = client.execute(request, handler);
115+
if (outputFilename) {
116+
stdout.println(filename);
117+
}
118+
}
119+
120+
private void assertSingleUri() {
121+
if (uris.size() > 1) {
122+
log.debug("Too many URIs: {}", uris);
123+
throw new IllegalArgumentException(
124+
"Multiple URIs can only be used with an output directory");
125+
}
126+
}
127+
128+
private static URI alterUriPath(URI uri, String path) throws URISyntaxException {
90129
return new URI(
91130
uri.getScheme(),
92131
uri.getAuthority(),

src/main/java/me/itzg/helpers/get/LatchingUrisInterceptor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ public ClassicHttpResponse execute(ClassicHttpRequest request, Scope scope, Exec
3737
public URI getLastRequestedUri() {
3838
return uris.peekFirst();
3939
}
40+
41+
public void reset() {
42+
uris.clear();
43+
}
4044
}

0 commit comments

Comments
 (0)