epugh commented on code in PR #1785:
URL: https://github.com/apache/solr/pull/1785#discussion_r1273448667


##########
solr/core/src/java/org/apache/solr/cli/CreateTool.java:
##########
@@ -43,24 +68,295 @@ public String getName() {
 
   @Override
   public List<Option> getOptions() {
-    return CreateCollectionTool.CREATE_COLLECTION_OPTIONS;
+    return List.of(
+        SolrCLI.OPTION_ZKHOST,
+        SolrCLI.OPTION_SOLRURL,
+        Option.builder("c")
+            .longOpt("name")
+            .argName("NAME")
+            .hasArg()
+            .required(true)
+            .desc("Name of collection or core to create.")
+            .build(),
+        Option.builder("s")
+            .longOpt("shards")
+            .argName("#")
+            .hasArg()
+            .required(false)
+            .desc("Number of shards; default is 1.")
+            .build(),
+        Option.builder("rf")
+            .longOpt("replicationFactor")
+            .argName("#")
+            .hasArg()
+            .required(false)
+            .desc(
+                "Number of copies of each document across the collection 
(replicas per shard); default is 1.")
+            .build(),
+        Option.builder("d")
+            .longOpt("confdir")
+            .argName("NAME")
+            .hasArg()
+            .required(false)
+            .desc(
+                "Configuration directory to copy when creating the new 
collection; default is "
+                    + SolrCLI.DEFAULT_CONFIG_SET
+                    + '.')
+            .build(),
+        Option.builder("n")
+            .longOpt("confname")
+            .argName("NAME")
+            .hasArg()
+            .required(false)
+            .desc("Configuration name; default is the collection name.")
+            .build(),
+        SolrCLI.OPTION_VERBOSE);
   }
 
   @Override
   public void runImpl(CommandLine cli) throws Exception {
     SolrCLI.raiseLogLevelUnlessVerbose(cli);
     String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.DEFAULT_SOLR_URL);
 
-    ToolBase tool;
     try (var solrClient = SolrCLI.getSolrClient(solrUrl)) {
-      NamedList<Object> systemInfo =
-          solrClient.request(new GenericSolrRequest(SolrRequest.METHOD.GET, 
SYSTEM_INFO_PATH));
-      if ("solrcloud".equals(systemInfo.get("mode"))) {
-        tool = new CreateCollectionTool(stdout);
+      if (SolrCLI.isCloudMode(solrClient)) {
+        createCollection(cli);
+      } else {
+        createCore(cli, solrClient);
+      }
+    }
+  }
+
+  protected void createCore(CommandLine cli, SolrClient solrClient) throws 
Exception {
+    String coreName = cli.getOptionValue("name");
+    String solrUrl = cli.getOptionValue("solrUrl", SolrCLI.DEFAULT_SOLR_URL);
+
+    final String solrInstallDir = System.getProperty("solr.install.dir");
+    final String confDirName = cli.getOptionValue("confdir", 
SolrCLI.DEFAULT_CONFIG_SET);
+
+    // we allow them to pass a directory instead of a configset name
+    Path configsetDir = Paths.get(confDirName);
+    Path solrInstallDirPath = Paths.get(solrInstallDir);
+
+    if (!Files.isDirectory(configsetDir)) {
+      ensureConfDirExists(configsetDir, solrInstallDirPath);
+    }
+    printDefaultConfigsetWarningIfNecessary(cli);
+
+    String coreRootDirectory; // usually same as solr home, but not always
+
+    Map<String, Object> systemInfo =
+        solrClient
+            .request(new GenericSolrRequest(SolrRequest.METHOD.GET, 
CommonParams.SYSTEM_INFO_PATH))
+            .asMap();
+
+    // convert raw JSON into user-friendly output
+    coreRootDirectory = (String) systemInfo.get("core_root");
+
+    if (SolrCLI.safeCheckCoreExists(solrUrl, coreName)) {
+      throw new IllegalArgumentException(
+          "\nCore '"
+              + coreName
+              + "' already exists!\nChecked core existence using Core API 
command");
+    }
+
+    Path coreInstanceDir = Paths.get(coreRootDirectory, coreName);
+    Path confDir = getFullConfDir(configsetDir, 
solrInstallDirPath).resolve("conf");
+    if (!Files.isDirectory(coreInstanceDir)) {
+      Files.createDirectories(coreInstanceDir);
+      if (!Files.isDirectory(coreInstanceDir)) {
+        throw new IOException(
+            "Failed to create new core instance directory: " + 
coreInstanceDir.toAbsolutePath());
+      }
+
+      FileUtils.copyDirectoryToDirectory(confDir.toFile(), 
coreInstanceDir.toFile());

Review Comment:
   Mostly that FileUtils already existed...   and I was worried about starting 
a full "Lets port everything to Paths"!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to