mirror of
https://github.com/RatzzFatzz/MKVAudioSubtitleChanger.git
synced 2026-02-12 02:25:59 +01:00
Add linux compatibility & Finalize Config Loader
This commit is contained in:
@@ -54,9 +54,15 @@ public class Config {
|
||||
return config;
|
||||
}
|
||||
|
||||
public String getPathFor(MkvToolNix exe) {
|
||||
return mkvToolNix.getAbsolutePath().endsWith("/") ? mkvToolNix.getAbsolutePath() + exe + ".exe" :
|
||||
mkvToolNix.getAbsolutePath() + "/" + exe + ".exe";
|
||||
/**
|
||||
* Get path to specific mkvtoolnix application.
|
||||
*
|
||||
* @return absolute path to desired application.
|
||||
*/
|
||||
public String getPathFor(MkvToolNix application) {
|
||||
String executable = isWindows() ? application + ".exe" : application.toString();
|
||||
return mkvToolNix.getAbsolutePath().endsWith("/") ? mkvToolNix.getAbsolutePath() + executable :
|
||||
mkvToolNix.getAbsolutePath() + "/" + executable;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,9 +17,8 @@ import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty.*;
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.util.CommandLineOptionsUtil.optionOf;
|
||||
|
||||
public class ConfigLoader {
|
||||
private static final ConfigValidator<?> CONFIG_VALIDATOR =
|
||||
new ConfigPathValidator(CONFIG_PATH, false, Path.of("./config.yaml").toFile());
|
||||
private static final List<ConfigValidator<?>> VALIDATORS = List.of(
|
||||
new ConfigPathValidator(CONFIG_PATH, false, Path.of("./config.yaml").toFile()),
|
||||
new PathValidator(LIBRARY, true, null),
|
||||
new ThreadValidator(THREADS, false, 2),
|
||||
new MkvToolNixPathValidator(MKV_TOOL_NIX, true, Path.of("C:\\Program Files\\MKVToolNix").toFile()),
|
||||
@@ -34,20 +33,26 @@ public class ConfigLoader {
|
||||
|
||||
public static void initConfig(String[] args) {
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
YAML yamlConfig = null;
|
||||
|
||||
Options options = initOptions();
|
||||
CommandLine cmd = parseCommandLineArgs(formatter, options, args);
|
||||
|
||||
exitIfHelp(cmd, options, formatter);
|
||||
exitIfVersion(cmd);
|
||||
exitIfConfigIsMissing(cmd);
|
||||
|
||||
List<ValidationResult> results = new ArrayList<>();
|
||||
try (YAML config = new YAML(Config.getInstance().getConfigPath())) {
|
||||
for (ConfigValidator<?> validator : VALIDATORS) {
|
||||
results.add(validator.validate(config, cmd));
|
||||
|
||||
for (ConfigValidator<?> validator: VALIDATORS) {
|
||||
results.add(validator.validate(yamlConfig, cmd));
|
||||
if (yamlConfig == null && Config.getInstance().getConfigPath() != null) {
|
||||
try {
|
||||
yamlConfig = Config.getInstance().getConfigPath() != null
|
||||
? new YAML(Config.getInstance().getConfigPath())
|
||||
: new YAML("");
|
||||
} catch (IOException | YamlInvalidContentException ignored) {}
|
||||
}
|
||||
} catch (IOException | YamlInvalidContentException ignored) {}
|
||||
}
|
||||
|
||||
if (results.contains(ValidationResult.INVALID)) System.exit(1);
|
||||
System.out.println();
|
||||
@@ -55,16 +60,16 @@ public class ConfigLoader {
|
||||
|
||||
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));
|
||||
options.addOption(optionOf(HELP, HELP.abrv(), HELP.args()));
|
||||
options.addOption(optionOf(VERSION, VERSION.abrv(), VERSION.args()));
|
||||
options.addOption(optionOf(LIBRARY, LIBRARY.abrv(), LIBRARY.args() ));
|
||||
options.addOption(optionOf(MKV_TOOL_NIX, MKV_TOOL_NIX.abrv(), MKV_TOOL_NIX.args() ));
|
||||
options.addOption(optionOf(CONFIG_PATH, CONFIG_PATH.abrv(), CONFIG_PATH.args() ));
|
||||
options.addOption(optionOf(THREADS, THREADS.abrv(), THREADS.args()));
|
||||
options.addOption(optionOf(SAFE_MODE, SAFE_MODE.abrv(), SAFE_MODE.args() ));
|
||||
options.addOption(optionOf(FORCED_KEYWORDS, FORCED_KEYWORDS.abrv(), FORCED_KEYWORDS.args()));
|
||||
options.addOption(optionOf(EXCLUDE_DIRECTORY, FORCED_KEYWORDS.abrv(), FORCED_KEYWORDS.args()));
|
||||
options.addOption(optionOf(INCLUDE_PATTERN, INCLUDE_PATTERN.abrv(), INCLUDE_PATTERN.args()));
|
||||
return options;
|
||||
}
|
||||
|
||||
@@ -100,13 +105,6 @@ public class ConfigLoader {
|
||||
}
|
||||
}
|
||||
|
||||
private static void exitIfConfigIsMissing(CommandLine cmd) {
|
||||
if (CONFIG_VALIDATOR.validate(null, cmd).equals(ValidationResult.INVALID)) {
|
||||
System.out.println("\nPlease use a valid config path!");
|
||||
System.exit(0);
|
||||
};
|
||||
}
|
||||
|
||||
private static File loadConfigPath(CommandLine cmd) {
|
||||
File configPath = new File(cmd.getOptionValue(CONFIG_PATH.prop(), "config.yaml"));
|
||||
if (configPath.isFile()) return configPath;
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public enum ValidationResult {
|
||||
VALID,
|
||||
DEFAULT,
|
||||
|
||||
@@ -18,6 +18,9 @@ public class AttributeConfigValidator extends ConfigValidator<List<AttributeConf
|
||||
super(ConfigProperty.ATTRIBUTE_CONFIG, true, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||
System.out.printf("%s: ", property.prop());
|
||||
List<AttributeConfig> result;
|
||||
@@ -48,11 +51,17 @@ public class AttributeConfigValidator extends ConfigValidator<List<AttributeConf
|
||||
return ValidationResult.VALID;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
List<AttributeConfig> parse(String value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
boolean isValid(List<AttributeConfig> result) {
|
||||
if (result.isEmpty()) {
|
||||
|
||||
@@ -18,6 +18,9 @@ public class BooleanValidator extends ConfigValidator<Boolean> {
|
||||
super(property, required, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected BiFunction<YAML, ConfigProperty, Optional<Boolean>> provideDataYaml() {
|
||||
return (yaml, property) -> {
|
||||
if (yaml.isSet(ARGUMENTS.prop())
|
||||
@@ -28,6 +31,9 @@ public class BooleanValidator extends ConfigValidator<Boolean> {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected BiFunction<CommandLine, ConfigProperty, Optional<Boolean>> provideDataCmd() {
|
||||
return (cmd, property) -> {
|
||||
if (cmd.hasOption(property.prop())) {
|
||||
@@ -37,11 +43,19 @@ public class BooleanValidator extends ConfigValidator<Boolean> {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* This should not be used.
|
||||
*/
|
||||
@Override
|
||||
Boolean parse(String value) {
|
||||
throw new RuntimeException("This should not be called");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* Validation is skipped.
|
||||
*/
|
||||
@Override
|
||||
boolean isValid(Boolean result) {
|
||||
return true; // skip
|
||||
|
||||
@@ -12,11 +12,17 @@ public class ConfigPathValidator extends PathValidator {
|
||||
super(property, required, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected BiFunction<YAML, ConfigProperty, Optional<File>> provideDataYaml() {
|
||||
return (yaml, property) -> Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected boolean isValid(File result) {
|
||||
return super.isValid(result) && (result.getAbsolutePath().endsWith(".yml") || result.getAbsolutePath().endsWith(".yaml"));
|
||||
|
||||
@@ -24,6 +24,13 @@ public abstract class ConfigValidator<FieldType> {
|
||||
protected final boolean required;
|
||||
protected final FieldType defaultValue;
|
||||
|
||||
/**
|
||||
* Validate the user input. Parameters of cmd are prioritised.
|
||||
*
|
||||
* @param yaml config file
|
||||
* @param cmd command line parameters
|
||||
* @return {@link ValidationResult} containing validity of input.
|
||||
*/
|
||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||
System.out.printf("%s: ", property.prop());
|
||||
FieldType result;
|
||||
@@ -63,6 +70,9 @@ public abstract class ConfigValidator<FieldType> {
|
||||
return ValidationResult.VALID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return parsed input of yaml config for property
|
||||
*/
|
||||
protected BiFunction<YAML, ConfigProperty, Optional<FieldType>> provideDataYaml() {
|
||||
return (yaml, property) -> {
|
||||
if (yaml.isSet(property.prop())) {
|
||||
@@ -76,6 +86,9 @@ public abstract class ConfigValidator<FieldType> {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return parsed input of command line parameters config for property
|
||||
*/
|
||||
protected BiFunction<CommandLine, ConfigProperty, Optional<FieldType>> provideDataCmd() {
|
||||
return (cmd, property) -> {
|
||||
if (cmd.hasOption(property.prop())) {
|
||||
@@ -85,10 +98,28 @@ public abstract class ConfigValidator<FieldType> {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse input parameter to desired format.
|
||||
*
|
||||
* @param value input parameter
|
||||
* @return parsed property
|
||||
*/
|
||||
abstract FieldType parse(String value);
|
||||
|
||||
/**
|
||||
* Validate if the data has the desired and allowed format.
|
||||
*
|
||||
* @param result parsed property
|
||||
* @return true if data is in desired format.
|
||||
*/
|
||||
abstract boolean isValid(FieldType result);
|
||||
|
||||
/**
|
||||
* Sets valid properties to {@link Config} via reflections.
|
||||
*
|
||||
* @param result parsed property
|
||||
* @return false if method invocation failed
|
||||
*/
|
||||
protected boolean setValue(FieldType result) {
|
||||
List<Method> methods = Arrays.stream(Config.getInstance().getClass().getDeclaredMethods())
|
||||
.filter(containsSetterOf(property))
|
||||
|
||||
@@ -9,6 +9,8 @@ import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix.MKV_MER
|
||||
import static at.pcgamingfreaks.mkvaudiosubtitlechanger.model.MkvToolNix.MKV_PROP_EDIT;
|
||||
|
||||
public class MkvToolNixPathValidator extends PathValidator {
|
||||
private static final String EXE = ".exe";
|
||||
|
||||
public MkvToolNixPathValidator(ConfigProperty property, boolean required, File defaultValue) {
|
||||
super(property, required, defaultValue);
|
||||
}
|
||||
@@ -16,8 +18,9 @@ public class MkvToolNixPathValidator extends PathValidator {
|
||||
@Override
|
||||
protected boolean isValid(File result) {
|
||||
return result.isDirectory()
|
||||
&& Path.of(result.getAbsolutePath() + "/" + MKV_MERGER + ".exe").toFile().isFile()
|
||||
&& Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT+ ".exe").toFile().isFile();
|
||||
// TODO: make linux compatible
|
||||
&& (Path.of(result.getAbsolutePath() + "/" + MKV_MERGER + EXE).toFile().isFile()
|
||||
&& Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT + EXE).toFile().isFile())
|
||||
|| (Path.of(result.getAbsolutePath() + "/" + MKV_MERGER).toFile().isFile()
|
||||
&& Path.of(result.getAbsolutePath() + "/" + MKV_PROP_EDIT).toFile().isFile());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ public class OperatingSystemValidator extends BooleanValidator {
|
||||
super(property, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather operating system from system properties.
|
||||
*/
|
||||
@Override
|
||||
public ValidationResult validate(YAML yaml, CommandLine cmd) {
|
||||
System.out.printf("%s: ", property.prop());
|
||||
|
||||
@@ -11,11 +11,17 @@ public class PathValidator extends ConfigValidator<File> {
|
||||
super(property, required, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected File parse(String value) {
|
||||
return Path.of(value).toFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected boolean isValid(File result) {
|
||||
return result.isDirectory() || result.isFile();
|
||||
|
||||
@@ -12,6 +12,9 @@ public class PatternValidator extends ConfigValidator<Pattern> {
|
||||
super(property, required, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
Pattern parse(String value) {
|
||||
try {
|
||||
@@ -21,6 +24,9 @@ public class PatternValidator extends ConfigValidator<Pattern> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
boolean isValid(Pattern result) {
|
||||
return !result.equals(EMPTY_PATTERN);
|
||||
|
||||
@@ -35,7 +35,7 @@ public class SetValidator extends ConfigValidator<Set<String>> {
|
||||
System.out.println("missing");
|
||||
return ValidationResult.MISSING;
|
||||
} else {
|
||||
System.out.println("not present");
|
||||
System.out.println("ok");
|
||||
return ValidationResult.NOT_PRESENT;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,17 @@ public class ThreadValidator extends ConfigValidator<Integer>{
|
||||
super(property, required, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
Integer parse(String value) {
|
||||
return NumberUtils.isParsable(value) ? Integer.parseInt(value) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
boolean isValid(Integer result) {
|
||||
return result > 0;
|
||||
|
||||
Reference in New Issue
Block a user