Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ public static void printHelp()

System.out.println();
System.out.println("Global required parameters:");
System.out.println(" -c <file> 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 <file> Authentication config file");
System.out.println(" -registry <url> 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 <url> 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:");
Expand Down Expand Up @@ -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)));
}

Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <harvest-config.xml> instead");
if (cmdLine.hasOption("auth")) {
LOG.warn("-auth is deprecated; use -c <harvest-config.xml> instead");
}
return new String[]{cmdLine.getOptionValue("registry"), cmdLine.getOptionValue("auth")};
}
throw new IllegalArgumentException("Must provide -c <harvest-config.xml> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public class DeleteDataCmd implements CliCommand
{
private static final Logger log = LogManager.getLogger(DeleteDataCmd.class);

Check failure on line 23 in manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/DeleteDataCmd.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename field "log" to prevent any misunderstanding/clash with field "LOG" defined in interface "gov.nasa.pds.registry.mgr.cmd.CliCommand".

See more on https://sonarcloud.io/project/issues?id=NASA-PDS_registry-loader&issues=AZ40nlBobrWrxHmecTzs&open=AZ40nlBobrWrxHmecTzs&pullRequest=63

/**
* Constructor
Expand All @@ -39,9 +39,8 @@
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @author karpenko
*/
public class SetArchiveStatusCmd implements CliCommand {
private static final Logger log = LogManager.getLogger(SetArchiveStatusCmd.class);

Check failure on line 29 in manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename field "log" to prevent any misunderstanding/clash with field "LOG" defined in interface "gov.nasa.pds.registry.mgr.cmd.CliCommand".

See more on https://sonarcloud.io/project/issues?id=NASA-PDS_registry-loader&issues=AZ40nlDFbrWrxHmecTzt&open=AZ40nlDFbrWrxHmecTzt&pullRequest=63
private ArchiveStatus archiveStatusUtils = new ArchiveStatus();
/**
* Constructor
Expand All @@ -43,9 +43,9 @@
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
public class UpdateAltIdsCmd implements CliCommand
{
private Logger log;

Check failure on line 31 in manager/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/UpdateAltIdsCmd.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename field "log" to prevent any misunderstanding/clash with field "LOG" defined in interface "gov.nasa.pds.registry.mgr.cmd.CliCommand".

See more on https://sonarcloud.io/project/issues?id=NASA-PDS_registry-loader&issues=AZ40nk_QbrWrxHmecTzr&open=AZ40nk_QbrWrxHmecTzr&pullRequest=63

/**
* Constructor
Expand All @@ -47,8 +47,8 @@
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 &lt;registry&gt; 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 <registry> element found in: " + configFile.getAbsolutePath());
}
Element regEl = (Element) nodes.item(0);
String url = regEl.getTextContent().trim();
if (url.isEmpty()) {
throw new IOException("<registry> 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);
}
}
}
Loading