Skip to content

Commit 3350900

Browse files
committed
Refactor output parameters
Remove `--output` parameter, add `--text` parameter for text output into a file, and allow all of them, `--html`, `--markdown` and `--text`, at the same time instead of only one. The `--output` parameter is not needed because the same functionality is provided by `--html`, `--markdown`, and `--text` in a more intuitive way.
1 parent cef8549 commit 3350900

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ usage: openapi-diff <old> <new>
7070
-l,--log <level> use given level for log (TRACE, DEBUG,
7171
INFO, WARN, ERROR, OFF). Default: ERROR
7272
--markdown <file> export diff as markdown in given file
73-
-o,--output <format=file> use given format (html, markdown) for
74-
output in file
7573
--off No information printed
7674
--query <property=value> use query param for authorisation
7775
--state Only output diff state: no_changes,

src/main/java/com/qdesrame/openapi/diff/Main.java

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import com.qdesrame.openapi.diff.output.MarkdownRender;
77
import java.io.File;
88
import java.io.IOException;
9-
import org.apache.commons.cli.*;
9+
import org.apache.commons.cli.CommandLine;
10+
import org.apache.commons.cli.CommandLineParser;
11+
import org.apache.commons.cli.DefaultParser;
12+
import org.apache.commons.cli.HelpFormatter;
13+
import org.apache.commons.cli.Option;
14+
import org.apache.commons.cli.Options;
15+
import org.apache.commons.cli.ParseException;
1016
import org.apache.commons.io.FileUtils;
1117
import org.apache.commons.lang3.exception.ExceptionUtils;
1218
import org.apache.log4j.Level;
@@ -66,15 +72,6 @@ public static void main(String... args) {
6672
.argName("property=value")
6773
.desc("use query param for authorisation")
6874
.build());
69-
options.addOption(
70-
Option.builder("o")
71-
.longOpt("output")
72-
.hasArgs()
73-
.numberOfArgs(2)
74-
.valueSeparator()
75-
.argName("format=file")
76-
.desc("use given format (html, markdown) for output in file")
77-
.build());
7875
options.addOption(
7976
Option.builder()
8077
.longOpt("markdown")
@@ -89,6 +86,13 @@ public static void main(String... args) {
8986
.argName("file")
9087
.desc("export diff as html in given file")
9188
.build());
89+
options.addOption(
90+
Option.builder()
91+
.longOpt("text")
92+
.hasArg()
93+
.argName("file")
94+
.desc("export diff as text in given file")
95+
.build());
9296

9397
final String message = "Hello logging!";
9498
// create the parser
@@ -148,38 +152,22 @@ public static void main(String... args) {
148152
if (!logLevel.equals("OFF")) {
149153
System.out.println(consoleRender.render(result));
150154
}
151-
HtmlRender htmlRender = new HtmlRender();
152-
MarkdownRender mdRender = new MarkdownRender();
153-
String output = null;
154-
String outputFile = null;
155155
if (line.hasOption("html")) {
156-
output = htmlRender.render(result);
157-
outputFile = line.getOptionValue("html");
156+
HtmlRender htmlRender = new HtmlRender();
157+
String output = htmlRender.render(result);
158+
String outputFile = line.getOptionValue("html");
159+
writeOutput(output, outputFile);
158160
}
159161
if (line.hasOption("markdown")) {
160-
output = mdRender.render(result);
161-
outputFile = line.getOptionValue("markdown");
162+
MarkdownRender mdRender = new MarkdownRender();
163+
String output = mdRender.render(result);
164+
String outputFile = line.getOptionValue("markdown");
165+
writeOutput(output, outputFile);
162166
}
163-
if (line.hasOption("output")) {
164-
String[] outputValues = line.getOptionValues("output");
165-
if (outputValues[0].equalsIgnoreCase("markdown")) {
166-
output = mdRender.render(result);
167-
} else if (outputValues[0].equalsIgnoreCase("html")) {
168-
output = htmlRender.render(result);
169-
} else {
170-
throw new ParseException("Invalid output format");
171-
}
172-
outputFile = outputValues[1];
173-
}
174-
if (output != null && outputFile != null) {
175-
File file = new File(outputFile);
176-
logger.debug("Output file: {}", file.getAbsolutePath());
177-
try {
178-
FileUtils.writeStringToFile(file, output);
179-
} catch (IOException e) {
180-
logger.error("Impossible to write output to file {}", outputFile, e);
181-
System.exit(2);
182-
}
167+
if (line.hasOption("text")) {
168+
String output = consoleRender.render(result);
169+
String outputFile = line.getOptionValue("text");
170+
writeOutput(output, outputFile);
183171
}
184172
if (line.hasOption("state")) {
185173
System.out.println(result.isChanged().getValue());
@@ -204,6 +192,17 @@ public static void main(String... args) {
204192
}
205193
}
206194

195+
private static void writeOutput(String output, String outputFile) {
196+
File file = new File(outputFile);
197+
logger.debug("Output file: {}", file.getAbsolutePath());
198+
try {
199+
FileUtils.writeStringToFile(file, output);
200+
} catch (IOException e) {
201+
logger.error("Impossible to write output to file {}", outputFile, e);
202+
System.exit(2);
203+
}
204+
}
205+
207206
public static void printHelp(Options options) {
208207
HelpFormatter formatter = new HelpFormatter();
209208
formatter.printHelp("openapi-diff <old> <new>", options);

0 commit comments

Comments
 (0)