agirstatool/src/org/april/agirstatool/cli/AgirStatoolCLI.java

270 lines
7.5 KiB
Java
Raw Normal View History

2020-01-05 17:24:43 +01:00
/*
* Copyright (C) 2020 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.april.agirstatool.cli;
import java.io.File;
import java.sql.Connection;
2020-01-23 08:56:01 +01:00
import java.time.LocalDateTime;
2020-01-05 17:24:43 +01:00
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 <code>JugaCLI</code> manages a Command Line Interface for Juga.
*
*/
2020-01-23 14:33:48 +01:00
public final class AgirStatoolCLI
2020-01-05 17:24:43 +01:00
{
2020-01-23 14:33:48 +01:00
private static Logger logger = LoggerFactory.getLogger(AgirStatoolCLI.class);
2020-01-05 17:24:43 +01:00
/**
* Instantiates a new JugaCLI.
*/
2020-01-23 14:33:48 +01:00
private AgirStatoolCLI()
2020-01-05 17:24:43 +01:00
{
}
/**
2020-01-30 16:43:20 +01:00
* Display help.
2020-01-05 17:24:43 +01:00
*/
2020-01-30 16:43:20 +01:00
public static void displayHelp()
2020-01-05 17:24:43 +01:00
{
StringList message = new StringList();
message.append("AgirStatool CLI version ").appendln(BuildInformation.instance().version());
message.appendln("Usage:");
message.appendln(" agirstatool [ -h | -help | --help ]");
2020-01-30 16:43:20 +01:00
message.appendln(" agirstatool -c configurationFilename [ clear | projects | projects+ | update | forceupdate ]");
message.appendln(" agirstatool [ -v | -version | --version ]");
2020-01-05 17:24:43 +01:00
2020-01-30 16:43:20 +01:00
logger.info(message.toString());
2020-01-05 17:24:43 +01:00
}
/**
2020-01-30 16:43:20 +01:00
* Display version.
*/
public static void displayVersion()
{
StringList message = new StringList();
message.appendln(BuildInformation.instance().version());
logger.info(message.toString());
}
/**
* Checks if is matching.
*
2020-01-05 17:24:43 +01:00
* @param args
2020-01-30 16:43:20 +01:00
* the args
* @param regexps
* the regexps
* @return true, if is matching
2020-01-05 17:24:43 +01:00
*/
2020-01-30 16:43:20 +01:00
public static boolean isMatching(final String[] args, final String... regexps)
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
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)
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
result = false;
2020-01-05 17:24:43 +01:00
}
else
{
2020-01-30 16:43:20 +01:00
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;
}
}
2020-01-05 17:24:43 +01:00
}
2020-01-30 16:43:20 +01:00
//
return result;
2020-01-05 17:24:43 +01:00
}
/**
*
* 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.");
}
});
2020-01-23 09:20:51 +01:00
logger.info("{} AgirStatool call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" "));
2020-01-23 08:56:01 +01:00
2020-01-30 16:43:20 +01:00
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)"))
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
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())
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
logger.info("Applying configuration file found in [{}].", log4jfile.getAbsoluteFile());
PropertyConfigurator.configure(log4jfile.getAbsolutePath());
logger.info("Configuration log configuration done.");
2020-01-05 17:24:43 +01:00
}
2020-01-30 16:43:20 +01:00
switch (action)
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
case "clear":
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
logger.info("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();
2020-01-05 17:24:43 +01:00
}
2020-01-30 16:43:20 +01:00
break;
case "projects":
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
logger.info("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);
2020-01-05 17:24:43 +01:00
}
2020-01-30 16:43:20 +01:00
break;
2020-01-05 17:24:43 +01:00
2020-01-30 16:43:20 +01:00
case "projects+":
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
logger.info("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);
2020-01-05 17:24:43 +01:00
}
2020-01-30 16:43:20 +01:00
break;
case "update":
2020-01-05 17:24:43 +01:00
{
2020-01-30 16:43:20 +01:00
logger.info("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.doRefreshPages();
2020-01-05 17:24:43 +01:00
}
2020-01-30 16:43:20 +01:00
break;
2020-01-05 17:24:43 +01:00
2020-01-30 16:43:20 +01:00
case "forceupdate":
{
logger.info("Force 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.doClearAllPages();
statool.doRefreshPages();
}
break;
2020-01-05 17:24:43 +01:00
}
}
2020-01-30 16:43:20 +01:00
else
{
logger.info("Bad usage.");
displayHelp();
}
2020-01-30 16:43:20 +01:00
//
logger.info("Finished.");
2020-01-05 17:24:43 +01:00
}
catch (AgirStatoolException exception)
{
System.err.println("AgirStatoolException = " + exception.getMessage());
logger.error(exception.getMessage(), exception);
2020-01-30 16:43:20 +01:00
displayHelp();
2020-01-05 17:24:43 +01:00
}
}
}