From 4e0d9e8ab7df7c9dea6d6b8403cd3d58af7919c9 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 18 Jul 2024 17:30:43 +0200 Subject: [PATCH] Improved parameter management. --- src/fr/devinsy/logar/app/Logar.java | 43 ++++++++++++++++++- .../logar/app/anonymizer/Anonymizer.java | 6 +-- src/fr/devinsy/logar/cli/LogarCLI.java | 22 +++++++--- src/fr/devinsy/logar/util/Files.java | 17 +++++++- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/fr/devinsy/logar/app/Logar.java b/src/fr/devinsy/logar/app/Logar.java index fbb4110..9a95293 100644 --- a/src/fr/devinsy/logar/app/Logar.java +++ b/src/fr/devinsy/logar/app/Logar.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Christian Pierre MOMON + * Copyright (C) 2021-2024 Christian Pierre MOMON * * 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) { diff --git a/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java b/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java index e6dbc73..05371d1 100644 --- a/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java +++ b/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java @@ -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 { diff --git a/src/fr/devinsy/logar/cli/LogarCLI.java b/src/fr/devinsy/logar/cli/LogarCLI.java index bd0ab96..ac56cb9 100644 --- a/src/fr/devinsy/logar/cli/LogarCLI.java +++ b/src/fr/devinsy/logar/cli/LogarCLI.java @@ -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", ".+", ".+")) diff --git a/src/fr/devinsy/logar/util/Files.java b/src/fr/devinsy/logar/util/Files.java index 1ad9279..4d715fd 100644 --- a/src/fr/devinsy/logar/util/Files.java +++ b/src/fr/devinsy/logar/util/Files.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Christian Pierre MOMON + * Copyright (C) 2021-2024 Christian Pierre MOMON * * This file is part of Logar, simple tool to manage http log files. * @@ -49,6 +49,21 @@ public class Files extends ArrayList 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. *