|
36 | 36 | import org.springframework.stereotype.Component; |
37 | 37 | import picocli.CommandLine; |
38 | 38 |
|
| 39 | +import java.io.FileOutputStream; |
| 40 | +import java.io.OutputStream; |
39 | 41 | import java.time.ZonedDateTime; |
40 | 42 | import java.util.Collection; |
41 | 43 | import java.util.Objects; |
@@ -66,6 +68,8 @@ public class GHSACommand extends AbstractJsonCommand { |
66 | 68 | private String classifications; |
67 | 69 | @CommandLine.Option(names = {"--interactive"}, description = "Displays a progress bar") |
68 | 70 | private boolean interactive; |
| 71 | + @CommandLine.Option(names = {"--out"}, description = "Output file path (writes to stdout if not specified)") |
| 72 | + private String output; |
69 | 73 | // yes - this should not be a string, but seriously down the call path the HttpClient |
70 | 74 | // doesn't support passing a header in as a char[]... |
71 | 75 | private String apiKey = null; |
@@ -127,37 +131,41 @@ public Integer timedCall() throws Exception { |
127 | 131 | objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
128 | 132 |
|
129 | 133 | JsonFactory jfactory = objectMapper.getFactory(); |
130 | | - JsonGenerator jsonOut = jfactory.createGenerator(System.out, JsonEncoding.UTF8); |
131 | | - if (isPrettyPrint()) { |
132 | | - jsonOut.useDefaultPrettyPrinter(); |
133 | | - } |
134 | | - |
135 | | - jsonOut.writeStartObject(); |
136 | | - jsonOut.writeFieldName("advisories"); |
137 | | - jsonOut.writeStartArray(); |
138 | 134 | BasicOutput output = new BasicOutput(); |
139 | | - try (GitHubSecurityAdvisoryClient api = builder.build(); |
| 135 | + try (OutputStream outStream = (this.output != null) ? new FileOutputStream(this.output) : System.out; |
| 136 | + JsonGenerator jsonOut = jfactory.createGenerator(outStream, JsonEncoding.UTF8); |
| 137 | + GitHubSecurityAdvisoryClient api = builder.build(); |
140 | 138 | IProgressMonitor monitor = new ProgressMonitor(interactive, "GHSA")) { |
| 139 | + if (isPrettyPrint()) { |
| 140 | + jsonOut.useDefaultPrettyPrinter(); |
| 141 | + } |
| 142 | + |
| 143 | + jsonOut.writeStartObject(); |
| 144 | + jsonOut.writeFieldName("advisories"); |
| 145 | + jsonOut.writeStartArray(); |
| 146 | + LOG.debug("START: Requesting GHSA data"); |
141 | 147 | while (api.hasNext()) { |
142 | 148 | Collection<SecurityAdvisory> list = api.next(); |
| 149 | + LOG.debug("Retrieved {} GHSA records", (list != null) ? list.size() : 0); |
143 | 150 | if (list != null) { |
144 | 151 | output.setSuccess(true); |
145 | 152 | output.addCount(list.size()); |
146 | 153 | for (SecurityAdvisory c : list) { |
147 | 154 | jsonOut.writeObject(c); |
148 | 155 | } |
| 156 | + jsonOut.flush(); |
149 | 157 | monitor.updateProgress("GHSA", output.getCount(), api.getTotalAvailable()); |
150 | 158 | } else { |
151 | 159 | output.setSuccess(false); |
152 | 160 | output.setReason(String.format("Received HTTP Status Code: %s", api.getLastStatusCode())); |
153 | 161 | break; |
154 | 162 | } |
155 | 163 | } |
| 164 | + LOG.debug("END: Requesting GHSA data"); |
156 | 165 | output.setLastModifiedDate(api.getLastUpdated()); |
157 | 166 | jsonOut.writeEndArray(); |
158 | 167 | jsonOut.writeObjectField("results", output); |
159 | 168 | jsonOut.writeEndObject(); |
160 | | - jsonOut.close(); |
161 | 169 |
|
162 | 170 | if (!output.isSuccess()) { |
163 | 171 | String msg = String.format("%nFAILED: %s", output.getReason()); |
|
0 commit comments