Added tagdate and format commands.

This commit is contained in:
Christian P. MOMON 2021-05-15 16:59:04 +02:00
parent 7e2f30cf00
commit cc141d81aa
7 changed files with 663 additions and 8 deletions

View file

@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.core.StatoolInfos;
import fr.devinsy.statoolinfos.util.BuildInformation;
import fr.devinsy.statoolinfos.util.Files;
import fr.devinsy.statoolinfos.util.FilesUtils;
import fr.devinsy.strings.StringList;
/**
@ -101,11 +102,13 @@ public final class StatoolInfosCLI
message.appendln("Usage:");
message.appendln(" statoolinfos [ -h | -help | --help ]");
message.appendln(" statoolinfos [ -v | -version | --version ]");
message.appendln(" statoolinfos clear [ directory | file ]");
message.appendln(" statoolinfos probe [ directory | file ]");
message.appendln(" statoolinfos build [ directory | file ]");
message.appendln(" statoolinfos crawl [ directory | file ]");
message.appendln(" statoolinfos htmlize [ directory | file ]");
message.appendln(" statoolinfos build <fileordirectory> build property files from conf and input");
message.appendln(" statoolinfos clear <fileordirectory> remove property files from conf");
message.appendln(" statoolinfos crawl <fileordirectory> crawl all file from conf and input");
message.appendln(" statoolinfos format <fileordirectory> format in tiny way");
message.appendln(" statoolinfos htmlize <fileordirectory> generate web pages from conf");
message.appendln(" statoolinfos probe <fileordirectory> generate metrics files from conf");
message.appendln(" statoolinfos tagdate <fileordirectory> update the file.datetime file");
System.out.println(message.toString());
}
@ -273,6 +276,22 @@ public final class StatoolInfosCLI
}
}
}
else if (isMatching(args, "format", "\\s*.+\\s*"))
{
Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties");
for (File input : inputs)
{
try
{
StatoolInfos.format(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]));
@ -305,6 +324,22 @@ public final class StatoolInfosCLI
}
}
}
else if (isMatching(args, "tagdate", "\\s*.+\\s*"))
{
Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties");
for (File input : inputs)
{
try
{
StatoolInfos.tagDate(input);
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
exception.printStackTrace();
}
}
}
else
{
System.out.println("Bad usage.");

View file

@ -20,7 +20,10 @@ package fr.devinsy.statoolinfos.core;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,6 +31,10 @@ import fr.devinsy.statoolinfos.build.Builder;
import fr.devinsy.statoolinfos.crawl.Crawler;
import fr.devinsy.statoolinfos.htmlize.Htmlizer;
import fr.devinsy.statoolinfos.metrics.Prober;
import fr.devinsy.statoolinfos.properties.PathProperties;
import fr.devinsy.statoolinfos.properties.PathPropertyUtils;
import fr.devinsy.strings.StringList;
import fr.devinsy.strings.StringsUtils;
/**
* The Class StatoolInfos.
@ -86,6 +93,43 @@ public class StatoolInfos
Crawler.crawl(configurationFile);
}
/**
* Format.
*
* @param inputFile
* the input file
* @throws StatoolInfosException
* the statool infos exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void format(final File inputFile) throws StatoolInfosException, IOException
{
logger.info("Format {}", inputFile);
if (inputFile == null)
{
throw new StatoolInfosException("Input is undefined.");
}
else if (!inputFile.exists())
{
throw new StatoolInfosException("Input does not exist.");
}
else if (!inputFile.isFile())
{
throw new StatoolInfosException("Input is not a file.");
}
else
{
// Load input properties.
PathProperties inputs = PathPropertyUtils.load(inputFile);
// Save input properties.
inputFile.renameTo(new File(inputFile.getParentFile(), inputFile.getName() + ".bak"));
PathPropertyUtils.save(inputFile, inputs);
}
}
/**
* Htmlize.
*
@ -115,4 +159,50 @@ public class StatoolInfos
{
Prober.probe(configurationFile);
}
/**
* Tag date.
*
* @param inputFile
* the source
* @throws StatoolInfosException
* the statool infos exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void tagDate(final File inputFile) throws StatoolInfosException, IOException
{
System.out.println("TagDate " + inputFile);
if (inputFile == null)
{
throw new StatoolInfosException("Null parameter [source].");
}
else if (!inputFile.exists())
{
throw new StatoolInfosException("Input does not exist.");
}
else if (!inputFile.isFile())
{
throw new StatoolInfosException("Input is not a file.");
}
else
{
StringList lines = StringsUtils.load(inputFile);
for (int lineIndex = 0; lineIndex < lines.size(); lineIndex++)
{
String line = lines.get(lineIndex);
if (StringUtils.startsWith(line, "file.datetime="))
{
lines.set(lineIndex, "file.datetime=" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss")));
}
}
inputFile.renameTo(new File(inputFile.getParentFile(), inputFile.getName() + ".bak"));
StringsUtils.save(inputFile, lines);
}
}
}

View file

@ -41,7 +41,7 @@ import fr.devinsy.statoolinfos.properties.PathPropertyList;
import fr.devinsy.statoolinfos.properties.PathPropertyUtils;
/**
* The Class StatoolInfos.
* The Class Crawler.
*/
public class Crawler
{

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org>
* Copyright (C) 2020-2021 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of StatoolInfos, simple service statistics tool.
*
@ -19,6 +19,8 @@
package fr.devinsy.statoolinfos.util;
import java.time.LocalDateTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
@ -235,6 +237,49 @@ public class CompareUtils
return result;
}
/**
* Compare natural.
*
* @param alpha
* the alpha
* @param bravo
* the bravo
* @return the int
*/
public static int compareNatural(final String alpha, final String bravo)
{
int result;
Pattern pattern = Pattern.compile("(?<left>.*)(?<digits>\\d+)(?<right>.*)");
Matcher matcherA = pattern.matcher(alpha);
Matcher matcherB = pattern.matcher(bravo);
if ((matcherA.find()) && (matcherB.find()))
{
result = StringUtils.compare(matcherA.group("left"), matcherB.group("left"));
if (result == 0)
{
Long a = Long.valueOf(matcherA.group("digits"));
Long b = Long.valueOf(matcherB.group("digits"));
result = compare(a, b);
if (result == 0)
{
result = compareNatural(matcherA.group("right"), matcherB.group("right"));
}
}
}
else
{
result = StringUtils.compare(alpha, bravo);
}
//
return result;
}
/**
* Compare reverse.
*

View file

@ -0,0 +1,124 @@
/*
* Copyright (C) 2020-2021 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.util;
import java.io.File;
import java.util.Comparator;
/**
* The Class FileComparator.
*/
public class FileComparator implements Comparator<File>
{
public enum Sorting
{
NAME
}
private Sorting sorting;
/**
* Instantiates a new organization comparator.
*
* @param sorting
* the sorting
*/
public FileComparator(final Sorting sorting)
{
this.sorting = sorting;
}
/**
* Compare.
*
* @param alpha
* the alpha
* @param bravo
* the bravo
* @return the int
*/
@Override
public int compare(final File alpha, final File bravo)
{
int result;
result = compare(alpha, bravo, this.sorting);
//
return result;
}
/**
* Compare.
*
* @param alpha
* the alpha
* @param bravo
* the bravo
* @param sorting
* the sorting
* @return the int
*/
public static int compare(final File alpha, final File bravo, final Sorting sorting)
{
int result;
if (sorting == null)
{
result = 0;
}
else
{
switch (sorting)
{
default:
case NAME:
result = CompareUtils.compareNatural(getName(alpha), getName(bravo));
break;
}
}
//
return result;
}
/**
* Gets the name.
*
* @param source
* the source
* @return the name
*/
public static String getName(final File source)
{
String result;
if (source == null)
{
result = null;
}
else
{
result = source.getName();
}
//
return result;
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org>
* Copyright (C) 2020-2021 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of StatoolInfos, simple service statistics tool.
*
@ -20,6 +20,8 @@ package fr.devinsy.statoolinfos.util;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Pattern;
/**
* The Class Files.
@ -46,4 +48,229 @@ public class Files extends ArrayList<File>
{
super(initialCapacity);
}
/**
* Keep.
*
* @param regex
* the regex
* @return the files
*/
public Files keep(final String regex)
{
Files result;
Pattern pattern = Pattern.compile(regex);
Iterator<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (!pattern.matcher(file.getName()).matches())
{
iterator.remove();
}
}
result = this;
//
return result;
}
/**
* Keep directories.
*
* @return the files
*/
public Files keepDirectoryType()
{
Files result;
Iterator<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (!file.isDirectory())
{
iterator.remove();
}
}
result = this;
//
return result;
}
/**
* Keep file type.
*
* @return the files
*/
public Files keepFileType()
{
Files result;
Iterator<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (!file.isFile())
{
iterator.remove();
}
}
result = this;
//
return result;
}
/**
* Removes the containing.
*
* @param token
* the token
* @return the files
*/
public Files removeContaining(final String token)
{
Files result;
Iterator<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (file.getName().contains(token))
{
iterator.remove();
}
}
result = this;
//
return result;
}
/**
* Removes the file type.
*
* @return the files
*/
public Files removeFileType()
{
Files result;
Iterator<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (file.isFile())
{
iterator.remove();
}
}
result = this;
//
return result;
}
/**
* Removes the hidden.
*
* @return the files
*/
public Files removeHidden()
{
Files result;
Iterator<File> iterator = iterator();
while (iterator.hasNext())
{
File file = iterator.next();
if (file.getName().startsWith("."))
{
iterator.remove();
}
}
result = this;
//
return result;
}
/**
* Sort.
*
* @param sorting
* the sorting
* @return the files
*/
public Files sort(final FileComparator.Sorting sorting)
{
Files result;
sort(new FileComparator(sorting));
result = this;
//
return result;
}
/**
* Sort by name.
*
* @return the files
*/
public Files sortByName()
{
Files result;
result = sort(FileComparator.Sorting.NAME);
//
return result;
}
public Files sortByPathname()
{
Files result;
result = sort(FileComparator.Sorting.NAME);
//
return result;
}
/**
* Of.
*
* @param source
* the source
* @return the files
*/
public static Files of(final File source)
{
Files result;
result = new Files();
if (source != null)
{
for (File file : source.listFiles())
{
result.add(file);
}
}
//
return result;
}
}

