-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathMain.java
More file actions
314 lines (303 loc) · 12.2 KB
/
Main.java
File metadata and controls
314 lines (303 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package org.openapitools.openapidiff.cli;
import ch.qos.logback.classic.Level;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openapitools.openapidiff.core.OpenApiCompare;
import org.openapitools.openapidiff.core.compare.OpenApiDiffOptions;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
import org.openapitools.openapidiff.core.output.AsciidocRender;
import org.openapitools.openapidiff.core.output.ConsoleRender;
import org.openapitools.openapidiff.core.output.HtmlRender;
import org.openapitools.openapidiff.core.output.I18n;
import org.openapitools.openapidiff.core.output.JsonRender;
import org.openapitools.openapidiff.core.output.MarkdownRender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String... args) {
Options options = new Options();
options.addOption(Option.builder("h").longOpt("help").desc("print this message").build());
options.addOption(
Option.builder("v")
.longOpt("version")
.desc("print the version information and exit")
.build());
options.addOption(
Option.builder()
.longOpt("state")
.desc("Only output diff state: no_changes, incompatible, compatible")
.build());
options.addOption(
Option.builder()
.longOpt("fail-on-incompatible")
.desc("Fail only if API changes broke backward compatibility")
.build());
options.addOption(
Option.builder()
.longOpt("fail-on-changed")
.desc("Fail if API changed but is backward compatible")
.build());
options.addOption(
Option.builder()
.longOpt("config-file")
.hasArg()
.desc("Config file to override default behavior. Supported file formats: .yaml")
.build());
options.addOption(
Option.builder()
.longOpt("config-prop")
.hasArg()
.desc(
"Config property to override default behavior. Arg in format of [propKey]:[propVal]")
.build());
options.addOption(Option.builder().longOpt("trace").desc("be extra verbose").build());
options.addOption(
Option.builder().longOpt("debug").desc("Print debugging information").build());
options.addOption(
Option.builder().longOpt("info").desc("Print additional information").build());
options.addOption(Option.builder().longOpt("warn").desc("Print warning information").build());
options.addOption(Option.builder().longOpt("error").desc("Print error information").build());
options.addOption(Option.builder().longOpt("off").desc("No information printed").build());
options.addOption(Option.builder().longOpt("off").desc("No information printed").build());
options.addOption(
Option.builder("l")
.longOpt("log")
.hasArg()
.argName("level")
.desc("use given level for log (TRACE, DEBUG, INFO, WARN, ERROR, OFF). Default: ERROR")
.build());
options.addOption(
Option.builder()
.longOpt("header")
.hasArgs()
.numberOfArgs(2)
.valueSeparator()
.argName("property=value")
.desc("use given header for authorization")
.build());
options.addOption(
Option.builder()
.longOpt("query")
.hasArgs()
.numberOfArgs(2)
.valueSeparator()
.argName("property=value")
.desc("use query param for authorization")
.build());
options.addOption(
Option.builder()
.longOpt("markdown")
.hasArg()
.argName("file")
.desc("export diff as markdown in given file")
.build());
options.addOption(
Option.builder()
.longOpt("asciidoc")
.hasArg()
.argName("file")
.desc("export diff as asciidoc in given file")
.build());
options.addOption(
Option.builder()
.longOpt("html")
.hasArg()
.argName("file")
.desc("export diff as html in given file with incompatible changes")
.build());
options.addOption(
Option.builder()
.longOpt("html-detailed")
.hasArg()
.argName("file")
.desc("export diff as html in given file with all changes")
.build());
options.addOption(
Option.builder()
.longOpt("text")
.hasArg()
.argName("file")
.desc("export diff as text in given file")
.build());
options.addOption(
Option.builder()
.longOpt("json")
.hasArg()
.argName("file")
.desc("export diff as json in given file")
.build());
options.addOption(
Option.builder()
.longOpt("lang")
.hasArg()
.argName("language")
.desc("output language (en, zh-Hant, zh-CN). Default: en")
.build());
// create the parser
CommandLineParser parser = new DefaultParser();
try {
// parse the command line arguments
CommandLine line = parser.parse(options, args);
if (line.hasOption("h")) { // automatically generate the help statement
printHelp(options);
System.exit(0);
}
if (line.hasOption("version") || line.hasOption("v")) {
String version = Main.class.getPackage().getImplementationVersion();
System.out.println(
I18n.getMessage("cli.version.prefix") + " " + (version != null ? version : "DEV"));
System.exit(0);
}
if (line.hasOption("lang")) {
I18n.setLocale(I18n.parseLocale(line.getOptionValue("lang")));
}
String logLevel = "ERROR";
if (line.hasOption("off")) {
logLevel = "OFF";
}
if (line.hasOption("error")) {
logLevel = "ERROR";
}
if (line.hasOption("warn")) {
logLevel = "WARN";
}
if (line.hasOption("info")) {
logLevel = "INFO";
}
if (line.hasOption("debug")) {
logLevel = "DEBUG";
}
if (line.hasOption("trace")) {
logLevel = "TRACE";
}
if (line.hasOption("log")) {
logLevel = line.getOptionValue("log");
if (!logLevel.equalsIgnoreCase("TRACE")
&& !logLevel.equalsIgnoreCase("DEBUG")
&& !logLevel.equalsIgnoreCase("INFO")
&& !logLevel.equalsIgnoreCase("WARN")
&& !logLevel.equalsIgnoreCase("ERROR")
&& !logLevel.equalsIgnoreCase("OFF")) {
throw new ParseException(I18n.getMessage("cli.invalid.log.level", logLevel));
}
}
if (line.hasOption("state")) {
logLevel = "OFF";
}
ch.qos.logback.classic.Logger root =
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.toLevel(logLevel));
if (line.getArgList().size() < 2) {
throw new ParseException(I18n.getMessage("cli.missing.arguments"));
}
String oldPath = line.getArgList().get(0);
String newPath = line.getArgList().get(1);
List<AuthorizationValue> auths = null;
if (line.hasOption("header")) {
String[] headers = line.getOptionValues("header");
auths = Collections.singletonList(new AuthorizationValue(headers[0], headers[1], "header"));
}
OpenApiDiffOptions.Builder optionBuilder = OpenApiDiffOptions.builder();
String[] configFilePaths = line.getOptionValues("config-file");
if (configFilePaths != null) {
for (String configFilePath : configFilePaths) {
optionBuilder.configYaml(new File(configFilePath));
}
}
String[] configProps = line.getOptionValues("config-prop");
if (configProps != null) {
for (String propKeyAndVal : configProps) {
String[] split = propKeyAndVal.split(":");
if (split.length != 2 || split[0].isEmpty() || split[1].isEmpty()) {
throw new IllegalArgumentException(
I18n.getMessage("cli.config.prop.unexpected.format") + " " + propKeyAndVal);
}
optionBuilder.configProperty(split[0], split[1]);
}
}
OpenApiDiffOptions compareOpts = optionBuilder.build();
ChangedOpenApi result = OpenApiCompare.fromLocations(oldPath, newPath, auths, compareOpts);
ConsoleRender consoleRender = new ConsoleRender();
if (!logLevel.equals("OFF")) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
consoleRender.render(result, outputStreamWriter);
System.out.println(outputStream);
}
if (line.hasOption("html")) {
HtmlRender htmlRender = new HtmlRender();
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("html"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
htmlRender.render(result, outputStreamWriter);
}
if (line.hasOption("html-detailed")) {
HtmlRender htmlRender = new HtmlRender(true);
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("html-detailed"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
htmlRender.render(result, outputStreamWriter);
}
if (line.hasOption("markdown")) {
MarkdownRender mdRender = new MarkdownRender();
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("markdown"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
mdRender.render(result, outputStreamWriter);
}
if (line.hasOption("asciidoc")) {
AsciidocRender adocRender = new AsciidocRender();
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("asciidoc"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
adocRender.render(result, outputStreamWriter);
}
if (line.hasOption("text")) {
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("text"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
consoleRender.render(result, outputStreamWriter);
}
if (line.hasOption("json")) {
JsonRender jsonRender = new JsonRender();
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("json"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
jsonRender.render(result, outputStreamWriter);
}
if (line.hasOption("state")) {
System.out.println(result.isChanged().getValue());
System.exit(0);
} else if (line.hasOption("fail-on-incompatible")) {
System.exit(result.isCompatible() ? 0 : 1);
} else if (line.hasOption("fail-on-changed")) {
System.exit(result.isUnchanged() ? 0 : 1);
}
} catch (ParseException e) {
// oops, something went wrong
System.err.println(I18n.getMessage("cli.parsing.failed") + " " + e.getMessage());
printHelp(options);
System.exit(2);
} catch (Exception e) {
System.err.println(
I18n.getMessage("cli.unexpected.exception")
+ " "
+ e.getMessage()
+ "\n"
+ ExceptionUtils.getStackTrace(e));
System.exit(2);
}
}
public static void printHelp(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("openapi-diff <old> <new>", options);
}
}