2020-09-13 01:28:27 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org>
|
|
|
|
*
|
|
|
|
* This file is part of StatoolInfos, simple service statistics tool.
|
|
|
|
*
|
|
|
|
* StatoolInfos 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.
|
|
|
|
*
|
|
|
|
* StatoolInfos 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 StatoolInfos. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package fr.devinsy.statoolinfos.cli;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import fr.devinsy.statoolinfos.core.StatoolInfos;
|
2020-09-19 02:37:52 +02:00
|
|
|
import fr.devinsy.statoolinfos.util.BuildInformation;
|
|
|
|
import fr.devinsy.statoolinfos.util.Files;
|
2020-09-13 01:28:27 +02:00
|
|
|
import fr.devinsy.strings.StringList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class <code>StatoolInfosCLI</code> manages a Command Line Interface for
|
|
|
|
* StatoolInfos.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public final class StatoolInfosCLI
|
|
|
|
{
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(StatoolInfosCLI.class);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates a new statool infos CLI.
|
|
|
|
*/
|
|
|
|
private StatoolInfosCLI()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert path.
|
|
|
|
*
|
|
|
|
* @param path
|
|
|
|
* the path
|
|
|
|
* @return the files
|
|
|
|
*/
|
|
|
|
public static Files convertPath(final String path)
|
|
|
|
{
|
|
|
|
Files result;
|
|
|
|
|
|
|
|
result = new Files();
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(path))
|
|
|
|
{
|
|
|
|
File input = new File(path);
|
|
|
|
|
|
|
|
if (input.exists())
|
|
|
|
{
|
|
|
|
if (input.isFile())
|
|
|
|
{
|
|
|
|
result.add(input);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (File file : input.listFiles())
|
|
|
|
{
|
|
|
|
if ((file.isFile()) && (file.getName().endsWith(".properties")))
|
|
|
|
{
|
|
|
|
result.add(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.add(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display help.
|
|
|
|
*/
|
|
|
|
public static void displayHelp()
|
|
|
|
{
|
|
|
|
StringList message = new StringList();
|
|
|
|
|
|
|
|
message.append("StatoolInfos CLI version ").appendln(BuildInformation.instance().version());
|
|
|
|
message.appendln("Usage:");
|
|
|
|
message.appendln(" statoolinfos [ -h | -help | --help ]");
|
|
|
|
message.appendln(" statoolinfos [ -v | -version | --version ]");
|
2020-10-15 01:47:48 +02:00
|
|
|
message.appendln(" statoolinfos clear [ directory | file ]");
|
|
|
|
message.appendln(" statoolinfos probe [ directory | file ]");
|
|
|
|
message.appendln(" statoolinfos build [ directory | file ]");
|
|
|
|
message.appendln(" statoolinfos crawl [ directory | file ]");
|
2020-09-17 02:03:56 +02:00
|
|
|
message.appendln(" statoolinfos htmlize [ directory | file ]");
|
2020-09-13 01:28:27 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// 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("{} StatoolInfos call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" "));
|
|
|
|
|
|
|
|
if (isMatching(args))
|
|
|
|
{
|
|
|
|
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, "clear", "\\s*.+\\s*"))
|
|
|
|
{
|
|
|
|
Files inputs = convertPath(StringUtils.trim(args[1]));
|
|
|
|
for (File input : inputs)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
StatoolInfos.clear(input);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 01:47:48 +02:00
|
|
|
else if (isMatching(args, "probe", "\\s*.+\\s*"))
|
|
|
|
{
|
|
|
|
Files inputs = convertPath(StringUtils.trim(args[1]));
|
|
|
|
for (File input : inputs)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
StatoolInfos.probe(input);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 01:28:27 +02:00
|
|
|
else if (isMatching(args, "build", "\\s*.+\\s*"))
|
|
|
|
{
|
|
|
|
Files inputs = convertPath(StringUtils.trim(args[1]));
|
|
|
|
for (File input : inputs)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
StatoolInfos.build(input);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isMatching(args, "crawl", "\\s*.+\\s*"))
|
|
|
|
{
|
|
|
|
Files inputs = convertPath(StringUtils.trim(args[1]));
|
|
|
|
for (File input : inputs)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
StatoolInfos.crawl(input);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isMatching(args, "htmlize", "\\s*.+\\s*"))
|
|
|
|
{
|
|
|
|
Files inputs = convertPath(StringUtils.trim(args[1]));
|
|
|
|
for (File input : inputs)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
StatoolInfos.htmlize(input);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
|
2020-09-15 03:16:26 +02:00
|
|
|
exception.printStackTrace();
|
2020-09-13 01:28:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logger.info("Bad usage.");
|
|
|
|
displayHelp();
|
|
|
|
}
|
2020-09-19 02:37:52 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
logger.info("Done.");
|
2020-09-13 01:28:27 +02:00
|
|
|
}
|
|
|
|
}
|