|
91 | 91 | @Slf4j |
92 | 92 | public class McImageHelper { |
93 | 93 |
|
94 | | - //language=RegExp |
95 | | - public static final String OPTION_SPLIT_COMMAS = "\\s*,\\s*"; |
96 | | - //language=RegExp |
97 | | - public static final String SPLIT_COMMA_NL = "\\n|\\s*,\\s*"; |
98 | | - public static final String SPLIT_SYNOPSIS_COMMA_NL = ",|<nl>"; |
99 | | - //language=RegExp |
100 | | - public static final String VERSION_REGEX = "\\d+(\\.\\d+)+"; |
101 | | - |
102 | | - @Option(names = {"-h", |
103 | | - "--help"}, usageHelp = true, description = "Show this usage and exit") |
104 | | - boolean showHelp; |
105 | | - |
106 | | - @Option(names = {"-V", "--version"}, versionHelp = true) |
107 | | - boolean showVersion; |
108 | | - |
109 | | - @ArgGroup |
110 | | - LoggingOptions loggingOptions = new LoggingOptions(); |
111 | | - |
112 | | - static class LoggingOptions { |
113 | | - @Option(names = "--debug", description = "Enable debug output." |
114 | | - + " Can also set environment variables DEBUG_HELPER or DEBUG", |
115 | | - defaultValue = "${env:DEBUG_HELPER:-${env:DEBUG}}") |
116 | | - void setDebug(boolean enabled) { |
117 | | - setLevel(enabled, Level.DEBUG); |
118 | | - } |
| 94 | + //language=RegExp |
| 95 | + public static final String OPTION_SPLIT_COMMAS = "\\s*,\\s*"; |
| 96 | + //language=RegExp |
| 97 | + public static final String SPLIT_COMMA_NL = "\\n|\\s*,\\s*"; |
| 98 | + public static final String SPLIT_SYNOPSIS_COMMA_NL = ",|<nl>"; |
| 99 | + /** |
| 100 | + * Also works for newline delimited values. |
| 101 | + */ |
| 102 | + //language=RegExp |
| 103 | + public static final String SPLIT_COMMA_WS = "\\s+|\\s*,\\s*"; |
| 104 | + public static final String SPLIT_SYNOPSIS_COMMA_WS = ",|<ws>"; |
| 105 | + //language=RegExp |
| 106 | + public static final String VERSION_REGEX = "\\d+(\\.\\d+)+"; |
| 107 | + |
| 108 | + @Option(names = {"-h", |
| 109 | + "--help"}, usageHelp = true, description = "Show this usage and exit") |
| 110 | + boolean showHelp; |
| 111 | + |
| 112 | + @Option(names = {"-V", "--version"}, versionHelp = true) |
| 113 | + boolean showVersion; |
| 114 | + |
| 115 | + @SuppressWarnings("unused") // processed by setters |
| 116 | + @ArgGroup |
| 117 | + LoggingOptions loggingOptions = new LoggingOptions(); |
| 118 | + |
| 119 | + static class LoggingOptions { |
| 120 | + |
| 121 | + @Option(names = "--debug", description = "Enable debug output." |
| 122 | + + " Can also set environment variables DEBUG_HELPER or DEBUG", |
| 123 | + defaultValue = "${env:DEBUG_HELPER:-${env:DEBUG}}") |
| 124 | + void setDebug(boolean enabled) { |
| 125 | + setLevel(enabled, Level.DEBUG); |
| 126 | + } |
119 | 127 |
|
120 | | - @Option(names = "--logging", description = "Set logging to specific level.\nValid values: ${COMPLETION-CANDIDATES}", |
121 | | - defaultValue = "${env:HELPER_LOGGING_LEVEL}", |
122 | | - converter = LogbackLevelConverter.class |
123 | | - ) |
124 | | - void setLoggingLevel(Level level) { |
125 | | - setLevel(true, level); |
126 | | - } |
| 128 | + @Option(names = "--logging", description = "Set logging to specific level.\nValid values: ${COMPLETION-CANDIDATES}", |
| 129 | + defaultValue = "${env:HELPER_LOGGING_LEVEL}", |
| 130 | + converter = LogbackLevelConverter.class |
| 131 | + ) |
| 132 | + void setLoggingLevel(Level level) { |
| 133 | + setLevel(true, level); |
| 134 | + } |
| 135 | + |
| 136 | + private static void setLevel(boolean enabled, Level level) { |
| 137 | + ((Logger) LoggerFactory.getLogger("me.itzg.helpers")).setLevel( |
| 138 | + enabled ? level : Level.INFO); |
| 139 | + if (Level.TRACE.isGreaterOrEqual(level)) { |
| 140 | + ((Logger) LoggerFactory.getLogger("org.apache.hc.client5.http")).setLevel( |
| 141 | + enabled ? level : Level.INFO); |
| 142 | + } |
| 143 | + } |
127 | 144 |
|
128 | | - private static void setLevel(boolean enabled, Level level) { |
129 | | - ((Logger) LoggerFactory.getLogger("me.itzg.helpers")).setLevel( |
130 | | - enabled ? level : Level.INFO); |
131 | | - if (Level.TRACE.isGreaterOrEqual(level)) { |
132 | | - ((Logger) LoggerFactory.getLogger("org.apache.hc.client5.http")).setLevel( |
133 | | - enabled ? level : Level.INFO); |
134 | | - } |
135 | 145 | } |
136 | 146 |
|
137 | | - } |
138 | 147 |
|
| 148 | + @Option(names = {"-s", "--silent"}, description = "Don't output logs even if there's an error") |
| 149 | + @Getter |
| 150 | + boolean silent; |
139 | 151 |
|
140 | | - @Option(names = {"-s", "--silent"}, description = "Don't output logs even if there's an error") |
141 | | - @Getter |
142 | | - boolean silent; |
| 152 | + @Getter |
| 153 | + private static String version; |
143 | 154 |
|
144 | | - @Getter |
145 | | - private static String version; |
| 155 | + public static void main(String[] args) { |
| 156 | + final McImageHelper rootCommand = new McImageHelper(); |
| 157 | + try { |
| 158 | + version = McImageHelper.loadVersion(); |
| 159 | + } catch (IOException e) { |
| 160 | + log.error("Failed to load version", e); |
| 161 | + System.exit(1); |
| 162 | + } |
146 | 163 |
|
147 | | - public static void main(String[] args) { |
148 | | - final McImageHelper rootCommand = new McImageHelper(); |
149 | | - try { |
150 | | - version = McImageHelper.loadVersion(); |
151 | | - } catch (IOException e) { |
152 | | - log.error("Failed to load version", e); |
153 | | - System.exit(1); |
| 164 | + System.exit( |
| 165 | + new CommandLine(rootCommand) |
| 166 | + .setExitCodeExceptionMapper(new ExitCodeMapper()) |
| 167 | + .setExecutionExceptionHandler(new ExceptionHandler(rootCommand)) |
| 168 | + .setCaseInsensitiveEnumValuesAllowed(true) |
| 169 | + .execute(args) |
| 170 | + ); |
154 | 171 | } |
155 | 172 |
|
156 | | - System.exit( |
157 | | - new CommandLine(rootCommand) |
158 | | - .setExitCodeExceptionMapper(new ExitCodeMapper()) |
159 | | - .setExecutionExceptionHandler(new ExceptionHandler(rootCommand)) |
160 | | - .setCaseInsensitiveEnumValuesAllowed(true) |
161 | | - .execute(args) |
162 | | - ); |
163 | | - } |
164 | | - |
165 | | - private static String loadVersion() throws IOException { |
166 | | - final Enumeration<URL> resources = McImageHelper.class.getClassLoader().getResources("META-INF/MANIFEST.MF"); |
167 | | - while (resources.hasMoreElements()) { |
168 | | - final URL url = resources.nextElement(); |
169 | | - try (InputStream inputStream = url.openStream()) { |
170 | | - final Manifest manifest = new Manifest(inputStream); |
171 | | - final Attributes attributes = manifest.getMainAttributes(); |
172 | | - if ("mc-image-helper".equals(attributes.getValue(Name.IMPLEMENTATION_TITLE))) { |
173 | | - return attributes.getValue(Name.IMPLEMENTATION_VERSION); |
| 173 | + private static String loadVersion() throws IOException { |
| 174 | + final Enumeration<URL> resources = McImageHelper.class.getClassLoader().getResources("META-INF/MANIFEST.MF"); |
| 175 | + while (resources.hasMoreElements()) { |
| 176 | + final URL url = resources.nextElement(); |
| 177 | + try (InputStream inputStream = url.openStream()) { |
| 178 | + final Manifest manifest = new Manifest(inputStream); |
| 179 | + final Attributes attributes = manifest.getMainAttributes(); |
| 180 | + if ("mc-image-helper".equals(attributes.getValue(Name.IMPLEMENTATION_TITLE))) { |
| 181 | + return attributes.getValue(Name.IMPLEMENTATION_VERSION); |
| 182 | + } |
| 183 | + } |
174 | 184 | } |
175 | | - } |
| 185 | + return "???"; |
176 | 186 | } |
177 | | - return "???"; |
178 | | - } |
179 | 187 |
|
180 | | - public static class AppVersionProvider implements IVersionProvider { |
181 | | - @Override |
182 | | - public String[] getVersion() { |
| 188 | + public static class AppVersionProvider implements IVersionProvider { |
| 189 | + |
| 190 | + @Override |
| 191 | + public String[] getVersion() { |
183 | 192 |
|
184 | | - return new String[]{ |
185 | | - "${COMMAND-FULL-NAME}", |
186 | | - version |
187 | | - }; |
| 193 | + return new String[]{ |
| 194 | + "${COMMAND-FULL-NAME}", |
| 195 | + version |
| 196 | + }; |
| 197 | + } |
188 | 198 | } |
189 | | - } |
190 | 199 |
|
191 | | - private static class LogbackLevelConverter implements ITypeConverter<Level> { |
192 | | - @Override |
193 | | - public Level convert(String value) { |
194 | | - return Level.toLevel(value); |
| 200 | + private static class LogbackLevelConverter implements ITypeConverter<Level> { |
| 201 | + |
| 202 | + @Override |
| 203 | + public Level convert(String value) { |
| 204 | + return Level.toLevel(value); |
| 205 | + } |
195 | 206 | } |
196 | | - } |
197 | 207 | } |
0 commit comments