View file

@ -0,0 +1,134 @@
/*
* Copyright (C) 2020-2021 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.util;
import java.io.File;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* The Class FilesUtils.
*/
public class FilesUtils
{
/**
* Instantiates a new files utils.
*/
private FilesUtils()
{
}
/**
* List recursively.
*
* @param source
* the source
* @return the files
*/
public static Files listRecursively(final File source)
{
Files result;
result = new Files();
if ((source != null) && (source.exists()))
{
if (source.isFile())
{
result.add(source);
}
else
{
for (File file : source.listFiles())
{
if (file.isDirectory())
{
result.addAll(listRecursively(file));
}
else
{
result.add(file);
}
}
}
}
//
return result;
}
/**
* Search recursively.
*
* @param source
* the source
* @param regex
* the regex
* @return the files
*/
public static Files search(final File source, final String regex)
{
Files result;
result = new Files();
Pattern pattern = Pattern.compile(regex);
Files full = listRecursively(source);
for (File file : full)
{
if (pattern.matcher(file.getName()).matches())
{
result.add(file);
}
}
//
return result;
}
/**
* List recursively.
*
* @param source
* the source
* @param extensions
* the extensions
* @return the files
*/
public static Files searchEndingWith(final File source, final String... extensions)
{
Files result;
result = new Files();
Files full = listRecursively(source);
for (File file : full)
{
if (StringUtils.endsWithAny(file.getName(), extensions))
{
result.add(file);
}
}
//
return result;
}
}