Improved parameter management.

This commit is contained in:
Christian P. MOMON 2024-07-18 17:30:43 +02:00
parent 16c239f1c6
commit 4e0d9e8ab7
4 changed files with 77 additions and 11 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Christian Pierre MOMON <christian@momon.org>
* Copyright (C) 2021-2024 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of Logar, simple tool to manage http log files.
*
@ -92,7 +92,7 @@ public final class Logar
}
else if (!source.exists())
{
throw new IllegalArgumentException("Source file does not exist.");
throw new IllegalArgumentException("Source file does not exist: " + source);
}
else
{
@ -101,6 +101,45 @@ public final class Logar
System.out.println("Table size=" + anonymizer.getMapTable().size());
Files files = FilesUtils.search(source, LOGFILE_PATTERN).keepFileType().removeContaining("-anon.").sortByName();
anonymize(files, mapFile);
}
}
/**
* Anonymize.
*
* @param files
* the files
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void anonymize(final Files files) throws IOException
{
anonymize(files, null);
}
/**
* Anonymize.
*
* @param files
* the files
* @param mapFile
* the map file
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void anonymize(final Files files, final File mapFile) throws IOException
{
if (files == null)
{
throw new IllegalArgumentException("Null source file.");
}
else
{
Anonymizer anonymizer = new Anonymizer();
anonymizer.loadMapTable(mapFile);
System.out.println("Table size=" + anonymizer.getMapTable().size());
logger.info("file count={}", files.size());
for (File file : files)
{

View file

@ -78,11 +78,11 @@ public final class Anonymizer
}
else if (!source.isFile())
{
throw new IllegalArgumentException("Parameter is not a file.");
throw new IllegalArgumentException("Parameter is not a file [" + source + "]");
}
else if (!StringUtils.containsAny(source.getName(), "access", "error"))
{
throw new IllegalArgumentException("File name does not contain 'access' or 'error'.");
throw new IllegalArgumentException("File name does not contain 'access' or 'error' [" + source + "]");
}
else
{
@ -92,7 +92,7 @@ public final class Anonymizer
File target;
if (source.getName().endsWith(".log"))
{
target = new File(source.getParentFile(), source.getName().replaceAll(".log$", "-anon.log."));
target = new File(source.getParentFile(), source.getName().replaceAll(".log$", "-anon.log"));
}
else
{

View file

@ -28,6 +28,8 @@ import fr.devinsy.logar.app.ExtractOptions;
import fr.devinsy.logar.app.Logar;
import fr.devinsy.logar.app.OnOffOption;
import fr.devinsy.logar.util.BuildInformation;
import fr.devinsy.logar.util.Files;
import fr.devinsy.logar.util.FilesUtils;
import fr.devinsy.strings.StringList;
/**
@ -128,17 +130,27 @@ public final class LogarCLI
{
displayVersion();
}
else if (CLIUtils.isMatching(args, "anonymize", ".+", "-map", ".+"))
else if (CLIUtils.isMatchingEllipsis(args, "anonymize", "-map", ".+", ".+"))
{
File source = new File(args[1]);
File map = new File(args[2]);
Files source = new Files();
for (int index = 3; index < args.length; index++)
{
File file = new File(args[index]);
Files filteredFiles = FilesUtils.search(file, Logar.LOGFILE_PATTERN).keepFileType().removeContaining("-anon.").sortByName();
source.addAll(filteredFiles);
}
Logar.anonymize(source, map);
}
else if (CLIUtils.isMatchingEllipsis(args, "anonymize", ".+"))
{
File source = new File(args[1]);
Files source = new Files();
for (int index = 1; index < args.length; index++)
{
File file = new File(args[index]);
Files filteredFiles = FilesUtils.search(file, Logar.LOGFILE_PATTERN).keepFileType().removeContaining("-anon.").sortByName();
source.addAll(filteredFiles);
}
Logar.anonymize(source);
}
else if (CLIUtils.isMatching(args, "archive", ".+", ".+"))

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Christian Pierre MOMON <christian@momon.org>
* Copyright (C) 2021-2024 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of Logar, simple tool to manage http log files.
*
@ -49,6 +49,21 @@ public class Files extends ArrayList<File>
super(initialCapacity);
}
/**
* Instantiates a new files.
*
* @param source
* the source
*/
public Files(final String[] source)
{
super();
for (String file : source)
{
add(new File(file));
}
}
/**
* Keep.
*