Skip to content

Commit

Permalink
SOLR-17429, SOLR-17338: Use stderr for SolrCLI deprecation logs (apac…
Browse files Browse the repository at this point in the history
…he#2679)

(cherry picked from commit 26d00c3)
  • Loading branch information
HoustonPutman authored and patsonluk committed Jan 3, 2025
1 parent 0bb1ad4 commit dd0ca8f
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions solr/core/src/java/org/apache/solr/cli/SolrCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ public static String getOptionWithDeprecatedAndDefault(
return val == null ? def : val;
}

// TODO: SOLR-17429 - remove the custom logic when CommonsCLI is upgraded and
// makes stderr the default, or makes Option.toDeprecatedString() public.
private static void deprecatedHandlerStdErr(Option o) {
if (o.isDeprecated()) {
final StringBuilder buf =
new StringBuilder().append("Option '-").append(o.getOpt()).append('\'');
if (o.getLongOpt() != null) {
buf.append(",'--").append(o.getLongOpt()).append('\'');
}
buf.append(": ").append(o.getDeprecated());
CLIO.err(buf.toString());
}
}

/** Parses the command-line arguments passed by the user. */
public static CommandLine processCommandLineArgs(Tool tool, String[] args) {
List<Option> customOptions = tool.getOptions();
Expand All @@ -379,7 +393,11 @@ public static CommandLine processCommandLineArgs(Tool tool, String[] args) {

CommandLine cli = null;
try {
cli = (new DefaultParser()).parse(options, args);
cli =
DefaultParser.builder()
.setDeprecatedHandler(SolrCLI::deprecatedHandlerStdErr)
.build()
.parse(options, args);
} catch (ParseException exp) {
// Check if we passed in a help argument with a non parsing set of arguments.
boolean hasHelpArg = false;
Expand Down Expand Up @@ -614,12 +632,14 @@ public static String normalizeSolrUrl(
if (urlPath.contains(hostContext)) {
String newSolrUrl =
uri.resolve(urlPath.substring(0, urlPath.indexOf(hostContext)) + "/").toString();
CLIO.out(
"WARNING: URLs provided to this tool needn't include Solr's context-root (e.g. \"/solr\"). Such URLs are deprecated and support for them will be removed in a future release. Correcting from ["
+ solrUrl
+ "] to ["
+ newSolrUrl
+ "].");
if (logUrlFormatWarning) {
CLIO.err(
"WARNING: URLs provided to this tool needn't include Solr's context-root (e.g. \"/solr\"). Such URLs are deprecated and support for them will be removed in a future release. Correcting from ["
+ solrUrl
+ "] to ["
+ newSolrUrl
+ "].");
}
solrUrl = newSolrUrl;
}
if (solrUrl.endsWith("/")) {
Expand All @@ -641,13 +661,11 @@ public static String normalizeSolrUrl(CommandLine cli) throws Exception {
cli.hasOption("zk-host") ? cli.getOptionValue("zk-host") : cli.getOptionValue("zkHost");
if (zkHost == null) {
solrUrl = SolrCLI.getDefaultSolrUrl();
CLIO.getOutStream()
.println(
"Neither --zk-host or --solr-url parameters provided so assuming solr url is "
+ solrUrl
+ ".");
CLIO.err(
"Neither --zk-host or --solr-url parameters provided so assuming solr url is "
+ solrUrl
+ ".");
} else {

try (CloudSolrClient cloudSolrClient = getCloudHttp2SolrClient(zkHost)) {
cloudSolrClient.connect();
Set<String> liveNodes = cloudSolrClient.getClusterState().getLiveNodes();
Expand Down

0 comments on commit dd0ca8f

Please sign in to comment.