Refactored CLI launching.

This commit is contained in:
Christian P. MOMON 2020-01-30 16:43:20 +01:00
parent f6a4d2366b
commit 3a1a1e79d9
6 changed files with 161 additions and 371 deletions

View File

@ -20,13 +20,11 @@ package org.april.agirstatool;
import java.io.File; import java.io.File;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.BasicConfigurator; import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender; import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.EnhancedPatternLayout; import org.apache.log4j.EnhancedPatternLayout;
import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.PropertyConfigurator;
import org.april.agirstatool.cli.AgirStatoolCLI; import org.april.agirstatool.cli.AgirStatoolCLI;
import org.april.agirstatool.demo.AgirStatool;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -63,9 +61,6 @@ public final class AgirStatoolLauncher
else else
{ {
BasicConfigurator.configure(new ConsoleAppender(new EnhancedPatternLayout("%m%n"))); BasicConfigurator.configure(new ConsoleAppender(new EnhancedPatternLayout("%m%n")));
// logger.info("Basic log configuration done.");
// logger.info("Configuration file was not found in [{}].",
// loggerConfig.getAbsoluteFile());
} }
// Run. // Run.
@ -74,16 +69,8 @@ public final class AgirStatoolLauncher
// TODO // TODO
// SikevaGUI.run(); // SikevaGUI.run();
} }
else if (ArrayUtils.contains(args, "-demo"))
{
AgirStatool.run(args);
}
else else
{ {
// String[] foo = { "-c",
// "/home/cpm/Projets/AgirStatool/TestZone/agirstatool.conf",
// "listProjects" };
// AgiStatoolCLI.run(foo);
AgirStatoolCLI.run(args); AgirStatoolCLI.run(args);
} }
} }

View File

