diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/RegistryManagerCli.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/RegistryManagerCli.java index 2592734c..63bd29d9 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/RegistryManagerCli.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/RegistryManagerCli.java @@ -94,8 +94,11 @@ public static void printHelp() System.out.println(); System.out.println("Global required parameters:"); + System.out.println(" -c Path to Harvest configuration XML (preferred). Registry URL and auth are read from the XML."); // NOSONAR + System.out.println(); + System.out.println(" Deprecated (use -c instead):"); // NOSONAR System.out.println(" -auth Authentication config file"); - System.out.println(" -registry File URI to the configuration to connect to the registry. For example, file:///home/user/.pds/mcp.xml. Default is app:/connections/direct/localhost.xml"); + System.out.println(" -registry File URI to the configuration to connect to the registry. For example, file:///home/user/.pds/mcp.xml"); // NOSONAR System.out.println(); System.out.println("Other:"); @@ -177,16 +180,11 @@ private static void initLogger(CommandLine cmdLine) private boolean checkVersion() throws Exception { - // if create-registry, then return true. - // check the database for correct versions - // print if not the correct version and to upgrade to latest tool if (this.commands.get("create-registry") == this.command) { - return true; // short cut out so that repo can be created + return true; } return Version.instance().checkVersion( - EstablishConnectionFactory.from( - CliCommand.getUsersRegistry(cmdLine), - cmdLine.getOptionValue("auth")), + EstablishConnectionFactory.from(CliCommand.getRegistryUrl(cmdLine), CliCommand.getAuthFile(cmdLine)), Arrays.asList(gov.nasa.pds.registry.common.Version.instance(), Version.instance().subcommand(this.cmdname))); } @@ -302,7 +300,10 @@ private void initOptions() bld = Option.builder("help"); options.addOption(bld.build()); - + + bld = Option.builder("c").hasArg().argName("file"); + options.addOption(bld.build()); + bld = Option.builder("registry").hasArg().argName("url"); options.addOption(bld.build()); diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/CliCommand.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/CliCommand.java index 6f3086dd..09a13db3 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/CliCommand.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/CliCommand.java @@ -1,27 +1,66 @@ package gov.nasa.pds.registry.mgr.cmd; +import java.io.File; import org.apache.commons.cli.CommandLine; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import gov.nasa.pds.registry.mgr.util.HarvestConfigReader; /** - * All Registry Manager command-line interface (CLI) commands such as + * All Registry Manager command-line interface (CLI) commands such as * "create-registry", "delete-registry", "load-data", etc. * should implement this interface. - * + * * @author karpenko */ public interface CliCommand { + Logger LOG = LogManager.getLogger(CliCommand.class); + /** - * Run CLI command. + * Run CLI command. * @param cmdLine Command line parameters. * @throws Exception an exception */ public void run(CommandLine cmdLine) throws Exception; - public static String getUsersRegistry (CommandLine cmdLine) { - if (cmdLine.hasOption("registry")) { - return cmdLine.getOptionValue("registry"); - } else { - throw new RuntimeException("Must provide -registry on the command line. See usuage for more details."); - } + + /** + * Returns {registryUrl, authFile} from either -c (harvest config XML) or + * the deprecated -registry / -auth flags. + */ + public static String[] getConfigPair(CommandLine cmdLine) throws Exception { + if (cmdLine.hasOption("c")) { + return HarvestConfigReader.readRegistryAndAuth(new File(cmdLine.getOptionValue("c"))); + } + if (cmdLine.hasOption("registry")) { + LOG.warn("-registry is deprecated; use -c instead"); + if (cmdLine.hasOption("auth")) { + LOG.warn("-auth is deprecated; use -c instead"); + } + return new String[]{cmdLine.getOptionValue("registry"), cmdLine.getOptionValue("auth")}; + } + throw new IllegalArgumentException("Must provide -c on the command line. See usage for more details."); + } + + /** Returns the registry connection URL from -c or the deprecated -registry flag. */ + public static String getRegistryUrl(CommandLine cmdLine) throws Exception { + return getConfigPair(cmdLine)[0]; + } + + /** Returns the auth file path from -c or the deprecated -auth flag. */ + public static String getAuthFile(CommandLine cmdLine) throws Exception { + return getConfigPair(cmdLine)[1]; + } + + /** @deprecated Use getRegistryUrl(cmdLine) instead. */ + @Deprecated(since = "1.3.0") + public static String getUsersRegistry(CommandLine cmdLine) { + try { + return getConfigPair(cmdLine)[0]; + } catch (RuntimeException ex) { + throw ex; + } catch (Exception ex) { + throw new IllegalArgumentException(ex.getMessage(), ex); + } } } diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/DeleteDataCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/DeleteDataCmd.java index 48157f95..8333d4eb 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/DeleteDataCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/DeleteDataCmd.java @@ -39,9 +39,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); - + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); log.info("Elasticsearch URL: {}", esUrl); diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/ExportDataCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/ExportDataCmd.java index aaa585c1..6e6a6d4f 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/ExportDataCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/ExportDataCmd.java @@ -46,8 +46,8 @@ public void run(CommandLine cmdLine) throws Exception throw new Exception("Missing required parameter '-file'"); } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); extractFilterParams(cmdLine); if(filterType == null) diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/ExportFileCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/ExportFileCmd.java index 5cdb0f2d..b7204339 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/ExportFileCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/ExportFileCmd.java @@ -41,9 +41,9 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); - + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); + // Lidvid String lidvid = cmdLine.getOptionValue("lidvid"); if(lidvid == null) diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java index be362b51..300d955a 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java @@ -43,9 +43,9 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); - + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); + String status = getStatus(cmdLine); String lidvid = cmdLine.getOptionValue("lidvid"), pid = cmdLine.getOptionValue("packageId"); diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateAltIdsCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateAltIdsCmd.java index 891879f7..100b4d45 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateAltIdsCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateAltIdsCmd.java @@ -47,8 +47,8 @@ public void run(CommandLine cmdLine) throws Exception printHelp(); return; } - String url = CliCommand.getUsersRegistry(cmdLine); - String authFile = cmdLine.getOptionValue("auth"); + String url = CliCommand.getRegistryUrl(cmdLine); + String authFile = CliCommand.getAuthFile(cmdLine); String filePath = cmdLine.getOptionValue("file"); if(filePath == null) throw new Exception("Missing required parameter '-file'"); File file = new File(filePath); diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateToolVersionCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateToolVersionCmd.java index 641c557d..825f6fdd 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateToolVersionCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateToolVersionCmd.java @@ -16,8 +16,8 @@ public void run(CommandLine cmdLine) throws Exception { printHelp(); return; } - String url = CliCommand.getUsersRegistry(cmdLine); - String authFile = cmdLine.getOptionValue("auth"); + String url = CliCommand.getRegistryUrl(cmdLine); + String authFile = CliCommand.getAuthFile(cmdLine); String[] args = cmdLine.getArgs(); String[] parts; diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/DeleteDDCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/DeleteDDCmd.java index 65d96bfc..83f441ba 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/DeleteDDCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/DeleteDDCmd.java @@ -39,8 +39,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - esUrl = CliCommand.getUsersRegistry(cmdLine); - authPath = cmdLine.getOptionValue("auth"); + esUrl = CliCommand.getRegistryUrl(cmdLine); + authPath = CliCommand.getAuthFile(cmdLine); this.conFact = EstablishConnectionFactory.from(esUrl, authPath); String id = cmdLine.getOptionValue("id"); if(id != null) diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/ExportDDCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/ExportDDCmd.java index 951c79f0..675e8b07 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/ExportDDCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/ExportDDCmd.java @@ -39,8 +39,8 @@ public void run(CommandLine cmdLine) throws Exception throw new Exception("Missing required parameter '-file'"); } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); System.out.println("Elasticsearch URL: " + esUrl); System.out.println(); diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/ListDDCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/ListDDCmd.java index b29778f8..600f9844 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/ListDDCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/ListDDCmd.java @@ -34,15 +34,12 @@ public void run(CommandLine cmdLine) throws Exception return; } - String authFile = cmdLine.getOptionValue("auth"); + String url = CliCommand.getRegistryUrl(cmdLine); + String authFile = CliCommand.getAuthFile(cmdLine); String namespace = cmdLine.getOptionValue("ns"); - String url = CliCommand.getUsersRegistry(cmdLine); - if (authFile == null || authFile.isBlank()) { - throw new IllegalArgumentException("missing argument: -auth must be given"); - } if (namespace == null || namespace.isBlank()) { - throw new IllegalArgumentException("missing argument: -ns must be given"); + throw new IllegalArgumentException("missing argument: -ns must be given"); } try { diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/LoadDDCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/LoadDDCmd.java index ac76eaab..c341441c 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/LoadDDCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/LoadDDCmd.java @@ -65,8 +65,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - this.url = CliCommand.getUsersRegistry(cmdLine); - this.authFile = cmdLine.getOptionValue("auth"); + this.url = CliCommand.getRegistryUrl(cmdLine); + this.authFile = CliCommand.getAuthFile(cmdLine); this.indexName = EstablishConnectionFactory.from(this.url, this.authFile).getIndexName(); RegistryManager.init(url, authFile); diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/UpgradeDDCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/UpgradeDDCmd.java index 8e28ec2c..467d8b78 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/UpgradeDDCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/dd/UpgradeDDCmd.java @@ -39,9 +39,9 @@ public void run(CommandLine cmdLine) throws Exception return; } - esUrl = CliCommand.getUsersRegistry(cmdLine); - authPath = cmdLine.getOptionValue("auth"); - + esUrl = CliCommand.getRegistryUrl(cmdLine); + authPath = CliCommand.getAuthFile(cmdLine); + boolean replace= cmdLine.hasOption("r"); RestClient client = null; diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/reg/CreateRegistryCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/reg/CreateRegistryCmd.java index 76be8513..10c4b984 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/reg/CreateRegistryCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/reg/CreateRegistryCmd.java @@ -39,8 +39,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); int shards = parseShards(cmdLine.getOptionValue("shards", "1")); int replicas = parseReplicas(cmdLine.getOptionValue("replicas", "0")); diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/reg/DeleteRegistryCmd.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/reg/DeleteRegistryCmd.java index 12115539..086b2c51 100644 --- a/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/reg/DeleteRegistryCmd.java +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/reg/DeleteRegistryCmd.java @@ -34,8 +34,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); RestClient client = null; diff --git a/manager/src/main/java/gov/nasa/pds/registry/mgr/util/HarvestConfigReader.java b/manager/src/main/java/gov/nasa/pds/registry/mgr/util/HarvestConfigReader.java new file mode 100644 index 00000000..7edbc075 --- /dev/null +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/util/HarvestConfigReader.java @@ -0,0 +1,42 @@ +package gov.nasa.pds.registry.mgr.util; + +import java.io.File; +import java.io.IOException; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * Reads the <registry> element from a Harvest configuration XML file, + * returning the registry connection URL and optional auth file path. + */ +public class HarvestConfigReader { + + private HarvestConfigReader() {} + + /** + * Parses a Harvest config XML and returns {registryUrl, authFile}. + * authFile may be null if the auth attribute is absent or empty. + */ + public static String[] readRegistryAndAuth(File configFile) throws IOException { + try { + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configFile); + NodeList nodes = doc.getElementsByTagName("registry"); + if (nodes.getLength() == 0) { + throw new IOException("No element found in: " + configFile.getAbsolutePath()); + } + Element regEl = (Element) nodes.item(0); + String url = regEl.getTextContent().trim(); + if (url.isEmpty()) { + throw new IOException(" element has no URL value in: " + configFile.getAbsolutePath()); + } + String auth = regEl.getAttribute("auth").trim(); + return new String[]{url, auth.isEmpty() ? null : auth}; + } catch (ParserConfigurationException | SAXException ex) { + throw new IOException("Failed to parse harvest config: " + configFile.getAbsolutePath(), ex); + } + } +}