Apache Solr open-source search software
Refactor duplicate CLI options (#791)
Mike Drob645dd9b132a0
diff --git a/solr/core/src/java/org/apache/solr/util/SolrCLI.java b/solr/core/src/java/org/apache/solr/util/SolrCLI.javaindex 15dc4fe1253..c4a29717c66 100755--- a/solr/core/src/java/org/apache/solr/util/SolrCLI.java+++ b/solr/core/src/java/org/apache/solr/util/SolrCLI.java@@ -152,7 +152,7 @@ public class SolrCLI implements CLIO {}protected void echoIfVerbose(final String msg, CommandLine cli) {- if (cli.hasOption("verbose")) {+ if (cli.hasOption(OPTION_VERBOSE.getOpt())) {echo(msg);}}@@ -162,7 +162,7 @@ public class SolrCLI implements CLIO {}public int runTool(CommandLine cli) throws Exception {- verbose = cli.hasOption("verbose");+ verbose = cli.hasOption(OPTION_VERBOSE.getOpt());int toolExitStatus = 0;try {@@ -201,7 +201,7 @@ public class SolrCLI implements CLIO {protected void runImpl(CommandLine cli) throws Exception {raiseLogLevelUnlessVerbose(cli);- String zkHost = cli.getOptionValue("zkHost", ZK_HOST);+ String zkHost = cli.getOptionValue(OPTION_ZKHOST.getOpt(), ZK_HOST);log.debug("Connecting to Solr cluster: {}", zkHost);try (var cloudSolrClient =@@ -225,14 +225,38 @@ public class SolrCLI implements CLIO {public static final String DEFAULT_SOLR_URL = "http://localhost:8983/solr";public static final String ZK_HOST = "localhost:9983";- public static Option[] cloudOptions =+ public static final Option OPTION_ZKHOST =+ Option.builder("z")+ .argName("HOST")+ .hasArg()+ .required(false)+ .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST)+ .longOpt("zkHost")+ .build();+ public static final Option OPTION_SOLRURL =+ Option.builder("solrUrl")+ .argName("HOST")+ .hasArg()+ .required(false)+ .desc(+ "Base Solr URL, which can be used to determine the zkHost if that's not known; defaults to: "+ + DEFAULT_SOLR_URL)+ .build();+ public static final Option OPTION_VERBOSE =+ Option.builder("verbose").required(false).desc("Enable more verbose command output.").build();++ public static final Option OPTION_RECURSE =+ Option.builder("recurse")+ .argName("recurse")+ .hasArg()+ .required(false)+ .desc("Recurse (true|false), default is false.")+ // .type(Boolean.class)+ .build();++ public static final Option[] cloudOptions =new Option[] {- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(false)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),+ OPTION_ZKHOST,Option.builder("c").argName("COLLECTION").hasArg()@@ -240,10 +264,7 @@ public class SolrCLI implements CLIO {.desc("Name of collection; no default.").longOpt("collection").build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_VERBOSE};private static void exit(int exitStatus) {@@ -300,8 +321,8 @@ public class SolrCLI implements CLIO {public static CommandLine parseCmdLine(String toolName, String[] args, Option[] toolOptions)throws Exception {// the parser doesn't like -D props- List<String> toolArgList = new ArrayList<String>();- List<String> dashDList = new ArrayList<String>();+ List<String> toolArgList = new ArrayList<>();+ List<String> dashDList = new ArrayList<>();for (int a = 1; a < args.length; a++) {String arg = args[a];if (arg.startsWith("-D")) {@@ -353,7 +374,7 @@ public class SolrCLI implements CLIO {}private static void raiseLogLevelUnlessVerbose(CommandLine cli) {- if (!cli.hasOption("verbose")) {+ if (!cli.hasOption(OPTION_VERBOSE.getOpt())) {StartupLoggingUtils.changeLogLevel("WARN");}}@@ -427,7 +448,7 @@ public class SolrCLI implements CLIO {private static Options getToolOptions(Tool tool) {Options options = new Options();options.addOption("help", false, "Print this message");- options.addOption("verbose", false, "Generate verbose log messages");+ options.addOption(OPTION_VERBOSE);Option[] toolOpts = joinCommonAndToolOptions(tool.getOptions());for (int i = 0; i < toolOpts.length; i++) options.addOption(toolOpts[i]);return options;@@ -438,16 +459,19 @@ public class SolrCLI implements CLIO {}public static Option[] joinOptions(Option[] lhs, Option[] rhs) {- List<Option> options = new ArrayList<Option>();- if (lhs != null && lhs.length > 0) {- for (Option opt : lhs) options.add(opt);+ if (lhs == null) {+ return rhs == null ? new Option[0] : rhs;}- if (rhs != null) {- for (Option opt : rhs) options.add(opt);+ if (rhs == null) {+ return lhs;}- return options.toArray(new Option[0]);+ Option[] options = new Option[lhs.length + rhs.length];+ System.arraycopy(lhs, 0, options, 0, lhs.length);+ System.arraycopy(rhs, 0, options, lhs.length, rhs.length);++ return options;}/**@@ -464,7 +488,7 @@ public class SolrCLI implements CLIO {Options options = new Options();options.addOption("help", false, "Print this message");- options.addOption("verbose", false, "Generate verbose log messages");+ options.addOption(OPTION_VERBOSE);if (customOptions != null) {for (int i = 0; i < customOptions.length; i++) options.addOption(customOptions[i]);@@ -507,7 +531,7 @@ public class SolrCLI implements CLIO {ClassLoader classLoader = SolrCLI.class.getClassLoader();String path = packageName.replace('.', '/');Enumeration<URL> resources = classLoader.getResources(path);- Set<String> classes = new TreeSet<String>();+ Set<String> classes = new TreeSet<>();while (resources.hasMoreElements()) {URL resource = resources.nextElement();classes.addAll(findClasses(resource.getFile(), packageName));@@ -525,7 +549,7 @@ public class SolrCLI implements CLIO {}private static Set<String> findClasses(String path, String packageName) throws Exception {- Set<String> classes = new TreeSet<String>();+ Set<String> classes = new TreeSet<>();if (path.startsWith("file:") && path.contains("!")) {String[] split = path.split("!");URL jar = new URL(split[0]);@@ -685,7 +709,7 @@ public class SolrCLI implements CLIO {+ respBody+ "\nTypically, this indicates a problem with the Solr server; check the Solr server logs for more information.");}- if (resp != null && resp instanceof Map) {+ if (resp instanceof Map) {return (Map<String, Object>) resp;} else {throw new ClientProtocolException(@@ -953,7 +977,7 @@ public class SolrCLI implements CLIO {public Map<String, Object> reportStatus(String solrUrl, Map<String, Object> info, HttpClient httpClient) throws Exception {- Map<String, Object> status = new LinkedHashMap<String, Object>();+ Map<String, Object> status = new LinkedHashMap<>();String solrHome = (String) info.get("solr_home");status.put("solr_home", solrHome != null ? solrHome : "?");@@ -980,7 +1004,7 @@ public class SolrCLI implements CLIO {*/protected Map<String, String> getCloudStatus(HttpClient httpClient, String solrUrl, String zkHost) throws Exception {- Map<String, String> cloudStatus = new LinkedHashMap<String, String>();+ Map<String, String> cloudStatus = new LinkedHashMap<>();cloudStatus.put("ZooKeeper", (zkHost != null) ? zkHost : "?");String clusterStatusUrl = solrUrl + "admin/collections?action=CLUSTERSTATUS";@@ -1091,7 +1115,7 @@ public class SolrCLI implements CLIO {}public Map<String, Object> asMap() {- Map<String, Object> map = new LinkedHashMap<String, Object>();+ Map<String, Object> map = new LinkedHashMap<>();map.put(NAME, name);map.put("url", url);map.put("numDocs", numDocs);@@ -1137,7 +1161,7 @@ public class SolrCLI implements CLIO {}}- static enum ShardState {+ enum ShardState {healthy,degraded,down,@@ -1178,7 +1202,7 @@ public class SolrCLI implements CLIO {Map<String, Object> map = new LinkedHashMap<>();map.put("shard", shard);map.put("status", getShardState().toString());- List<Object> replicaList = new ArrayList<Object>();+ List<Object> replicaList = new ArrayList<>();for (ReplicaHealth replica : replicas) replicaList.add(replica.asMap());map.put("replicas", replicaList);return map;@@ -1256,7 +1280,7 @@ public class SolrCLI implements CLIO {log.warn("Failed to get leader for shard {} due to: {}", shardName, exc);}- List<ReplicaHealth> replicaList = new ArrayList<ReplicaHealth>();+ List<ReplicaHealth> replicaList = new ArrayList<>();for (Replica r : slice.getReplicas()) {String uptime = null;@@ -1324,7 +1348,7 @@ public class SolrCLI implements CLIO {shardList.add(shardHealth.asMap());}- Map<String, Object> report = new LinkedHashMap<String, Object>();+ Map<String, Object> report = new LinkedHashMap<>();report.put("collection", collection);report.put("status", collectionIsHealthy ? "healthy" : "degraded");if (collErr != null) {@@ -1342,18 +1366,8 @@ public class SolrCLI implements CLIO {private static final Option[] CREATE_COLLECTION_OPTIONS =new Option[] {- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(false)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("solrUrl")- .argName("HOST")- .hasArg()- .required(false)- .desc("Base Solr URL, which can be used to determine the zkHost if that's not known.")- .build(),+ OPTION_ZKHOST,+ OPTION_SOLRURL,Option.builder(NAME).argName("NAME").hasArg()@@ -1394,10 +1408,7 @@ public class SolrCLI implements CLIO {.required(true).desc("Path to configsets directory on the local system.").build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_VERBOSE};/**@@ -1646,7 +1657,7 @@ public class SolrCLI implements CLIO {"Failed to create collection '" + collectionName + "' due to: " + sse.getMessage());}- if (cli.hasOption("verbose")) {+ if (cli.hasOption(OPTION_VERBOSE.getOpt())) {CharArr arr = new CharArr();new JSONWriter(arr, 2).write(json);echo(arr.toString());@@ -1687,12 +1698,7 @@ public class SolrCLI implements CLIO {public Option[] getOptions() {return new Option[] {- Option.builder("solrUrl")- .argName("URL")- .hasArg()- .required(false)- .desc("Base Solr URL, default is " + DEFAULT_SOLR_URL + '.')- .build(),+ OPTION_SOLRURL,Option.builder(NAME).argName("NAME").hasArg()@@ -1714,10 +1720,7 @@ public class SolrCLI implements CLIO {.required(true).desc("Path to configsets directory on the local system.").build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_VERBOSE};}@@ -1822,7 +1825,7 @@ public class SolrCLI implements CLIO {try {Map<String, Object> json = getJson(createCoreUrl);- if (cli.hasOption("verbose")) {+ if (cli.hasOption(OPTION_VERBOSE.getOpt())) {CharArr arr = new CharArr();new JSONWriter(arr, 2).write(json);echo(arr.toString());@@ -1909,16 +1912,8 @@ public class SolrCLI implements CLIO {.required(false).desc("Parent directory of example configsets.").build(),- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(true)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_ZKHOST,+ OPTION_VERBOSE};}@@ -1987,16 +1982,8 @@ public class SolrCLI implements CLIO {.required(true).desc("Local directory with configs.").build(),- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(true)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_ZKHOST,+ OPTION_VERBOSE};}@@ -2059,22 +2046,9 @@ public class SolrCLI implements CLIO {.required(true).desc("Path to remove.").build(),- Option.builder("recurse")- .argName("recurse")- .hasArg()- .required(false)- .desc("Recurse (true|false), default is false.")- .build(),- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(true)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_RECURSE,+ OPTION_ZKHOST,+ OPTION_VERBOSE};}@@ -2141,22 +2115,9 @@ public class SolrCLI implements CLIO {.required(true).desc("Path to list.").build(),- Option.builder("recurse")- .argName("recurse")- .hasArg()- .required(false)- .desc("Recurse (true|false), default is false.")- .build(),- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(true)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_RECURSE,+ OPTION_ZKHOST,+ OPTION_VERBOSE};}@@ -2214,16 +2175,8 @@ public class SolrCLI implements CLIO {.required(true).desc("Path to create.").build(),- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(true)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_ZKHOST,+ OPTION_VERBOSE};}@@ -2279,22 +2232,9 @@ public class SolrCLI implements CLIO {.required(true).desc("Destination of copy, may be local or a Znode.").build(),- Option.builder("recurse")- .argName("recurse")- .hasArg()- .required(false)- .desc("Recurse (true|false), default is false.")- .build(),- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(true)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_RECURSE,+ OPTION_ZKHOST,+ OPTION_VERBOSE};}@@ -2369,16 +2309,8 @@ public class SolrCLI implements CLIO {.required(true).desc("Destination Znode to move to.").build(),- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(true)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_ZKHOST,+ OPTION_VERBOSE};}@@ -2441,12 +2373,7 @@ public class SolrCLI implements CLIO {public Option[] getOptions() {return new Option[] {- Option.builder("solrUrl")- .argName("URL")- .hasArg()- .required(false)- .desc("Base Solr URL, default is " + DEFAULT_SOLR_URL + '.')- .build(),+ OPTION_SOLRURL,Option.builder(NAME).argName("NAME").hasArg()@@ -2465,16 +2392,8 @@ public class SolrCLI implements CLIO {.desc("Skip safety checks when deleting the configuration directory used by a collection.").build(),- Option.builder("zkHost")- .argName("HOST")- .hasArg()- .required(false)- .desc("Address of the ZooKeeper ensemble; defaults to: " + ZK_HOST + '.')- .build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_ZKHOST,+ OPTION_VERBOSE};}@@ -2547,19 +2466,21 @@ public class SolrCLI implements CLIO {configName);}- for (String next : collections) {- if (collectionName.equals(next)) continue; // don't check the collection we're deleting-- if (configName.equals(- zkStateReader.getClusterState().getCollection(next).getConfigName())) {- deleteConfig = false;- log.warn(- "Configuration directory {} is also being used by {}{}",- configName,- next,- "; configuration will not be deleted from ZooKeeper. You can pass the -forceDeleteConfig flag to force delete.");- break;- }+ Optional<String> inUse =+ collections.stream()+ .filter(name -> !name.equals(collectionName)) // ignore this collection+ .filter(+ name ->+ configName.equals(+ zkStateReader.getClusterState().getCollection(name).getConfigName()))+ .findFirst();+ if (inUse.isPresent()) {+ deleteConfig = false;+ log.warn(+ "Configuration directory {} is also being used by {}{}",+ configName,+ inUse.get(),+ "; configuration will not be deleted from ZooKeeper. You can pass the -forceDeleteConfig flag to force delete.");}}}@@ -2675,20 +2596,8 @@ public class SolrCLI implements CLIO {.required(false).desc("Set the property to this value; accepts JSON objects and strings.").build(),- Option.builder("solrUrl")- .argName("HOST")- .hasArg()- .required(false)- .desc(- "Base Solr URL, which can be used to determine the zkHost if that's not known.")- .build(),- Option.builder("z")- .argName("HOST")- .hasArg()- .required(false)- .desc("Address of the ZooKeeper ensemble.")- .longOpt("zkHost")- .build(),+ OPTION_SOLRURL,+ OPTION_ZKHOST,Option.builder("p").argName("PORT").hasArg()@@ -2924,15 +2833,14 @@ public class SolrCLI implements CLIO {+ exampleDir.getAbsolutePath()+ " is not a directory!");- if (verbose) {- echo(- "Running with\nserverDir="- + serverDir.getAbsolutePath()- + ",\nexampleDir="- + exampleDir.getAbsolutePath()- + "\nscript="- + script);- }+ echoIfVerbose(+ "Running with\nserverDir="+ + serverDir.getAbsolutePath()+ + ",\nexampleDir="+ + exampleDir.getAbsolutePath()+ + "\nscript="+ + script,+ cli);String exampleType = cli.getOptionValue("example");if ("cloud".equals(exampleType)) {@@ -3161,7 +3069,7 @@ public class SolrCLI implements CLIO {}cloudPorts[n] = port;- if (verbose) echo("Using port " + port + " for node " + (n + 1));+ echoIfVerbose("Using port " + port + " for node " + (n + 1), cli);}} else {echo("Starting up " + numNodes + " Solr nodes for your example SolrCloud cluster.\n");@@ -3845,7 +3753,7 @@ public class SolrCLI implements CLIO {}public int runTool(CommandLine cli) throws Exception {- verbose = cli.hasOption("verbose");+ verbose = cli.hasOption(OPTION_VERBOSE.getOpt());int toolExitStatus = 0;try {@@ -4171,10 +4079,7 @@ public class SolrCLI implements CLIO {.hasArg().desc("ZooKeeper host to connect to.").build(),- Option.builder("verbose")- .required(false)- .desc("Enable more verbose command output.")- .build()+ OPTION_VERBOSE};}