From a0ba83b8857df584ab5e1cf7849fc01da17554df Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 27 Feb 2023 22:45:52 +0100 Subject: [PATCH] Improved CLI commands. --- src/fr/devinsy/statoolinfos/cli/CLIUtils.java | 11 +- .../statoolinfos/cli/StatoolInfosCLI.java | 126 +++++++++--------- 2 files changed, 69 insertions(+), 68 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/cli/CLIUtils.java b/src/fr/devinsy/statoolinfos/cli/CLIUtils.java index e56a319..c652010 100644 --- a/src/fr/devinsy/statoolinfos/cli/CLIUtils.java +++ b/src/fr/devinsy/statoolinfos/cli/CLIUtils.java @@ -18,6 +18,7 @@ */ package fr.devinsy.statoolinfos.cli; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +48,7 @@ public final class CLIUtils public static boolean isMatching(final String[] args, final String... regexps) { boolean result; - + if ((args.length == 0) && (regexps == null)) { result = true; @@ -69,9 +70,9 @@ public final class CLIUtils { if (index < args.length) { - String arg = args[index]; + String arg = StringUtils.trim(args[index]); String regexp = regexps[index]; - + if (arg.matches(regexp)) { index += 1; @@ -89,7 +90,7 @@ public final class CLIUtils } } } - + // return result; } @@ -128,7 +129,7 @@ public final class CLIUtils { if (index < args.length) { - String arg = args[index]; + String arg = StringUtils.trim(args[index]); String regexp; if (index < regexps.length) { diff --git a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java index 3cf6058..4e0df43 100644 --- a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java +++ b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java @@ -116,14 +116,14 @@ public final class StatoolInfosCLI message.appendln(" statoolinfos tagdate update the file.datetime file"); message.appendln(" statoolinfos uptime update uptime journal"); message.appendln(); - message.appendln(" statoolinfos list files display http access log files"); - message.appendln(" statoolinfos list logs [-bot|-nobot] display http access log lines"); + message.appendln(" statoolinfos list file display http access log files"); + message.appendln(" statoolinfos list log [-bot|-nobot] display http access log lines"); message.appendln(" statoolinfos list ip [-bot|-nobot] generate ip list from http log file"); message.appendln(" statoolinfos list ua [-bot|-nobot] generate user agent list from http log file"); - message.appendln(" statoolinfos list visitors [-bot|-nobot] generate visitors (ip+ua) list from http log file"); + message.appendln(" statoolinfos list visitor [-bot|-nobot] generate visitors (ip+ua) list from http log file"); message.appendln(" statoolinfos stat ip [-bot|-nobot] generate stats about ip from http log file"); message.appendln(" statoolinfos stat ua [-bot|-nobot] generate stats about user agent from http log file"); - message.appendln(" statoolinfos stat visitors [-bot|-nobot] generate stats about visitors (ip+ua) from http log file"); + message.appendln(" statoolinfos stat visitor [-bot|-nobot] generate stats about visitor (ip+ua) from http log file"); System.out.print(message.toString()); } @@ -242,7 +242,7 @@ public final class StatoolInfosCLI { displayVersion(); } - else if (CLIUtils.isMatching(args, "build", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "build", ".+\\.conf")) { File configurationFile = new File(StringUtils.trim(args[1])); try @@ -254,7 +254,7 @@ public final class StatoolInfosCLI logger.error("Error with [{}]: {}", configurationFile.getAbsoluteFile(), exception.getMessage()); } } - else if (CLIUtils.isMatching(args, "clear", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "clear", ".+\\.conf*")) { File configurationFile = new File(StringUtils.trim(args[1])); try @@ -266,7 +266,7 @@ public final class StatoolInfosCLI logger.error("Error with [{}]: {}", configurationFile.getAbsoluteFile(), exception.getMessage()); } } - else if (CLIUtils.isMatching(args, "crawl", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "crawl", ".+\\.conf")) { Chrono chrono = new Chrono().start(); @@ -282,7 +282,7 @@ public final class StatoolInfosCLI } System.out.println(chrono.format()); } - else if (CLIUtils.isMatching(args, "format", "\\s*.+\\s*")) + else if (CLIUtils.isMatching(args, "format", ".+")) { Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties"); for (File input : inputs) @@ -298,7 +298,7 @@ public final class StatoolInfosCLI } } } - else if (CLIUtils.isMatching(args, "htmlize", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "htmlize", ".+\\.conf")) { Chrono chrono = new Chrono().start(); File configurationFile = new File(StringUtils.trim(args[1])); @@ -313,17 +313,7 @@ public final class StatoolInfosCLI } System.out.println(chrono.format()); } - else if (CLIUtils.isMatchingEllipsis(args, "list", "ip", "\\s*\\S+\\s*")) - { - Files files = new Files(); - for (int source = 2; source < args.length; source++) - { - files.add(new File(args[source])); - } - - StatoolInfos.listIps(files, BotFilter.ALL); - } - else if (CLIUtils.isMatching(args, "list", "ip", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "ip", "(-all|-bot|-nobot)", ".+")) { BotFilter filter = parseLogFilterOption(args[2]); Files source = new Files(); @@ -334,13 +324,23 @@ public final class StatoolInfosCLI StatoolInfos.listIps(source, filter); } - else if (CLIUtils.isMatching(args, "list", "files", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "ip", ".+")) + { + Files files = new Files(); + for (int source = 2; source < args.length; source++) + { + files.add(new File(args[source])); + } + + StatoolInfos.listIps(files, BotFilter.ALL); + } + else if (CLIUtils.isMatching(args, "list", "(file|files)", ".+\\.conf")) { File configurationFile = new File(StringUtils.trim(args[2])); StatoolInfos.listFiles(configurationFile); } - else if (CLIUtils.isMatchingEllipsis(args, "list", "files", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "(file|files)", ".+")) { Files source = new Files(); for (int index = 2; index < args.length; index++) @@ -350,30 +350,20 @@ public final class StatoolInfosCLI StatoolInfos.listFiles(source); } - else if (CLIUtils.isMatching(args, "list", "logs", "\\s*.+\\.conf\\s*")) - { - File configurationFile = new File(StringUtils.trim(args[2])); - - StatoolInfos.listLogs(configurationFile, BotFilter.ALL); - } - else if (CLIUtils.isMatching(args, "list", "logs", "(-all|-bot|-nobot)", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "list", "(log|logs)", "(-all|-bot|-nobot)", ".+\\.conf*")) { BotFilter filter = parseLogFilterOption(args[2]); File configurationFile = new File(StringUtils.trim(args[3])); StatoolInfos.listLogs(configurationFile, filter); } - else if (CLIUtils.isMatchingEllipsis(args, "list", "logs", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "list", "(log|logs)", ".+\\.conf")) { - Files source = new Files(); - for (int index = 2; index < args.length; index++) - { - source.add(new File(args[index])); - } + File configurationFile = new File(StringUtils.trim(args[2])); - StatoolInfos.listLogs(source, BotFilter.ALL); + StatoolInfos.listLogs(configurationFile, BotFilter.ALL); } - else if (CLIUtils.isMatchingEllipsis(args, "list", "logs", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "(log|logs)", "(-all|-bot|-nobot)", ".+")) { BotFilter filter = parseLogFilterOption(args[2]); Files source = new Files(); @@ -384,7 +374,7 @@ public final class StatoolInfosCLI StatoolInfos.listLogs(source, filter); } - else if (CLIUtils.isMatching(args, "list", "(useragent|ua)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "(log|logs)", ".+")) { Files source = new Files(); for (int index = 2; index < args.length; index++) @@ -392,9 +382,9 @@ public final class StatoolInfosCLI source.add(new File(args[index])); } - StatoolInfos.listUserAgents(source, BotFilter.ALL); + StatoolInfos.listLogs(source, BotFilter.ALL); } - else if (CLIUtils.isMatching(args, "list", "(useragent|ua)", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "(useragent|ua)", "(-all|-bot|-nobot)", ".+")) { BotFilter filter = parseLogFilterOption(args[2]); Files source = new Files(); @@ -405,7 +395,7 @@ public final class StatoolInfosCLI StatoolInfos.listUserAgents(source, filter); } - else if (CLIUtils.isMatching(args, "list", "visitors", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "(useragent|ua)", ".+")) { Files source = new Files(); for (int index = 2; index < args.length; index++) @@ -413,9 +403,9 @@ public final class StatoolInfosCLI source.add(new File(args[index])); } - StatoolInfos.listVisitors(source, BotFilter.ALL); + StatoolInfos.listUserAgents(source, BotFilter.ALL); } - else if (CLIUtils.isMatching(args, "list", "visitors", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "(visitor|visitors)", "(-all|-bot|-nobot)", ".+")) { BotFilter filter = parseLogFilterOption(args[2]); Files source = new Files(); @@ -426,7 +416,17 @@ public final class StatoolInfosCLI StatoolInfos.listVisitors(source, filter); } - else if (CLIUtils.isMatching(args, "probe", "\\S*(-full|-today|-previousday|-\\d+)\\s*", "\\s*.+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "list", "(visitor|visitors)", ".+")) + { + Files source = new Files(); + for (int index = 2; index < args.length; index++) + { + source.add(new File(args[index])); + } + + StatoolInfos.listVisitors(source, BotFilter.ALL); + } + else if (CLIUtils.isMatching(args, "probe", "(-full|-today|-previousday|-\\d+)", ".+")) { File configurationFile = new File(StringUtils.trim(args[2])); @@ -479,17 +479,7 @@ public final class StatoolInfosCLI exception.printStackTrace(); } } - else if (CLIUtils.isMatching(args, "stat", "ip", "\\s*\\S+\\s*")) - { - Files source = new Files(); - for (int index = 2; index < args.length; index++) - { - source.add(new File(args[index])); - } - - StatoolInfos.statIps(source, BotFilter.ALL); - } - else if (CLIUtils.isMatching(args, "stat", "ip", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "stat", "ip", "(-all|-bot|-nobot)", ".+")) { BotFilter filter = parseLogFilterOption(args[2]); Files source = new Files(); @@ -500,7 +490,7 @@ public final class StatoolInfosCLI StatoolInfos.statIps(source, filter); } - else if (CLIUtils.isMatching(args, "stat", "(useragent|ua)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "stat", "ip", ".+")) { Files source = new Files(); for (int index = 2; index < args.length; index++) @@ -508,9 +498,9 @@ public final class StatoolInfosCLI source.add(new File(args[index])); } - StatoolInfos.statUserAgents(source, BotFilter.ALL); + StatoolInfos.statIps(source, BotFilter.ALL); } - else if (CLIUtils.isMatching(args, "stat", "(useragent|ua)", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "stat", "(useragent|ua)", "(-all|-bot|-nobot)", ".+")) { BotFilter filter = parseLogFilterOption(args[2]); Files source = new Files(); @@ -521,7 +511,7 @@ public final class StatoolInfosCLI StatoolInfos.statUserAgents(source, filter); } - else if (CLIUtils.isMatching(args, "stat", "visitors", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "stat", "(useragent|ua)", ".+")) { Files source = new Files(); for (int index = 2; index < args.length; index++) @@ -529,9 +519,9 @@ public final class StatoolInfosCLI source.add(new File(args[index])); } - StatoolInfos.statVisitors(source, BotFilter.ALL); + StatoolInfos.statUserAgents(source, BotFilter.ALL); } - else if (CLIUtils.isMatching(args, "stat", "visitors", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "stat", "(visitor|visitors)", "(-all|-bot|-nobot)", ".+")) { BotFilter filter = parseLogFilterOption(args[2]); Files source = new Files(); @@ -542,7 +532,17 @@ public final class StatoolInfosCLI StatoolInfos.statVisitors(source, filter); } - else if (CLIUtils.isMatching(args, "tagdate", "\\s*.+\\s*")) + else if (CLIUtils.isMatchingEllipsis(args, "stat", "(visitor|visitors)", ".+")) + { + Files source = new Files(); + for (int index = 2; index < args.length; index++) + { + source.add(new File(args[index])); + } + + StatoolInfos.statVisitors(source, BotFilter.ALL); + } + else if (CLIUtils.isMatching(args, "tagdate", ".+")) { Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties"); for (File input : inputs) @@ -558,7 +558,7 @@ public final class StatoolInfosCLI } } } - else if (CLIUtils.isMatching(args, "uptime", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "uptime", ".+\\.conf")) { Chrono chrono = new Chrono().start();