Skip to content

Commit

Permalink
Move from sysprops to cmdline options
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed May 30, 2024
1 parent 18cc24a commit 994a8cc
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/main/java/net/neoforged/camelot/BotMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@
import net.neoforged.camelot.log.ModerationActionRecorder;
import net.neoforged.camelot.module.CamelotModule;
import net.neoforged.camelot.module.StatsModule;
import net.neoforged.camelot.script.ScriptOptions;
import net.neoforged.camelot.util.AuthUtil;
import net.neoforged.camelot.util.Utils;
import net.neoforged.camelot.util.jda.ButtonManager;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.jdbi.v3.core.extension.ExtensionConsumer;
import org.jetbrains.annotations.NotNull;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.ExampleMode;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.ParserProperties;
import org.kohsuke.github.GitHubBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.http.HttpClient;
import java.nio.file.Files;
Expand Down Expand Up @@ -158,6 +165,27 @@ public static JDA awaitReady() {
}

public static void main(String[] args) {
final var cliConfig = new CliConfig();
final var parser = new CmdLineParser(cliConfig, ParserProperties.defaults()
.withUsageWidth(100));

try {
parser.parseArgument(args);
} catch (CmdLineException e) {
System.err.println(Common.NAME_WITH_VERSION);
System.err.println(e.getMessage());
System.err.println("java -jar camelot.jar [options...]");
parser.printUsage(System.err);
return;
}

if (cliConfig.help) {
System.err.println(Common.NAME_WITH_VERSION);
System.err.println("java -jar camelot.jar [options...]");
parser.printUsage(System.err);
return;
}

LOGGER.info("Starting {}", Common.NAME_WITH_VERSION);

GHAuth.AppAuthBuilder.setAppProvider(builder -> {
Expand All @@ -178,7 +206,7 @@ public static void main(String[] args) {
.collect(Collectors.toMap(
CamelotModule::id,
Function.identity(),
(a, b) -> b,
(_, b) -> b,
IdentityHashMap::new
));

Expand All @@ -190,7 +218,8 @@ public static void main(String[] args) {
Function.identity()
))
));
loadConfig();

loadConfig(cliConfig.config.toPath());

modules = Map.copyOf(allModules.values().stream()
.filter(module -> module.config().isEnabled() && module.shouldLoad())
Expand Down Expand Up @@ -256,8 +285,7 @@ public static void main(String[] args) {
}, 1, 1, TimeUnit.MINUTES);
}

private static void loadConfig() {
final var config = Path.of(System.getProperty("camelot.config", "camelot.groovy"));
private static void loadConfig(Path config) {
if (!Files.isRegularFile(config)) {
LOGGER.warn("No camelot configuration found at {}", config.toAbsolutePath());
final var oldConfigs = Path.of("config.properties");
Expand Down Expand Up @@ -314,4 +342,12 @@ public static <T extends StatsDAO> void stats(Class<T> type, ExtensionConsumer<T
module.use(type, dao);
}
}

public static class CliConfig {
@Option(name = "--config", aliases = "-c", usage = "the path to the config file")
private File config = new File("camelot.groovy");

@Option(name = "--help", aliases = "-h", usage = "print help options", help = true, hidden = true)
private boolean help = false;
}
}

0 comments on commit 994a8cc

Please sign in to comment.