Skip to content

Commit 123d4d1

Browse files
authored
cf: handle spaces around commas in exclude/include args (#509)
1 parent a123ff5 commit 123d4d1

File tree

3 files changed

+100
-90
lines changed

3 files changed

+100
-90
lines changed

src/main/java/me/itzg/helpers/McImageHelper.java

Lines changed: 96 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -91,107 +91,117 @@
9191
@Slf4j
9292
public class McImageHelper {
9393

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+
}
119127

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+
}
127144

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-
}
135145
}
136146

137-
}
138147

148+
@Option(names = {"-s", "--silent"}, description = "Don't output logs even if there's an error")
149+
@Getter
150+
boolean silent;
139151

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;
143154

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+
}
146163

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+
);
154171
}
155172

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+
}
174184
}
175-
}
185+
return "???";
176186
}
177-
return "???";
178-
}
179187

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() {
183192

184-
return new String[]{
185-
"${COMMAND-FULL-NAME}",
186-
version
187-
};
193+
return new String[]{
194+
"${COMMAND-FULL-NAME}",
195+
version
196+
};
197+
}
188198
}
189-
}
190199

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+
}
195206
}
196-
}
197207
}

src/main/java/me/itzg/helpers/cache/ApiCachingImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private void pruneExpiredEntries() {
6262
if (entry.getExpiresAt().isBefore(now)) {
6363
final Path contentFile = resolveContentFile(operation, entry.getFilename());
6464
try {
65-
log.debug("Pruning cached content file {}", contentFile);
65+
log.trace("Pruning cached content file {}", contentFile);
6666
Files.delete(contentFile);
6767
} catch (IOException e) {
6868
log.warn("Failed to delete cached content file {}", contentFile, e);
@@ -162,7 +162,7 @@ private <R> Mono<R> saveToCache(String operation, String keys, R value) {
162162
);
163163
}
164164

165-
log.debug("Saved cache content of {}({}) to {}", operation, keys, contentFile);
165+
log.trace("Saved cache content of {}({}) to {}", operation, keys, contentFile);
166166
} catch (IOException e) {
167167
log.warn("Failed to cache file for operation={} keys={}", operation, keys, e);
168168
}

src/main/java/me/itzg/helpers/curseforge/InstallCurseForgeCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ static class ExcludeIncludeArgs {
102102

103103
static class Listed {
104104
@Option(names = "--exclude-mods", paramLabel = "PROJECT_ID|SLUG",
105-
split = "\\s+|,", splitSynopsisLabel = ",|<ws>",
105+
split = McImageHelper.SPLIT_COMMA_WS, splitSynopsisLabel = McImageHelper.SPLIT_SYNOPSIS_COMMA_WS,
106106
description = "For mods that need to be excluded from server deployments, such as those that don't label as client"
107107
)
108108
Set<String> excludedMods;
109109

110110
@Option(names = "--force-include-mods", paramLabel = "PROJECT_ID|SLUG",
111-
split = "\\s+|,", splitSynopsisLabel = ",|<ws>",
111+
split = McImageHelper.SPLIT_COMMA_WS, splitSynopsisLabel = McImageHelper.SPLIT_SYNOPSIS_COMMA_WS,
112112
description = "Some mods incorrectly declare client-only support, but still need to be included in a server deploy."
113113
+ "%nThis can also be used to selectively override exclusions."
114114
)

0 commit comments

Comments
 (0)