Inceptum/launcher-dist/src/main/java/io/gitlab/jfronny/inceptum/UpdateCheckCommand.java
2022-09-06 14:55:36 +02:00

50 lines
1.6 KiB
Java

package io.gitlab.jfronny.inceptum;
import io.gitlab.jfronny.inceptum.cli.Command;
import io.gitlab.jfronny.inceptum.cli.CommandArgs;
import io.gitlab.jfronny.inceptum.common.*;
import io.gitlab.jfronny.inceptum.common.model.inceptum.UpdateMetadata;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
public class UpdateCheckCommand extends Command {
private final boolean install;
public UpdateCheckCommand() {
super("Checks for inceptum updates", "", List.of("update"), List.of(
new UpdateCheckCommand("Automatically install updates", "install", true)
));
install = false;
}
private UpdateCheckCommand(String help, String name, boolean install) {
super(help, "", name);
this.install = install;
}
@Override
protected void invoke(CommandArgs args) {
if (install && !MetaHolder.isWrapper()) {
Utils.LOGGER.error("Automatic updates are not supported without the wrapper");
return;
}
UpdateMetadata updateUrl = BuildMetadata.IS_PUBLIC ? Updater.getUpdate() : null;
if (updateUrl == null) {
Utils.LOGGER.info("No update was found");
} else {
if (install) {
Utils.LOGGER.info("Installing from " + updateUrl);
try {
Updater.update(updateUrl, false);
} catch (IOException | URISyntaxException e) {
Utils.LOGGER.error("Could not download update", e);
}
} else {
Utils.LOGGER.info("An update was found: " + updateUrl);
}
}
}
}