Inceptum/launcher-cli/src/main/java/io/gitlab/jfronny/inceptum/cli/BaseInstanceCommand.java

61 lines
2.0 KiB
Java
Raw Normal View History

2022-09-04 21:21:24 +02:00
package io.gitlab.jfronny.inceptum.cli;
2022-01-04 23:32:05 +01:00
2022-09-04 21:21:24 +02:00
import io.gitlab.jfronny.inceptum.common.MetaHolder;
import io.gitlab.jfronny.inceptum.common.Utils;
2022-10-27 20:54:55 +02:00
import io.gitlab.jfronny.inceptum.launcher.system.instance.Instance;
import io.gitlab.jfronny.inceptum.launcher.system.instance.InstanceList;
2022-01-04 23:32:05 +01:00
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public abstract class BaseInstanceCommand extends Command {
public BaseInstanceCommand(String help, String usage, String... aliases) {
super(help, mutateUsage(usage), aliases);
}
public BaseInstanceCommand(String help, String usage, List<String> aliases, List<Command> subCommands) {
super(help, mutateUsage(usage), aliases, subCommands);
}
private static String mutateUsage(String usage) {
StringBuilder sb = new StringBuilder();
for (String s : usage.split("\n")) {
if (s.isBlank())
sb.append("\n<instance>");
else
sb.append("\n<instance> ").append(s);
}
return sb.substring(1);
}
@Override
protected void invoke(CommandArgs args) {
if (args.length == 0) {
Utils.LOGGER.error("You must specify an instance to commit in");
2022-01-04 23:32:05 +01:00
return;
}
Path instancePath = MetaHolder.INSTANCE_DIR.resolve(args.get(0));
if (!Files.exists(instancePath)) {
Utils.LOGGER.error("Invalid instance: \"" + args.get(0) + "\"");
2022-01-04 23:32:05 +01:00
return;
}
Instance instance;
2022-01-04 23:32:05 +01:00
try {
instance = InstanceList.read(instancePath);
2022-01-04 23:32:05 +01:00
} catch (IOException e) {
Utils.LOGGER.error("Could not read instance metadata", e);
2022-01-04 23:32:05 +01:00
return;
}
try {
invoke(args.subArgs(), instance);
} catch (Exception e) {
Utils.LOGGER.error("Could not execute command", e);
return;
}
2022-01-04 23:32:05 +01:00
}
protected abstract void invoke(CommandArgs args, Instance instance) throws Exception;
2022-01-04 23:32:05 +01:00
}