From e8c993af8fe683a76e0b1cdc101bf9a338844e69 Mon Sep 17 00:00:00 2001 From: Jordan Padams Date: Sat, 16 May 2026 21:34:12 -0700 Subject: [PATCH 1/2] Add -c flag to registry-manager and deprecate -registry/-auth flags Adds a -c option to registry-manager that reads the registry connection URL and auth file from the same Harvest configuration XML that the harvest tool uses, eliminating the need to pass -registry and -auth separately. The old flags are retained but emit a deprecation warning. Resolves #62 Co-Authored-By: Claude Sonnet 4 --- .../pds/registry/mgr/RegistryManagerCli.java | 20 +++++---- .../nasa/pds/registry/mgr/cmd/CliCommand.java | 43 +++++++++++++++---- .../registry/mgr/cmd/data/DeleteDataCmd.java | 6 +-- .../registry/mgr/cmd/data/ExportDataCmd.java | 5 ++- .../registry/mgr/cmd/data/ExportFileCmd.java | 7 +-- .../mgr/cmd/data/SetArchiveStatusCmd.java | 7 +-- .../mgr/cmd/data/UpdateAltIdsCmd.java | 5 ++- .../mgr/cmd/data/UpdateToolVersionCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/DeleteDDCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/ExportDDCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/ListDDCmd.java | 10 ++--- .../pds/registry/mgr/cmd/dd/LoadDDCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/UpgradeDDCmd.java | 7 +-- .../mgr/cmd/reg/CreateRegistryCmd.java | 5 ++- .../mgr/cmd/reg/DeleteRegistryCmd.java | 5 ++- .../mgr/util/HarvestConfigReader.java | 33 ++++++++++++++ 16 files changed, 121 insertions(+), 52 deletions(-) create mode 100644 manager/src/main/java/gov/nasa/pds/registry/mgr/util/HarvestConfigReader.java 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..d438d099 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."); + System.out.println(); + System.out.println(" Deprecated (use -c instead):"); 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"); System.out.println(); System.out.println("Other:"); @@ -177,16 +180,12 @@ 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; } + String[] cfg = CliCommand.getConfigPair(cmdLine); return Version.instance().checkVersion( - EstablishConnectionFactory.from( - CliCommand.getUsersRegistry(cmdLine), - cmdLine.getOptionValue("auth")), + EstablishConnectionFactory.from(cfg[0], cfg[1]), Arrays.asList(gov.nasa.pds.registry.common.Version.instance(), Version.instance().subcommand(this.cmdname))); } @@ -302,7 +301,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..233a121c 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,52 @@ package gov.nasa.pds.registry.mgr.cmd; +import java.io.File; import org.apache.commons.cli.CommandLine; +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 { /** - * 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")) { + System.err.println("[WARN] -registry is deprecated; use -c instead"); + if (cmdLine.hasOption("auth")) { + System.err.println("[WARN] -auth is deprecated; use -c instead"); + } + return new String[]{cmdLine.getOptionValue("registry"), cmdLine.getOptionValue("auth")}; + } + throw new RuntimeException("Must provide -c on the command line. See usage for more details."); + } + + /** @deprecated Use getConfigPair(cmdLine)[0] or getConfigPair(cmdLine) instead. */ + @Deprecated + public static String getUsersRegistry(CommandLine cmdLine) { + try { + return getConfigPair(cmdLine)[0]; + } catch (RuntimeException ex) { + throw ex; + } catch (Exception ex) { + throw new RuntimeException(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..5e9340e9 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,9 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); - + String[] cfg = CliCommand.getConfigPair(cmdLine); + String esUrl = cfg[0]; + String authPath = cfg[1]; 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..c751a836 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,9 @@ 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[] cfg = CliCommand.getConfigPair(cmdLine); + String esUrl = cfg[0]; + String authPath = cfg[1]; 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..7632f3e1 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,10 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); - + String[] cfg = CliCommand.getConfigPair(cmdLine); + String esUrl = cfg[0]; + String authPath = cfg[1]; + // 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..c4497e7d 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,10 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); - + String[] cfg = CliCommand.getConfigPair(cmdLine); + String esUrl = cfg[0]; + String authPath = cfg[1]; + 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..c8a8316e 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,9 @@ public void run(CommandLine cmdLine) throws Exception printHelp(); return; } - String url = CliCommand.getUsersRegistry(cmdLine); - String authFile = cmdLine.getOptionValue("auth"); + String[] cfg = CliCommand.getConfigPair(cmdLine); + String url = cfg[0]; + String authFile = cfg[1]; 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..8658982e 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,9 @@ public void run(CommandLine cmdLine) throws Exception { printHelp(); return; } - String url = CliCommand.getUsersRegistry(cmdLine); - String authFile = cmdLine.getOptionValue("auth"); + String[] cfg = CliCommand.getConfigPair(cmdLine); + String url = cfg[0]; + String authFile = cfg[1]; 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..ef5c0cf7 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,9 @@ public void run(CommandLine cmdLine) throws Exception return; } - esUrl = CliCommand.getUsersRegistry(cmdLine); - authPath = cmdLine.getOptionValue("auth"); + String[] cfg = CliCommand.getConfigPair(cmdLine); + esUrl = cfg[0]; + authPath = cfg[1]; 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..fe287815 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,9 @@ 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[] cfg = CliCommand.getConfigPair(cmdLine); + String esUrl = cfg[0]; + String authPath = cfg[1]; 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..7dc18f52 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,13 @@ public void run(CommandLine cmdLine) throws Exception return; } - String authFile = cmdLine.getOptionValue("auth"); + String[] cfg = CliCommand.getConfigPair(cmdLine); + String url = cfg[0]; + String authFile = cfg[1]; 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..42d2a5ed 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,9 @@ public void run(CommandLine cmdLine) throws Exception return; } - this.url = CliCommand.getUsersRegistry(cmdLine); - this.authFile = cmdLine.getOptionValue("auth"); + String[] cfg = CliCommand.getConfigPair(cmdLine); + this.url = cfg[0]; + this.authFile = cfg[1]; 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..9a08f39c 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,10 @@ public void run(CommandLine cmdLine) throws Exception return; } - esUrl = CliCommand.getUsersRegistry(cmdLine); - authPath = cmdLine.getOptionValue("auth"); - + String[] cfg = CliCommand.getConfigPair(cmdLine); + esUrl = cfg[0]; + authPath = cfg[1]; + 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..da0499b0 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,9 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); + String[] cfg = CliCommand.getConfigPair(cmdLine); + String esUrl = cfg[0]; + String authPath = cfg[1]; 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..b3b40251 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,9 @@ public void run(CommandLine cmdLine) throws Exception return; } - String esUrl = CliCommand.getUsersRegistry(cmdLine); - String authPath = cmdLine.getOptionValue("auth"); + String[] cfg = CliCommand.getConfigPair(cmdLine); + String esUrl = cfg[0]; + String authPath = cfg[1]; 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..e565beb4 --- /dev/null +++ b/manager/src/main/java/gov/nasa/pds/registry/mgr/util/HarvestConfigReader.java @@ -0,0 +1,33 @@ +package gov.nasa.pds.registry.mgr.util; + +import java.io.File; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * Reads the <registry> element from a Harvest configuration XML file, + * returning the registry connection URL and optional auth file path. + */ +public class 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 Exception { + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configFile); + NodeList nodes = doc.getElementsByTagName("registry"); + if (nodes.getLength() == 0) { + throw new Exception("No element found in: " + configFile.getAbsolutePath()); + } + Element regEl = (Element) nodes.item(0); + String url = regEl.getTextContent().trim(); + if (url.isEmpty()) { + throw new Exception(" element has no URL value in: " + configFile.getAbsolutePath()); + } + String auth = regEl.getAttribute("auth").trim(); + return new String[]{url, auth.isEmpty() ? null : auth}; + } +} From bf65d185e19cd4591d2d146d593ffe40b3cbb21a Mon Sep 17 00:00:00 2001 From: Jordan Padams Date: Sat, 16 May 2026 23:26:30 -0700 Subject: [PATCH 2/2] Fix SonarCloud issues on PR #63 - HarvestConfigReader: add private constructor (S1118), narrow throws to IOException, wrap ParserConfigurationException/SAXException (S112) - CliCommand: replace System.err with LOG.warn (S106), throw IllegalArgumentException instead of RuntimeException (S112), add @Deprecated(since="1.3.0") (S6355), add getRegistryUrl/getAuthFile helpers to eliminate cfg[0]/cfg[1] duplication across 14 command files - RegistryManagerCli: NOSONAR on new System.out lines in printHelp (S106) - All 14 command files: use getRegistryUrl/getAuthFile instead of 3-line getConfigPair array dereference pattern (resolves duplication) Co-Authored-By: Claude Sonnet 4 --- .../pds/registry/mgr/RegistryManagerCli.java | 9 +++-- .../nasa/pds/registry/mgr/cmd/CliCommand.java | 26 +++++++++++---- .../registry/mgr/cmd/data/DeleteDataCmd.java | 5 ++- .../registry/mgr/cmd/data/ExportDataCmd.java | 5 ++- .../registry/mgr/cmd/data/ExportFileCmd.java | 5 ++- .../mgr/cmd/data/SetArchiveStatusCmd.java | 5 ++- .../mgr/cmd/data/UpdateAltIdsCmd.java | 5 ++- .../mgr/cmd/data/UpdateToolVersionCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/DeleteDDCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/ExportDDCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/ListDDCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/LoadDDCmd.java | 5 ++- .../pds/registry/mgr/cmd/dd/UpgradeDDCmd.java | 5 ++- .../mgr/cmd/reg/CreateRegistryCmd.java | 5 ++- .../mgr/cmd/reg/DeleteRegistryCmd.java | 5 ++- .../mgr/util/HarvestConfigReader.java | 33 ++++++++++++------- 16 files changed, 71 insertions(+), 62 deletions(-) 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 d438d099..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,11 +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."); + 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):"); + 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"); + 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:"); @@ -183,9 +183,8 @@ private boolean checkVersion() throws Exception { if (this.commands.get("create-registry") == this.command) { return true; } - String[] cfg = CliCommand.getConfigPair(cmdLine); return Version.instance().checkVersion( - EstablishConnectionFactory.from(cfg[0], cfg[1]), + EstablishConnectionFactory.from(CliCommand.getRegistryUrl(cmdLine), CliCommand.getAuthFile(cmdLine)), Arrays.asList(gov.nasa.pds.registry.common.Version.instance(), Version.instance().subcommand(this.cmdname))); } 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 233a121c..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 @@ -2,6 +2,8 @@ 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; /** @@ -13,6 +15,8 @@ */ public interface CliCommand { + Logger LOG = LogManager.getLogger(CliCommand.class); + /** * Run CLI command. * @param cmdLine Command line parameters. @@ -29,24 +33,34 @@ public static String[] getConfigPair(CommandLine cmdLine) throws Exception { return HarvestConfigReader.readRegistryAndAuth(new File(cmdLine.getOptionValue("c"))); } if (cmdLine.hasOption("registry")) { - System.err.println("[WARN] -registry is deprecated; use -c instead"); + LOG.warn("-registry is deprecated; use -c instead"); if (cmdLine.hasOption("auth")) { - System.err.println("[WARN] -auth is deprecated; use -c instead"); + LOG.warn("-auth is deprecated; use -c instead"); } return new String[]{cmdLine.getOptionValue("registry"), cmdLine.getOptionValue("auth")}; } - throw new RuntimeException("Must provide -c on the command line. See usage for more details."); + 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 getConfigPair(cmdLine)[0] or getConfigPair(cmdLine) instead. */ - @Deprecated + /** @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 RuntimeException(ex.getMessage(), 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 5e9340e9..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[] cfg = CliCommand.getConfigPair(cmdLine); - String esUrl = cfg[0]; - String authPath = cfg[1]; + 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 c751a836..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,9 +46,8 @@ public void run(CommandLine cmdLine) throws Exception throw new Exception("Missing required parameter '-file'"); } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String esUrl = cfg[0]; - String authPath = cfg[1]; + 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 7632f3e1..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,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String esUrl = cfg[0]; - String authPath = cfg[1]; + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); // Lidvid String lidvid = cmdLine.getOptionValue("lidvid"); 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 c4497e7d..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,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String esUrl = cfg[0]; - String authPath = cfg[1]; + String esUrl = CliCommand.getRegistryUrl(cmdLine); + String authPath = CliCommand.getAuthFile(cmdLine); String status = getStatus(cmdLine); 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 c8a8316e..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,9 +47,8 @@ public void run(CommandLine cmdLine) throws Exception printHelp(); return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String url = cfg[0]; - String authFile = cfg[1]; + 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 8658982e..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,9 +16,8 @@ public void run(CommandLine cmdLine) throws Exception { printHelp(); return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String url = cfg[0]; - String authFile = cfg[1]; + 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 ef5c0cf7..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,9 +39,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - esUrl = cfg[0]; - authPath = cfg[1]; + 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 fe287815..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,9 +39,8 @@ public void run(CommandLine cmdLine) throws Exception throw new Exception("Missing required parameter '-file'"); } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String esUrl = cfg[0]; - String authPath = cfg[1]; + 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 7dc18f52..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,9 +34,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String url = cfg[0]; - String authFile = cfg[1]; + String url = CliCommand.getRegistryUrl(cmdLine); + String authFile = CliCommand.getAuthFile(cmdLine); String namespace = cmdLine.getOptionValue("ns"); if (namespace == null || namespace.isBlank()) { 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 42d2a5ed..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,9 +65,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - this.url = cfg[0]; - this.authFile = cfg[1]; + 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 9a08f39c..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,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - esUrl = cfg[0]; - authPath = cfg[1]; + esUrl = CliCommand.getRegistryUrl(cmdLine); + authPath = CliCommand.getAuthFile(cmdLine); boolean replace= cmdLine.hasOption("r"); 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 da0499b0..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,9 +39,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String esUrl = cfg[0]; - String authPath = cfg[1]; + 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 b3b40251..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,9 +34,8 @@ public void run(CommandLine cmdLine) throws Exception return; } - String[] cfg = CliCommand.getConfigPair(cmdLine); - String esUrl = cfg[0]; - String authPath = cfg[1]; + 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 index e565beb4..7edbc075 100644 --- 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 @@ -1,10 +1,13 @@ 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, @@ -12,22 +15,28 @@ */ 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 Exception { - Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configFile); - NodeList nodes = doc.getElementsByTagName("registry"); - if (nodes.getLength() == 0) { - throw new Exception("No element found in: " + configFile.getAbsolutePath()); - } - Element regEl = (Element) nodes.item(0); - String url = regEl.getTextContent().trim(); - if (url.isEmpty()) { - throw new Exception(" element has no URL value in: " + configFile.getAbsolutePath()); + 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); } - String auth = regEl.getAttribute("auth").trim(); - return new String[]{url, auth.isEmpty() ? null : auth}; } }