@ -1,225 +0,0 @@
/*
* 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;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
import org.april.agirstatool.core.AgirStatool;
import org.april.agirstatool.core.AgirStatoolException;
import org.april.agirstatool.core.Project;
import org.april.agirstatool.core.Projects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
import utils.BuildInformation;
/**
* The Class <code>JugaCLI</code> manages a Command Line Interface for Juga.
*
*/
public final class AgirConfig
{
private static Logger logger = LoggerFactory.getLogger(AgirConfig.class);
/**
* Instantiates a new JugaCLI.
*/
private AgirConfig()
{
}
/**
* This method displays the CLI help.
*
*/
public static void help()
{
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");
message.appendln(" agirstatool -c configurationFilename update");
message.appendln(" agirstatool -c configurationFilename projects");
System.out.println(message.toString());
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(final String[] args)
{
// Configure log.
File loggerConfig = new File("log4j.properties");
if (loggerConfig.exists())
{
PropertyConfigurator.configure(loggerConfig.getAbsolutePath());
logger.info("Dedicated log configuration done.");
logger.info("Configuration file was found in [{}].", loggerConfig.getAbsoluteFile());
}
else
{
BasicConfigurator.configure();
logger.info("Basic log configuration done.");
logger.info("Configuration file was not found in [{}].", loggerConfig.getAbsoluteFile());
}
// Run.
AgirConfig.run(args);
}
/**
*
* 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("Single connection opened with [{}].", this.url);
System.out.println("ok");
// This part implements an automate.
int parameterCount = args.length;
switch (parameterCount)
{
case 0:
{
help();
}
break;
case 1:
{
String token = args[0];
if (StringsUtils.containsAny(token, "-h", "-help", "--help"))
{
help();
}
else
{
throw new AgirStatoolException("Bad usage.");
}
}
break;
case 3:
{
String token = args[0];
String configurationFilename = args[1];
String command = args[2];
if ((StringUtils.equals(token, "-c")) && (StringsUtils.containsAny(command, "clear", "update", "projects")))
{
File configurationFile = new File(configurationFilename);
if (!configurationFile.exists())
{
throw new AgirStatoolException("Configuration file does not exist.");
}
else if (!configurationFile.isFile())
{
throw new AgirStatoolException("Configuration file is not a file.");
}
else
{
switch (command)
{
case "clear":
break;
case "update":
break;
case "projects":
{
Connection connection = SQLUtils.getConnexion("jdbc:mysql://localhost/", "agir2020", "admin", "suko7Gun");
AgirStatool statool = new AgirStatool(connection, new File("/home/cpm/Projets/AgirStatool/TestZone/www/"));
Projects projects = statool.listProjects();
String header = String.format("%3s %-30s %-30s %s", "ID", "Identifier", "Name", "ParentId");
System.out.println(header);
for (Project project : projects)
{
String line = String.format("%3d %-30s %-30s %4d", project.getId(), project.getIdentifier(), project.getName(), project.getParentId());
System.out.println(line);
}
}
break;
}
}
}
else
{
System.out.println("Bad usage detected.");
help();
}
}
break;
default:
{
throw new AgirStatoolException("Bad parameter count.");
}
}
}
catch (AgirStatoolException exception)
{
System.err.println("AgirStatoolException = " + exception.getMessage());
logger.error(exception.getMessage(), exception);
help();
}
}
}

View File

@ -21,10 +21,7 @@ package org.april.agirstatool.cli;
import java.io.File; import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.PropertyConfigurator;
import org.april.agirstatool.core.AgirStatool; import org.april.agirstatool.core.AgirStatool;
import org.april.agirstatool.core.AgirStatoolException; import org.april.agirstatool.core.AgirStatoolException;
@ -32,7 +29,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.strings.StringList; import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
import utils.BuildInformation; import utils.BuildInformation;
/** /**
@ -51,49 +47,90 @@ public final class AgirStatoolCLI
} }
/** /**
* This method displays the CLI help. * Display help.
*
*/ */
public static void help() public static void displayHelp()
{ {
StringList message = new StringList(); StringList message = new StringList();
message.append("AgirStatool CLI version ").appendln(BuildInformation.instance().version()); message.append("AgirStatool CLI version ").appendln(BuildInformation.instance().version());
message.appendln("Usage:"); message.appendln("Usage:");
message.appendln(" agirstatool [ -h | -help | --help ]"); message.appendln(" agirstatool [ -h | -help | --help ]");
message.appendln(" agirstatool -c configurationFilename clear"); message.appendln(" agirstatool -c configurationFilename [ clear | projects | projects+ | update | forceupdate ]");
message.appendln(" agirstatool -c configurationFilename update"); message.appendln(" agirstatool [ -v | -version | --version ]");
message.appendln(" agirstatool -c configurationFilename projects");
System.out.println(message.toString()); logger.info(message.toString());
} }
/** /**
* The main method. * Display version.
*
* @param args
* the arguments
*/ */
public static void main(final String[] args) public static void displayVersion()
{ {
// Configure log. StringList message = new StringList();
File loggerConfig = new File("log4j.properties");
if (loggerConfig.exists()) 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))
{ {
PropertyConfigurator.configure(loggerConfig.getAbsolutePath()); result = true;
logger.info("Dedicated log configuration done."); }
logger.info("Configuration file was found in [{}].", loggerConfig.getAbsoluteFile()); else if ((args.length != 0) && (regexps == null))
{
result = false;
}
else if (args.length != regexps.length)
{
result = false;
} }
else else
{ {
BasicConfigurator.configure(); boolean ended = false;
// logger.info("Basic log configuration done."); int index = 0;
// logger.info("Configuration file was not found in [{}].", result = false;
// loggerConfig.getAbsoluteFile()); 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;
}
}
} }
// Run. //
AgirStatoolCLI.run(args); return result;
} }
/** /**
@ -129,125 +166,104 @@ public final class AgirStatoolCLI
} }
}); });
// Set the locale to France is required for week alignment.
Locale.setDefault(Locale.FRANCE);
logger.info("{} AgirStatool call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" ")); logger.info("{} AgirStatool call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" "));
// logger.info("Single connection opened with [{}].", this.url); if (isMatching(args, (String) null))
// This part implements an automate.
int parameterCount = args.length;
switch (parameterCount)
{ {
case 0: 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())
{ {
help(); logger.info("Applying configuration file found in [{}].", log4jfile.getAbsoluteFile());
PropertyConfigurator.configure(log4jfile.getAbsolutePath());
logger.info("Configuration log configuration done.");
} }
break;
case 1: switch (action)
{ {
String token = args[0]; case "clear":
if (StringsUtils.containsAny(token, "-h", "-help", "--help"))
{ {
help(); 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();
} }
else break;
case "projects":
{ {
throw new AgirStatoolException("Bad usage."); 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);
} }
} break;
break;
case 3: case "projects+":
{
String token = args[0];
String configurationFilename = args[1];
String command = args[2];
if ((StringUtils.equals(token, "-c")) && (StringsUtils.containsAny(command, "clear", "refresh", "update", "projects", "projects+")))
{ {
File configurationFile = new File(configurationFilename); logger.info("projects+ command…");
if (!configurationFile.exists()) AgirStatoolConfigFile config = new AgirStatoolConfigFile(configurationFile);
{ Connection connection = SQLUtils.getConnexion(config.getDabataseUrl(), config.getDabataseName(), config.getDabataseLogin(), config.getDabatasePassword());
throw new AgirStatoolException("Configuration file does not exist."); AgirStatool statool = new AgirStatool(connection, new File(config.getTargetDirectory()));
} StringList lines = statool.doBuildTextProjectExtendedList();
else if (!configurationFile.isFile()) System.out.println(lines);
{
throw new AgirStatoolException("Configuration file is not a file.");
}
else
{
// Apply -c parameter.
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 (command)
{
case "clear":
break;
case "refresh":
case "update":
{
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();
}
break;
case "projects":
{
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);
}
break;
case "projects+":
{
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);
}
break;
}
}
} }
else break;
case "update":
{ {
System.out.println("Bad usage detected."); logger.info("Update command…");
help(); 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();
} }
} break;
break;
default: case "forceupdate":
{ {
throw new AgirStatoolException("Bad parameter count."); 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;
} }
} }
else
{
logger.info("Bad usage.");
displayHelp();
}
//
logger.info("Finished."); logger.info("Finished.");
} }
catch (AgirStatoolException exception) catch (AgirStatoolException exception)
{ {
System.err.println("AgirStatoolException = " + exception.getMessage()); System.err.println("AgirStatoolException = " + exception.getMessage());
logger.error(exception.getMessage(), exception); logger.error(exception.getMessage(), exception);
help(); displayHelp();
} }
} }
} }

View File

@ -19,7 +19,6 @@
package org.april.agirstatool.cli; package org.april.agirstatool.cli;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
@ -49,22 +48,29 @@ public class AgirStatoolConfigFile extends Properties
* @throws AgirStatoolException * @throws AgirStatoolException
* the agir statool exception * the agir statool exception
*/ */
public AgirStatoolConfigFile(final File source) throws AgirStatoolException public AgirStatoolConfigFile(final File configurationFile) throws AgirStatoolException
{ {
super(); super();
try try
{ {
Properties config = loadProperties(source); if (configurationFile == null)
for (String key : config.stringPropertyNames())
{ {
put(key, config.getProperty(key)); throw new AgirStatoolException("Configuration file undefined.");
}
else if (!configurationFile.exists())
{
throw new AgirStatoolException("Configuration file does not exist.");
}
else if (!configurationFile.isFile())
{
throw new AgirStatoolException("Configuration file is not a file.");
}
else
{
Properties config = loadProperties(configurationFile);
this.putAll(config);
} }
}
catch (FileNotFoundException exception)
{
throw new AgirStatoolException("File not found.", exception);
} }
catch (IOException exception) catch (IOException exception)
{ {

View File

@ -19,6 +19,7 @@
package org.april.agirstatool.core; package org.april.agirstatool.core;
import java.io.File; import java.io.File;
import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
@ -29,6 +30,7 @@ import java.time.LocalDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.april.agirstatool.charts.DateCount; import org.april.agirstatool.charts.DateCount;
import org.april.agirstatool.charts.DateCountList; import org.april.agirstatool.charts.DateCountList;
import org.april.agirstatool.charts.DateCountMap; import org.april.agirstatool.charts.DateCountMap;
@ -181,8 +183,8 @@ public class AgirStatool
{ {
try try
{ {
// TODO: add filter on .xhtml and .css file. FileFilter filter = new WildcardFileFilter(new String[] { "*.xhtml", "*.html", "*.css", "*.js" });
for (File file : this.targetDirectory.listFiles()) for (File file : this.targetDirectory.listFiles(filter))
{ {
FileUtils.forceDelete(file); FileUtils.forceDelete(file);
} }

View File

@ -38,6 +38,8 @@ public class Issue
* *
* @param id * @param id
* the id * the id
* @param projectId
* the project id
* @param createdOn * @param createdOn
* the created on * the created on
*/ */
@ -51,6 +53,8 @@ public class Issue
* *
* @param id * @param id
* the id * the id
* @param projectId
* the project id
* @param createdOn * @param createdOn
* the created on * the created on
* @param closedOn * @param closedOn