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

62 lines
2.1 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.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.common.MetaHolder;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.launcher.model.inceptum.InstanceMeta;
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));
2022-01-04 23:32:05 +01:00
Path instanceMetaPath = instancePath.resolve("instance.json");
if (!Files.exists(instanceMetaPath)) {
Utils.LOGGER.error("Invalid instance: \"" + args.get(0) + "\"");
2022-01-04 23:32:05 +01:00
return;
}
InstanceMeta meta;
try {
2022-09-04 21:21:24 +02:00
meta = JFiles.readObject(instanceMetaPath, InstanceMeta.class);
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(), instancePath, meta);
} 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, Path instancePath, InstanceMeta meta) throws Exception;
2022-01-04 23:32:05 +01:00
}