mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-11 02:05:56 +01:00
Add config validators
This commit is contained in:
@@ -5,18 +5,13 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix;
|
||||
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.VersionUtil;
|
||||
import at.pcgamingfreaks.yaml.YAML;
|
||||
import at.pcgamingfreaks.yaml.YamlInvalidContentException;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.*;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.commons.cli.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
@@ -28,6 +23,8 @@ import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.LanguageValidatorUt
|
||||
|
||||
@Log4j2
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class Config {
|
||||
@Getter(AccessLevel.NONE)
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
@@ -35,18 +32,18 @@ public class Config {
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
|
||||
@Getter(AccessLevel.NONE)
|
||||
@Setter(AccessLevel.NONE)
|
||||
private static Config config = null;
|
||||
|
||||
private File configPath;
|
||||
private String libraryPath;
|
||||
private boolean isSafeMode;
|
||||
|
||||
private int threadCount;
|
||||
private Pattern includePattern;
|
||||
private File libraryPath;
|
||||
@Getter(AccessLevel.NONE)
|
||||
private String mkvToolNixPath;
|
||||
private File mkvToolNix;
|
||||
|
||||
private boolean isWindows;
|
||||
private int threads;
|
||||
private Pattern includePattern;
|
||||
private boolean windows;
|
||||
private boolean safeMode;
|
||||
|
||||
private final Set<String> forcedKeywords = new HashSet<>(Arrays.asList("forced", "signs"));
|
||||
private final Set<String> commentaryKeywords = new HashSet<>(Arrays.asList("commentary", "director"));
|
||||
@@ -61,104 +58,6 @@ public class Config {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void initConfig(String[] args) throws InvalidConfigException {
|
||||
ConfigErrors errors = new ConfigErrors();
|
||||
CommandLine cmd = null;
|
||||
Options options = initOptions();
|
||||
|
||||
try {
|
||||
cmd = parser.parse(options, args);
|
||||
if (cmd == null) throw new NullPointerException();
|
||||
} catch (ParseException | NullPointerException e) {
|
||||
formatter.printHelp(106, "java -jar MKVAudioSubtitlesChanger.jar -l <path_to_library>",
|
||||
"\nParameters:", options,
|
||||
"\nFeature requests and bug reports: https://github.com/RatzzFatzz/MKVAudioSubtitleChanger/issues");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
exitIfHelp(cmd, options);
|
||||
exitIfVersion(cmd);
|
||||
|
||||
configPath = loadConfigPath(cmd, errors);
|
||||
libraryPath = loadLibraryPath(cmd, errors);
|
||||
isSafeMode = cmd.hasOption(SAFE_MODE.prop());
|
||||
|
||||
try (YAML config = new YAML(configPath)) {
|
||||
threadCount = loadThreadCount(cmd, config);
|
||||
includePattern = loadIncludePattern(cmd, config, errors);
|
||||
mkvToolNixPath = loadMkvToolNixPath(cmd, config, errors);
|
||||
|
||||
isWindows = loadOperatingSystem();
|
||||
|
||||
loadForcedKeywords(cmd, config);
|
||||
loadExcludedDirectories(cmd, config);
|
||||
|
||||
attributeConfig = loadAttributeConfig(config, errors);
|
||||
} catch (IOException | YamlInvalidContentException ignored) {}
|
||||
|
||||
if (errors.hasErrors()) {
|
||||
throw new InvalidConfigException(errors);
|
||||
}
|
||||
}
|
||||
|
||||
private static Options initOptions() {
|
||||
Options options = new Options();
|
||||
options.addOption(optionOf(HELP, "h", false));
|
||||
options.addOption(optionOf(VERSION, "v", false));
|
||||
options.addOption(optionOf(LIBRARY, "l", true));
|
||||
options.addOption(optionOf(MKV_TOOL_NIX, "m", true));
|
||||
options.addOption(optionOf(CONFIG_PATH, "c", true));
|
||||
options.addOption(optionOf(THREADS, "t", true));
|
||||
options.addOption(optionOf(SAFE_MODE, "s", false));
|
||||
options.addOption(optionOf(FORCED_KEYWORDS, "k", Option.UNLIMITED_VALUES, false));
|
||||
options.addOption(optionOf(EXCLUDE_DIRECTORY, "e", Option.UNLIMITED_VALUES, false));
|
||||
options.addOption(optionOf(INCLUDE_PATTERN, "i", true));
|
||||
return options;
|
||||
}
|
||||
|
||||
private void exitIfHelp(CommandLine cmd, Options options) {
|
||||
if (cmd.hasOption("help")) {
|
||||
formatter.printHelp(106, "java -jar MKVAudioSubtitlesChanger.jar -l <path_to_library>",
|
||||
"\nParameters:", options,
|
||||
"\nFeature requests and bug reports: https://github.com/RatzzFatzz/MKVAudioSubtitleChanger/issues");
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void exitIfVersion(CommandLine cmd) {
|
||||
if (cmd.hasOption(VERSION.prop())) {
|
||||
System.out.printf("MKV Audio Subtitle Changer Version %s%n", VersionUtil.getVersion());
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
private File loadConfigPath(CommandLine cmd, ConfigErrors errors) {
|
||||
File configPath = new File(cmd.getOptionValue(CONFIG_PATH.prop(), "config.yaml"));
|
||||
if (configPath.isFile()) return configPath;
|
||||
|
||||
errors.add("invalid config path");
|
||||
return null;
|
||||
}
|
||||
|
||||
private String loadLibraryPath(CommandLine cmd, ConfigErrors errors) {
|
||||
if (cmd.hasOption(LIBRARY.prop())) {
|
||||
File libraryPath = new File(cmd.getOptionValue(LIBRARY.prop()));
|
||||
if (libraryPath.isFile() || libraryPath.isDirectory()) {
|
||||
return libraryPath.getAbsolutePath();
|
||||
} else {
|
||||
errors.add("invalid library path");
|
||||
}
|
||||
} else {
|
||||
errors.add("missing library path");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private int loadThreadCount(CommandLine cmd, YAML config) {
|
||||
return cmd.hasOption(THREADS.prop())
|
||||
? Integer.parseInt(cmd.getOptionValue(THREADS.prop()))
|
||||
: config.getInt(THREADS.prop(), 2);
|
||||
}
|
||||
|
||||
private Pattern loadIncludePattern(CommandLine cmd, YAML config, ConfigErrors errors) {
|
||||
try {
|
||||
@@ -218,7 +117,27 @@ public class Config {
|
||||
}
|
||||
|
||||
public String getPathFor(MkvToolNix exe) {
|
||||
return mkvToolNixPath.endsWith("/") ? mkvToolNixPath + exe : mkvToolNixPath + "/" + exe;
|
||||
return mkvToolNix.getAbsolutePath().endsWith("/") ? mkvToolNix.getAbsolutePath() + exe + ".exe" :
|
||||
mkvToolNix.getAbsolutePath() + "/" + exe + ".exe";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Config.class.getSimpleName() + "[", "]")
|
||||
.add("parser=" + parser).add("\n")
|
||||
.add("formatter=" + formatter).add("\n")
|
||||
.add("configPath=" + configPath).add("\n")
|
||||
.add("libraryPath=" + libraryPath).add("\n")
|
||||
.add("isWindows=" + windows).add("\n")
|
||||
.add("isSafeMode=" + safeMode).add("\n")
|
||||
.add("forcedKeywords=" + forcedKeywords).add("\n")
|
||||
.add("commentaryKeywords=" + commentaryKeywords).add("\n")
|
||||
.add("excludedDirectories=" + excludedDirectories).add("\n")
|
||||
.add("threadCount=" + threads).add("\n")
|
||||
.add("includePattern=" + includePattern).add("\n")
|
||||
.add("mkvToolNixPath='" + mkvToolNix + "'").add("\n")
|
||||
.add("attributeConfig=" + attributeConfig)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user