From c2520f05b7ced7a18e86e809f7ed6365e52b524c Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 18 Jul 2024 18:04:57 +0200 Subject: [PATCH] Improved parameter management. --- src/fr/devinsy/logar/app/Logar.java | 113 ++++++++++++++++++++++--- src/fr/devinsy/logar/cli/LogarCLI.java | 55 ++++++++---- src/fr/devinsy/logar/util/Files.java | 15 ++++ 3 files changed, 155 insertions(+), 28 deletions(-) diff --git a/src/fr/devinsy/logar/app/Logar.java b/src/fr/devinsy/logar/app/Logar.java index 9a95293..9793485 100644 --- a/src/fr/devinsy/logar/app/Logar.java +++ b/src/fr/devinsy/logar/app/Logar.java @@ -395,7 +395,7 @@ public final class Logar } else if (!file.isFile()) { - throw new IllegalArgumentException("Parameter is not a file."); + throw new IllegalArgumentException("Parameter is not a file [" + file + "]"); } else { @@ -458,13 +458,31 @@ public final class Logar } else if (!source.exists()) { - System.out.println("Missing source to check."); + System.out.println("Missing source to check [" + source + "]"); } else { - Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName(); + checkLogFiles(new Files(source)); + } + } - for (File file : files) + /** + * Check log files. + * + * @param source + * the source + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void checkLogFiles(final Files source) throws IOException + { + if (source == null) + { + System.out.println("Undefined source."); + } + else + { + for (File file : source) { checkLogFile(file); } @@ -487,7 +505,7 @@ public final class Logar } else if (!source.exists()) { - System.out.println("Missing source to check."); + System.out.println("Missing source to check [" + source + "]"); } else { @@ -500,6 +518,29 @@ public final class Logar } } + /** + * Check sort. + * + * @param source + * the source + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void checkSort(final Files source) throws IOException + { + if (source == null) + { + System.out.println("Undefined source."); + } + else + { + for (File file : source) + { + checkSortFile(file); + } + } + } + /** * Check sort file. * @@ -651,12 +692,31 @@ public final class Logar } else if (!source.exists()) { - System.out.println("Missing source to sort."); + System.out.println("Missing source to sort [" + source + "]"); } else { - Files files = FilesUtils.search(source, LOGFILE_PATTERN).removeHidden().sortByName(); - for (File file : files) + identify(new Files(source)); + } + } + + /** + * Identify. + * + * @param source + * the source + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void identify(final Files source) throws IOException + { + if (source == null) + { + System.out.println("Undefined source."); + } + else + { + for (File file : source) { String type = LogFile.getType(file); @@ -681,13 +741,31 @@ public final class Logar } else if (!source.exists()) { - System.out.println("Missing source to sort."); + System.out.println("Missing source to sort [" + source + "]"); } else { - Files files = FilesUtils.search(source, LOGFILE_PATTERN).removeHidden().sortByName(); + sort(new Files(source)); + } + } - for (File file : files) + /** + * Sort. + * + * @param source + * the source + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void sort(final Files source) throws IOException + { + if (source == null) + { + System.out.println("Undefined source."); + } + else + { + for (File file : source) { System.out.println("== Sort for [" + file.getName() + "]"); LogFile.sortLogFile(file); @@ -703,9 +781,18 @@ public final class Logar */ public static void statUserAgents(final File source) { - Files files = FilesUtils.searchEndingWith(source, ".log", ".log.gz").keepFileType().sortByName(); + statUserAgents(new Files(source)); + } - for (File file : files) + /** + * Stat user agents. + * + * @param source + * the source + */ + public static void statUserAgents(final Files source) + { + for (File file : source) { statUserAgentsForFile(file); } diff --git a/src/fr/devinsy/logar/cli/LogarCLI.java b/src/fr/devinsy/logar/cli/LogarCLI.java index 3090d7f..df61fbe 100644 --- a/src/fr/devinsy/logar/cli/LogarCLI.java +++ b/src/fr/devinsy/logar/cli/LogarCLI.java @@ -167,16 +167,26 @@ public final class LogarCLI Logar.archive(source, target, OnOffOption.ON); } - else if (CLIUtils.isMatching(args, "check", ".+")) + else if (CLIUtils.isMatchingEllipsis(args, "check", ".+")) { - 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().sortByName(); + source.addAll(filteredFiles); + } Logar.checkLogFiles(source); } - else if (CLIUtils.isMatching(args, "checksort", ".+")) + else if (CLIUtils.isMatchingEllipsis(args, "checksort", ".+")) { - 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().sortByName(); + source.addAll(filteredFiles); + } Logar.checkSort(source); } else if (CLIUtils.isMatching(args, "extract", "\\s*((ip)?(,)?(remoteuser)?(,)?(datetime)?(,)?(useragent)?)\\s*", "\\s*\\S+\\s*")) @@ -186,22 +196,37 @@ public final class LogarCLI Logar.extract(source, options); } - else if (CLIUtils.isMatching(args, "identify", ".+")) + else if (CLIUtils.isMatchingEllipsis(args, "identify", ".+")) { - 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().sortByName(); + source.addAll(filteredFiles); + } Logar.identify(source); } - else if (CLIUtils.isMatching(args, "sort", ".+")) + else if (CLIUtils.isMatchingEllipsis(args, "sort", ".+")) { - 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().sortByName(); + source.addAll(filteredFiles); + } Logar.sort(source); } - else if (CLIUtils.isMatching(args, "statuseragent", ".+")) + else if (CLIUtils.isMatchingEllipsis(args, "statuseragent", ".+")) { - 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().sortByName(); + source.addAll(filteredFiles); + } Logar.statUserAgents(source); } else if (CLIUtils.isMatching(args, "testconcate", ".+")) diff --git a/src/fr/devinsy/logar/util/Files.java b/src/fr/devinsy/logar/util/Files.java index 4d715fd..86033ee 100644 --- a/src/fr/devinsy/logar/util/Files.java +++ b/src/fr/devinsy/logar/util/Files.java @@ -38,6 +38,21 @@ public class Files extends ArrayList super(); } + /** + * Instantiates a new files. + * + * @param file + * the file + */ + public Files(final File file) + { + super(); + if (file != null) + { + add(file); + } + } + /** * Instantiates a new files. *