From 3a1a1e79d9bb32387fc36503d42192c80fc4552a Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 30 Jan 2020 16:43:20 +0100 Subject: [PATCH] Refactored CLI launching. --- .../agirstatool/AgirStatoolLauncher.java | 13 - src/org/april/agirstatool/cli/AgirConfig.java | 225 --------------- .../april/agirstatool/cli/AgirStatoolCLI.java | 258 ++++++++++-------- .../cli/AgirStatoolConfigFile.java | 26 +- .../april/agirstatool/core/AgirStatool.java | 6 +- src/org/april/agirstatool/core/Issue.java | 4 + 6 files changed, 161 insertions(+), 371 deletions(-) delete mode 100644 src/org/april/agirstatool/cli/AgirConfig.java diff --git a/src/org/april/agirstatool/AgirStatoolLauncher.java b/src/org/april/agirstatool/AgirStatoolLauncher.java index 7deccdf..e2bc9b4 100644 --- a/src/org/april/agirstatool/AgirStatoolLauncher.java +++ b/src/org/april/agirstatool/AgirStatoolLauncher.java @@ -20,13 +20,11 @@ package org.april.agirstatool; import java.io.File; -import org.apache.commons.lang3.ArrayUtils; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.EnhancedPatternLayout; import org.apache.log4j.PropertyConfigurator; import org.april.agirstatool.cli.AgirStatoolCLI; -import org.april.agirstatool.demo.AgirStatool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,9 +61,6 @@ public final class AgirStatoolLauncher else { 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. @@ -74,16 +69,8 @@ public final class AgirStatoolLauncher // TODO // SikevaGUI.run(); } - else if (ArrayUtils.contains(args, "-demo")) - { - AgirStatool.run(args); - } else { - // String[] foo = { "-c", - // "/home/cpm/Projets/AgirStatool/TestZone/agirstatool.conf", - // "listProjects" }; - // AgiStatoolCLI.run(foo); AgirStatoolCLI.run(args); } } diff --git a/src/org/april/agirstatool/cli/AgirConfig.java b/src/org/april/agirstatool/cli/AgirConfig.java deleted file mode 100644 index ff54dca..0000000 --- a/src/org/april/agirstatool/cli/AgirConfig.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * 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 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 JugaCLI 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(); - } - } -} diff --git a/src/org/april/agirstatool/cli/AgirStatoolCLI.java b/src/org/april/agirstatool/cli/AgirStatoolCLI.java index 077e600..474906e 100644 --- a/src/org/april/agirstatool/cli/AgirStatoolCLI.java +++ b/src/org/april/agirstatool/cli/AgirStatoolCLI.java @@ -21,10 +21,7 @@ package org.april.agirstatool.cli; import java.io.File; import java.sql.Connection; 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.april.agirstatool.core.AgirStatool; import org.april.agirstatool.core.AgirStatoolException; @@ -32,7 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.strings.StringList; -import fr.devinsy.strings.StringsUtils; 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(); 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"); + message.appendln(" agirstatool -c configurationFilename [ clear | projects | projects+ | update | forceupdate ]"); + message.appendln(" agirstatool [ -v | -version | --version ]"); - System.out.println(message.toString()); + logger.info(message.toString()); } /** - * The main method. - * - * @param args - * the arguments + * Display version. */ - public static void main(final String[] args) + public static void displayVersion() { - // Configure log. - File loggerConfig = new File("log4j.properties"); - if (loggerConfig.exists()) + 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)) { - PropertyConfigurator.configure(loggerConfig.getAbsolutePath()); - logger.info("Dedicated log configuration done."); - logger.info("Configuration file was found in [{}].", loggerConfig.getAbsoluteFile()); + result = true; + } + else if ((args.length != 0) && (regexps == null)) + { + result = false; + } + else if (args.length != regexps.length) + { + result = false; } else { - BasicConfigurator.configure(); - // logger.info("Basic log configuration done."); - // logger.info("Configuration file was not found in [{}].", - // loggerConfig.getAbsoluteFile()); + 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; + } + } } - // 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("Single connection opened with [{}].", this.url); - - // This part implements an automate. - int parameterCount = args.length; - switch (parameterCount) + if (isMatching(args, (String) null)) { - 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]; - if (StringsUtils.containsAny(token, "-h", "-help", "--help")) + case "clear": { - 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: - { - 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+"))) + case "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 - { - // 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; - } - } + 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); } - else + break; + + case "update": { - System.out.println("Bad usage detected."); - help(); + 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; + break; - default: - { - throw new AgirStatoolException("Bad parameter count."); + 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; } } + else + { + logger.info("Bad usage."); + displayHelp(); + } + // logger.info("Finished."); } catch (AgirStatoolException exception) { System.err.println("AgirStatoolException = " + exception.getMessage()); logger.error(exception.getMessage(), exception); - help(); + displayHelp(); } } } diff --git a/src/org/april/agirstatool/cli/AgirStatoolConfigFile.java b/src/org/april/agirstatool/cli/AgirStatoolConfigFile.java index 85613f8..030c862 100644 --- a/src/org/april/agirstatool/cli/AgirStatoolConfigFile.java +++ b/src/org/april/agirstatool/cli/AgirStatoolConfigFile.java @@ -19,7 +19,6 @@ package org.april.agirstatool.cli; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; @@ -49,22 +48,29 @@ public class AgirStatoolConfigFile extends Properties * @throws AgirStatoolException * the agir statool exception */ - public AgirStatoolConfigFile(final File source) throws AgirStatoolException + public AgirStatoolConfigFile(final File configurationFile) throws AgirStatoolException { super(); try { - Properties config = loadProperties(source); - - for (String key : config.stringPropertyNames()) + if (configurationFile == null) { - 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) { diff --git a/src/org/april/agirstatool/core/AgirStatool.java b/src/org/april/agirstatool/core/AgirStatool.java index 9ba732c..c7a7d0f 100644 --- a/src/org/april/agirstatool/core/AgirStatool.java +++ b/src/org/april/agirstatool/core/AgirStatool.java @@ -19,6 +19,7 @@ package org.april.agirstatool.core; import java.io.File; +import java.io.FileFilter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.sql.Connection; @@ -29,6 +30,7 @@ import java.time.LocalDateTime; import java.time.ZoneOffset; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.filefilter.WildcardFileFilter; import org.april.agirstatool.charts.DateCount; import org.april.agirstatool.charts.DateCountList; import org.april.agirstatool.charts.DateCountMap; @@ -181,8 +183,8 @@ public class AgirStatool { try { - // TODO: add filter on .xhtml and .css file. - for (File file : this.targetDirectory.listFiles()) + FileFilter filter = new WildcardFileFilter(new String[] { "*.xhtml", "*.html", "*.css", "*.js" }); + for (File file : this.targetDirectory.listFiles(filter)) { FileUtils.forceDelete(file); } diff --git a/src/org/april/agirstatool/core/Issue.java b/src/org/april/agirstatool/core/Issue.java index bd49244..531385d 100644 --- a/src/org/april/agirstatool/core/Issue.java +++ b/src/org/april/agirstatool/core/Issue.java @@ -38,6 +38,8 @@ public class Issue * * @param id * the id + * @param projectId + * the project id * @param createdOn * the created on */ @@ -51,6 +53,8 @@ public class Issue * * @param id * the id + * @param projectId + * the project id * @param createdOn * the created on * @param closedOn