Add config validators & tests

This commit is contained in:
2022-09-13 18:58:16 +02:00
parent b5030f9401
commit 1d6098efc1
15 changed files with 250 additions and 161 deletions

View File

@@ -58,64 +58,6 @@ public class Config {
return config;
}
private Pattern loadIncludePattern(CommandLine cmd, YAML config, ConfigErrors errors) {
try {
return Pattern.compile(cmd.hasOption(INCLUDE_PATTERN.prop())
? cmd.getOptionValue(INCLUDE_PATTERN.prop())
: config.getString(INCLUDE_PATTERN.prop(), ".*"));
} catch (PatternSyntaxException e) {
errors.add("invalid regex pattern");
}
return null;
}
@SneakyThrows
private String loadMkvToolNixPath(CommandLine cmd, YAML config, ConfigErrors errors){
if (cmd.hasOption(MKV_TOOL_NIX.prop())) return cmd.getOptionValue(MKV_TOOL_NIX.prop());
if (config.isSet(MKV_TOOL_NIX.prop())) return config.getString(MKV_TOOL_NIX.prop());
errors.add("path to mkv tool nix installation missing");
return null;
}
private boolean loadOperatingSystem() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
@SneakyThrows
private void loadForcedKeywords(CommandLine cmd, YAML config) {
if (cmd.hasOption(FORCED_KEYWORDS.prop())) forcedKeywords.addAll(List.of(cmd.getOptionValues(FORCED_KEYWORDS.prop())));
if (config.isSet(FORCED_KEYWORDS.prop())) forcedKeywords.addAll(config.getStringList(FORCED_KEYWORDS.prop()));
}
@SneakyThrows
private void loadExcludedDirectories(CommandLine cmd, YAML config) {
if (cmd.hasOption(EXCLUDE_DIRECTORY.prop())) excludedDirectories.addAll(List.of(cmd.getOptionValues(EXCLUDE_DIRECTORY.prop())));
if (config.isSet(EXCLUDE_DIRECTORY.prop())) excludedDirectories.addAll(config.getStringList(EXCLUDE_DIRECTORY.prop()));
}
private List<AttributeConfig> loadAttributeConfig(YAML config, ConfigErrors errors) {
Function<String, String> audio = key -> config.getString(key + ".audio", null);
Function<String, String> subtitle = key -> config.getString(key + ".subtitle", null);
List<AttributeConfig> attributeConfigs = config.getKeysFiltered(".*audio.*").stream()
.sorted()
.map(key -> key.replace(".audio", ""))
.map(key -> new AttributeConfig(audio.apply(key), subtitle.apply(key)))
.collect(Collectors.toList());
if (attributeConfigs.isEmpty()) {
errors.add("no language configuration");
} else {
for (AttributeConfig attributeConfig : attributeConfigs) {
isLanguageValid(attributeConfig.getAudioLanguage(), errors);
isLanguageValid(attributeConfig.getSubtitleLanguage(), errors);
}
}
return attributeConfigs;
}
public String getPathFor(MkvToolNix exe) {
return mkvToolNix.getAbsolutePath().endsWith("/") ? mkvToolNix.getAbsolutePath() + exe + ".exe" :
mkvToolNix.getAbsolutePath() + "/" + exe + ".exe";