|
4 | 4 | import com.qdesrame.openapi.diff.output.ConsoleRender;
|
5 | 5 | import com.qdesrame.openapi.diff.output.HtmlRender;
|
6 | 6 | import com.qdesrame.openapi.diff.output.MarkdownRender;
|
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
7 | 9 | import org.apache.commons.cli.*;
|
8 | 10 | import org.apache.commons.io.FileUtils;
|
9 | 11 | import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
12 | 14 | import org.slf4j.Logger;
|
13 | 15 | import org.slf4j.LoggerFactory;
|
14 | 16 |
|
15 |
| -import java.io.File; |
16 |
| -import java.io.IOException; |
17 |
| - |
18 | 17 | public class Main {
|
19 | 18 |
|
20 |
| - static final Logger logger = LoggerFactory.getLogger(Main.class); |
| 19 | + static final Logger logger = LoggerFactory.getLogger(Main.class); |
21 | 20 |
|
22 |
| - public static void main(String... args) { |
23 |
| - Options options = new Options(); |
24 |
| - options.addOption(Option.builder("h").longOpt("help").desc("print this message").build()); |
25 |
| - options.addOption(Option.builder().longOpt("version").desc("print the version information and exit").build()); |
26 |
| - options.addOption(Option.builder().longOpt("state").desc("Only output diff state: no_changes, incompatible, compatible").build()); |
27 |
| - options.addOption(Option.builder().longOpt("trace").desc("be extra verbose").build()); |
28 |
| - options.addOption(Option.builder().longOpt("debug").desc("Print debugging information").build()); |
29 |
| - options.addOption(Option.builder().longOpt("info").desc("Print additional information").build()); |
30 |
| - options.addOption(Option.builder().longOpt("warn").desc("Print warning information").build()); |
31 |
| - options.addOption(Option.builder().longOpt("error").desc("Print error information").build()); |
32 |
| - options.addOption(Option.builder().longOpt("off").desc("No information printed").build()); |
33 |
| - options.addOption(Option.builder("l").longOpt("log").hasArg().argName("level") |
34 |
| - .desc("use given level for log (TRACE, DEBUG, INFO, WARN, ERROR, OFF). Default: ERROR").build()); |
35 |
| - options.addOption(Option.builder().longOpt("header").hasArgs().numberOfArgs(2).valueSeparator() |
36 |
| - .argName("property=value").desc("use given header for authorisation").build()); |
37 |
| - options.addOption(Option.builder().longOpt("query").hasArgs().numberOfArgs(2).valueSeparator() |
38 |
| - .argName("property=value").desc("use query param for authorisation").build()); |
39 |
| - options.addOption(Option.builder("o").longOpt("output").hasArgs().numberOfArgs(2).valueSeparator() |
40 |
| - .argName("format=file").desc("use given format (html, markdown) for output in file").build()); |
41 |
| - options.addOption(Option.builder().longOpt("markdown").hasArg().argName("file") |
42 |
| - .desc("export diff as markdown in given file").build()); |
43 |
| - options.addOption(Option.builder().longOpt("html").hasArg().argName("file") |
44 |
| - .desc("export diff as html in given file").build()); |
| 21 | + public static void main(String... args) { |
| 22 | + Options options = new Options(); |
| 23 | + options.addOption(Option.builder("h").longOpt("help").desc("print this message").build()); |
| 24 | + options.addOption( |
| 25 | + Option.builder().longOpt("version").desc("print the version information and exit").build()); |
| 26 | + options.addOption( |
| 27 | + Option.builder() |
| 28 | + .longOpt("state") |
| 29 | + .desc("Only output diff state: no_changes, incompatible, compatible") |
| 30 | + .build()); |
| 31 | + options.addOption(Option.builder().longOpt("trace").desc("be extra verbose").build()); |
| 32 | + options.addOption( |
| 33 | + Option.builder().longOpt("debug").desc("Print debugging information").build()); |
| 34 | + options.addOption( |
| 35 | + Option.builder().longOpt("info").desc("Print additional information").build()); |
| 36 | + options.addOption(Option.builder().longOpt("warn").desc("Print warning information").build()); |
| 37 | + options.addOption(Option.builder().longOpt("error").desc("Print error information").build()); |
| 38 | + options.addOption(Option.builder().longOpt("off").desc("No information printed").build()); |
| 39 | + options.addOption( |
| 40 | + Option.builder("l") |
| 41 | + .longOpt("log") |
| 42 | + .hasArg() |
| 43 | + .argName("level") |
| 44 | + .desc("use given level for log (TRACE, DEBUG, INFO, WARN, ERROR, OFF). Default: ERROR") |
| 45 | + .build()); |
| 46 | + options.addOption( |
| 47 | + Option.builder() |
| 48 | + .longOpt("header") |
| 49 | + .hasArgs() |
| 50 | + .numberOfArgs(2) |
| 51 | + .valueSeparator() |
| 52 | + .argName("property=value") |
| 53 | + .desc("use given header for authorisation") |
| 54 | + .build()); |
| 55 | + options.addOption( |
| 56 | + Option.builder() |
| 57 | + .longOpt("query") |
| 58 | + .hasArgs() |
| 59 | + .numberOfArgs(2) |
| 60 | + .valueSeparator() |
| 61 | + .argName("property=value") |
| 62 | + .desc("use query param for authorisation") |
| 63 | + .build()); |
| 64 | + options.addOption( |
| 65 | + Option.builder("o") |
| 66 | + .longOpt("output") |
| 67 | + .hasArgs() |
| 68 | + .numberOfArgs(2) |
| 69 | + .valueSeparator() |
| 70 | + .argName("format=file") |
| 71 | + .desc("use given format (html, markdown) for output in file") |
| 72 | + .build()); |
| 73 | + options.addOption( |
| 74 | + Option.builder() |
| 75 | + .longOpt("markdown") |
| 76 | + .hasArg() |
| 77 | + .argName("file") |
| 78 | + .desc("export diff as markdown in given file") |
| 79 | + .build()); |
| 80 | + options.addOption( |
| 81 | + Option.builder() |
| 82 | + .longOpt("html") |
| 83 | + .hasArg() |
| 84 | + .argName("file") |
| 85 | + .desc("export diff as html in given file") |
| 86 | + .build()); |
45 | 87 |
|
46 |
| - final String message = "Hello logging!"; |
47 |
| - // create the parser |
48 |
| - CommandLineParser parser = new DefaultParser(); |
49 |
| - try { |
50 |
| - // parse the command line arguments |
51 |
| - CommandLine line = parser.parse(options, args); |
52 |
| - if (line.hasOption("h")) { // automatically generate the help statement |
53 |
| - printHelp(options); |
54 |
| - System.exit(0); |
55 |
| - } |
56 |
| - String logLevel = "ERROR"; |
57 |
| - if (line.hasOption("off")) { |
58 |
| - logLevel = "OFF"; |
59 |
| - } |
60 |
| - if (line.hasOption("error")) { |
61 |
| - logLevel = "ERROR"; |
62 |
| - } |
63 |
| - if (line.hasOption("warn")) { |
64 |
| - logLevel = "WARN"; |
65 |
| - } |
66 |
| - if (line.hasOption("info")) { |
67 |
| - logLevel = "INFO"; |
68 |
| - } |
69 |
| - if (line.hasOption("debug")) { |
70 |
| - logLevel = "DEBUG"; |
71 |
| - } |
72 |
| - if (line.hasOption("trace")) { |
73 |
| - logLevel = "TRACE"; |
74 |
| - } |
75 |
| - if (line.hasOption("log")) { |
76 |
| - logLevel = line.getOptionValue("log"); |
77 |
| - if (!logLevel.equalsIgnoreCase("TRACE") && !logLevel.equalsIgnoreCase("DEBUG") |
78 |
| - && !logLevel.equalsIgnoreCase("INFO") && !logLevel.equalsIgnoreCase("WARN") |
79 |
| - && !logLevel.equalsIgnoreCase("ERROR") && !logLevel.equalsIgnoreCase("OFF")) { |
80 |
| - throw new ParseException( |
81 |
| - String.format("Invalid log level. Excepted: [TRACE, DEBUG, INFO, WARN, ERROR, OFF]. Given: %s", |
82 |
| - logLevel)); |
83 |
| - } |
84 |
| - } |
85 |
| - if (line.hasOption("state")) { |
86 |
| - logLevel = "OFF"; |
87 |
| - } |
88 |
| - LogManager.getRootLogger().setLevel(Level.toLevel(logLevel)); |
89 |
| - |
90 |
| - if (line.getArgList().size() < 2) { |
91 |
| - throw new ParseException("Missing arguments"); |
92 |
| - } |
93 |
| - String oldPath = line.getArgList().get(0); |
94 |
| - String newPath = line.getArgList().get(1); |
95 |
| - ChangedOpenApi result = OpenApiCompare.fromLocations(oldPath, newPath); |
96 |
| - ConsoleRender consoleRender = new ConsoleRender(); |
97 |
| - if (!logLevel.equals("OFF")) { |
98 |
| - System.out.println(consoleRender.render(result)); |
99 |
| - } |
100 |
| - HtmlRender htmlRender = new HtmlRender(); |
101 |
| - MarkdownRender mdRender = new MarkdownRender(); |
102 |
| - String output = null; |
103 |
| - String outputFile = null; |
104 |
| - if (line.hasOption("html")) { |
105 |
| - output = htmlRender.render(result); |
106 |
| - outputFile = line.getOptionValue("html"); |
107 |
| - } |
108 |
| - if (line.hasOption("markdown")) { |
109 |
| - output = mdRender.render(result); |
110 |
| - outputFile = line.getOptionValue("markdown"); |
111 |
| - } |
112 |
| - if (line.hasOption("output")) { |
113 |
| - String[] outputValues = line.getOptionValues("output"); |
114 |
| - if (outputValues[0].equalsIgnoreCase("markdown")) { |
115 |
| - output = mdRender.render(result); |
116 |
| - } else if (outputValues[0].equalsIgnoreCase("html")) { |
117 |
| - output = htmlRender.render(result); |
118 |
| - } else { |
119 |
| - throw new ParseException("Invalid output format"); |
120 |
| - } |
121 |
| - outputFile = outputValues[1]; |
122 |
| - } |
123 |
| - if (output != null && outputFile != null) { |
124 |
| - File file = new File(outputFile); |
125 |
| - logger.debug("Output file: {}", file.getAbsolutePath()); |
126 |
| - try { |
127 |
| - FileUtils.writeStringToFile(file, output); |
128 |
| - } catch (IOException e) { |
129 |
| - logger.error("Impossible to write output to file {}", outputFile, e); |
130 |
| - System.exit(2); |
131 |
| - } |
132 |
| - } |
133 |
| - if (line.hasOption("state")) { |
134 |
| - System.out.println(result.isChanged().getValue()); |
135 |
| - System.exit(0); |
136 |
| - } else { |
137 |
| - System.exit(result.isUnchanged() ? 0 : 1); |
138 |
| - } |
139 |
| - } catch (ParseException e) { |
140 |
| - // oops, something went wrong |
141 |
| - System.err.println("Parsing failed. Reason: " + e.getMessage()); |
142 |
| - printHelp(options); |
143 |
| - System.exit(2); |
144 |
| - } catch (Exception e) { |
145 |
| - System.err.println("Unexpected exception. Reason: " + e.getMessage() + "\n" + ExceptionUtils.getStackTrace(e)); |
146 |
| - System.exit(2); |
| 88 | + final String message = "Hello logging!"; |
| 89 | + // create the parser |
| 90 | + CommandLineParser parser = new DefaultParser(); |
| 91 | + try { |
| 92 | + // parse the command line arguments |
| 93 | + CommandLine line = parser.parse(options, args); |
| 94 | + if (line.hasOption("h")) { // automatically generate the help statement |
| 95 | + printHelp(options); |
| 96 | + System.exit(0); |
| 97 | + } |
| 98 | + String logLevel = "ERROR"; |
| 99 | + if (line.hasOption("off")) { |
| 100 | + logLevel = "OFF"; |
| 101 | + } |
| 102 | + if (line.hasOption("error")) { |
| 103 | + logLevel = "ERROR"; |
| 104 | + } |
| 105 | + if (line.hasOption("warn")) { |
| 106 | + logLevel = "WARN"; |
| 107 | + } |
| 108 | + if (line.hasOption("info")) { |
| 109 | + logLevel = "INFO"; |
| 110 | + } |
| 111 | + if (line.hasOption("debug")) { |
| 112 | + logLevel = "DEBUG"; |
| 113 | + } |
| 114 | + if (line.hasOption("trace")) { |
| 115 | + logLevel = "TRACE"; |
| 116 | + } |
| 117 | + if (line.hasOption("log")) { |
| 118 | + logLevel = line.getOptionValue("log"); |
| 119 | + if (!logLevel.equalsIgnoreCase("TRACE") |
| 120 | + && !logLevel.equalsIgnoreCase("DEBUG") |
| 121 | + && !logLevel.equalsIgnoreCase("INFO") |
| 122 | + && !logLevel.equalsIgnoreCase("WARN") |
| 123 | + && !logLevel.equalsIgnoreCase("ERROR") |
| 124 | + && !logLevel.equalsIgnoreCase("OFF")) { |
| 125 | + throw new ParseException( |
| 126 | + String.format( |
| 127 | + "Invalid log level. Excepted: [TRACE, DEBUG, INFO, WARN, ERROR, OFF]. Given: %s", |
| 128 | + logLevel)); |
147 | 129 | }
|
| 130 | + } |
| 131 | + if (line.hasOption("state")) { |
| 132 | + logLevel = "OFF"; |
| 133 | + } |
| 134 | + LogManager.getRootLogger().setLevel(Level.toLevel(logLevel)); |
148 | 135 |
|
| 136 | + if (line.getArgList().size() < 2) { |
| 137 | + throw new ParseException("Missing arguments"); |
| 138 | + } |
| 139 | + String oldPath = line.getArgList().get(0); |
| 140 | + String newPath = line.getArgList().get(1); |
| 141 | + ChangedOpenApi result = OpenApiCompare.fromLocations(oldPath, newPath); |
| 142 | + ConsoleRender consoleRender = new ConsoleRender(); |
| 143 | + if (!logLevel.equals("OFF")) { |
| 144 | + System.out.println(consoleRender.render(result)); |
| 145 | + } |
| 146 | + HtmlRender htmlRender = new HtmlRender(); |
| 147 | + MarkdownRender mdRender = new MarkdownRender(); |
| 148 | + String output = null; |
| 149 | + String outputFile = null; |
| 150 | + if (line.hasOption("html")) { |
| 151 | + output = htmlRender.render(result); |
| 152 | + outputFile = line.getOptionValue("html"); |
| 153 | + } |
| 154 | + if (line.hasOption("markdown")) { |
| 155 | + output = mdRender.render(result); |
| 156 | + outputFile = line.getOptionValue("markdown"); |
| 157 | + } |
| 158 | + if (line.hasOption("output")) { |
| 159 | + String[] outputValues = line.getOptionValues("output"); |
| 160 | + if (outputValues[0].equalsIgnoreCase("markdown")) { |
| 161 | + output = mdRender.render(result); |
| 162 | + } else if (outputValues[0].equalsIgnoreCase("html")) { |
| 163 | + output = htmlRender.render(result); |
| 164 | + } else { |
| 165 | + throw new ParseException("Invalid output format"); |
| 166 | + } |
| 167 | + outputFile = outputValues[1]; |
| 168 | + } |
| 169 | + if (output != null && outputFile != null) { |
| 170 | + File file = new File(outputFile); |
| 171 | + logger.debug("Output file: {}", file.getAbsolutePath()); |
| 172 | + try { |
| 173 | + FileUtils.writeStringToFile(file, output); |
| 174 | + } catch (IOException e) { |
| 175 | + logger.error("Impossible to write output to file {}", outputFile, e); |
| 176 | + System.exit(2); |
| 177 | + } |
| 178 | + } |
| 179 | + if (line.hasOption("state")) { |
| 180 | + System.out.println(result.isChanged().getValue()); |
| 181 | + System.exit(0); |
| 182 | + } else { |
| 183 | + System.exit(result.isUnchanged() ? 0 : 1); |
| 184 | + } |
| 185 | + } catch (ParseException e) { |
| 186 | + // oops, something went wrong |
| 187 | + System.err.println("Parsing failed. Reason: " + e.getMessage()); |
| 188 | + printHelp(options); |
| 189 | + System.exit(2); |
| 190 | + } catch (Exception e) { |
| 191 | + System.err.println( |
| 192 | + "Unexpected exception. Reason: " |
| 193 | + + e.getMessage() |
| 194 | + + "\n" |
| 195 | + + ExceptionUtils.getStackTrace(e)); |
| 196 | + System.exit(2); |
149 | 197 | }
|
| 198 | + } |
150 | 199 |
|
151 |
| - public static void printHelp(Options options) { |
152 |
| - HelpFormatter formatter = new HelpFormatter(); |
153 |
| - formatter.printHelp("openapi-diff <old> <new>", options); |
154 |
| - } |
| 200 | + public static void printHelp(Options options) { |
| 201 | + HelpFormatter formatter = new HelpFormatter(); |
| 202 | + formatter.printHelp("openapi-diff <old> <new>", options); |
| 203 | + } |
155 | 204 | }
|
0 commit comments