/* * Copyright (C) 2020 Christian Pierre MOMON * * This file is part of AgirStatool, simple key value database. * * AgirStatool is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * AgirStatool is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with AgirStatool. If not, see . */ package org.april.agirstatool.cli; import java.io.File; import java.sql.Connection; import java.time.LocalDateTime; import org.apache.log4j.PropertyConfigurator; import org.april.agirstatool.core.AgirStatool; import org.april.agirstatool.core.AgirStatoolException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.strings.StringList; import utils.BuildInformation; /** * The Class JugaCLI manages a Command Line Interface for Juga. * */ public final class AgirStatoolCLI { private static Logger logger = LoggerFactory.getLogger(AgirStatoolCLI.class); /** * Instantiates a new JugaCLI. */ private AgirStatoolCLI() { } /** * Display help. */ public static void displayHelp() { StringList message = new StringList(); message.append("AgirStatool CLI version ").appendln(BuildInformation.instance().version()); message.appendln("Usage:"); message.appendln(" agirstatool [ -h | -help | --help ]"); message.appendln(" agirstatool -c configurationFilename [ clear | projects | projects+ | update | forceupdate ]"); message.appendln(" agirstatool [ -v | -version | --version ]"); logger.info(message.toString()); } /** * Display version. */ public static void displayVersion() { StringList message = new StringList(); message.appendln(BuildInformation.instance().version()); logger.info(message.toString()); } /** * Checks if is matching. * * @param args * the args * @param regexps * the regexps * @return true, if is matching */ public static boolean isMatching(final String[] args, final String... regexps) { boolean result; if ((args.length == 0) && (regexps == null)) { result = true; } else if ((args.length != 0) && (regexps == null)) { result = false; } else if (args.length != regexps.length) { result = false; } else { boolean ended = false; int index = 0; result = false; while (!ended) { if (index < args.length) { String arg = args[index]; String regexp = regexps[index]; if (arg.matches(regexp)) { index += 1; } else { ended = true; result = false; } } else { ended = true; result = true; } } } // return result; } /** * * This method launch CLI. * * @param args * necessary arguments */ public static void run(final String[] args) { try { // Set default catch. Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(final Thread thread, final Throwable exception) { String message; if (exception instanceof OutOfMemoryError) { message = "Java ran out of memory!\n\n"; } else { message = String.format("An error occured: %1s(%2s)", exception.getClass(), exception.getMessage()); } logger.error("uncaughtException ", exception); logger.error(message); logger.info("Oups, an unexpected error occured. Please try again."); } }); logger.info("{} AgirStatool call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" ")); if (isMatching(args, (String) null)) { logger.info("No parameter."); displayHelp(); } else if (isMatching(args, "(-h|--h|--help)")) { displayHelp(); } else if (isMatching(args, "(-v|-version|--version)")) { displayVersion(); } else if (isMatching(args, "-c", ".+", "(clear|projects|projects\\+|update|forceupdate)")) { File configurationFile = new File(args[1]); String action = args[2]; File log4jfile = new File(configurationFile.getParentFile(), "log4j.properties"); if (log4jfile.exists()) { logger.info("Applying configuration file found in [{}].", log4jfile.getAbsoluteFile()); PropertyConfigurator.configure(log4jfile.getAbsolutePath()); logger.info("Configuration log configuration done."); } switch (action) { case "clear": { logger.info("Applying clear command…"); AgirStatoolConfigFile config = new AgirStatoolConfigFile(configurationFile); Connection connection = SQLUtils.getConnexion(config.getDabataseUrl(), config.getDabataseName(), config.getDabataseLogin(), config.getDabatasePassword()); AgirStatool statool = new AgirStatool(connection, new File(config.getTargetDirectory())); statool.doClearAllPages(); } break; case "forceupdate": { logger.info("Applying forceupdate command…"); AgirStatoolConfigFile config = new AgirStatoolConfigFile(configurationFile); Connection connection = SQLUtils.getConnexion(config.getDabataseUrl(), config.getDabataseName(), config.getDabataseLogin(), config.getDabatasePassword()); AgirStatool statool = new AgirStatool(connection, new File(config.getTargetDirectory())); statool.doClearAllPages(); statool.doUpdatePages(); } break; case "projects": { logger.info("Applying projects command…"); AgirStatoolConfigFile config = new AgirStatoolConfigFile(configurationFile); Connection connection = SQLUtils.getConnexion(config.getDabataseUrl(), config.getDabataseName(), config.getDabataseLogin(), config.getDabatasePassword()); AgirStatool statool = new AgirStatool(connection, new File(config.getTargetDirectory())); StringList lines = statool.doBuildTextProjectList(); System.out.println(lines); } break; case "projects+": { logger.info("Applying projects+ command…"); AgirStatoolConfigFile config = new AgirStatoolConfigFile(configurationFile); Connection connection = SQLUtils.getConnexion(config.getDabataseUrl(), config.getDabataseName(), config.getDabataseLogin(), config.getDabatasePassword()); AgirStatool statool = new AgirStatool(connection, new File(config.getTargetDirectory())); StringList lines = statool.doBuildTextProjectExtendedList(); System.out.println(lines); } break; case "update": { logger.info("Applying update command…"); AgirStatoolConfigFile config = new AgirStatoolConfigFile(configurationFile); Connection connection = SQLUtils.getConnexion(config.getDabataseUrl(), config.getDabataseName(), config.getDabataseLogin(), config.getDabatasePassword()); AgirStatool statool = new AgirStatool(connection, new File(config.getTargetDirectory())); statool.doUpdatePages(); } break; } } else { logger.info("Bad usage."); displayHelp(); } // logger.info("Finished."); } catch (AgirStatoolException exception) { System.err.println("AgirStatoolException = " + exception.getMessage()); logger.error(exception.getMessage(), exception); displayHelp(); } } }