Skip to content
Merged
2 changes: 2 additions & 0 deletions changelog/unreleased/SOLR-17697-picocli-experimental-cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ links:
url: https://issues.apache.org/jira/browse/SOLR-18516
- name: SOLR-18519
url: https://issues.apache.org/jira/browse/SOLR-18519
- name: SOLR-18518
url: https://issues.apache.org/jira/browse/SOLR-18518
42 changes: 42 additions & 0 deletions solr/core/src/java/org/apache/solr/cli/CLIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,48 @@ public static String normalizeSolrUrl(CommandLine cli) throws Exception {
return solrUrl;
}

/**
* The picocli counterpart of {@link #normalizeSolrUrl(CommandLine)}: {@code --solr-url}, else the
* connection named by {@code --solr-connection} or {@code --zk-host} (or the matching property
* when the option is absent), else the default URL with the same warning.
*
* @param connectionOptions the parsed connection group, or null if none of its options was given
*/
static String resolveSolrUrl(ConnectionOptions connectionOptions, String credentials)
throws Exception {
String solrUrl = connectionOptions != null ? connectionOptions.solrUrl : null;
if (solrUrl == null) {
String solrConnection =
(connectionOptions != null && connectionOptions.solrConnection != null)
? connectionOptions.solrConnection
: EnvUtils.getProperty("solr.connection");
String zkHost =
(connectionOptions != null && connectionOptions.zkHost != null)
? connectionOptions.zkHost
: EnvUtils.getProperty("zkHost");
if (solrConnection != null && !solrConnection.isBlank()) {
solrUrl =
solrUrlFromConnection(
CloudSolrClient.CloudSolrClientConnection.parse(solrConnection), credentials);
} else if (zkHost != null && !zkHost.isBlank()) {
var zkSolrConnection = CloudSolrClient.CloudSolrClientConnection.parse(zkHost);
if (!zkSolrConnection.isZookeeper()) {
throw new IOException(
String.format(
Locale.ROOT, "Expected ZooKeeper connection string, but got: '%s'.", zkHost));
}
solrUrl = solrUrlFromConnection(zkSolrConnection, credentials);
} else {
solrUrl = getDefaultSolrUrl();
CLIO.err(
"Neither --solr-connection, --zk-host or --solr-url parameters, nor SOLR_CONNECTION, ZK_HOST env var provided, so assuming solr url is "
+ solrUrl
+ ".");
}
}
return normalizeSolrUrl(solrUrl);
}

/**
* Resolves a base Solr URL from a parsed connection. The HTTP form (e.g. {@code -s
* http://host:port}) already names a Solr URL, so it is used directly without spinning up a
Expand Down
32 changes: 32 additions & 0 deletions solr/core/src/java/org/apache/solr/cli/CollectionNameOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.cli;

import picocli.CommandLine;

/**
* Picocli mixin for the {@code -c} / {@code --name} option naming the collection a command acts on.
*/
class CollectionNameOptions {

@CommandLine.Option(
names = {"-c", "--name"},
required = true,
paramLabel = "NAME",
description = "Name of the collection.")
String name;
}
42 changes: 42 additions & 0 deletions solr/core/src/java/org/apache/solr/cli/SnapshotCreateShim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.cli;

import org.apache.solr.logging.DeprecationLog;

/**
* The old top-level {@code snapshot-create} spelling of {@code bin/solr snapshot create}, kept so
* that existing scripts keep working. It is hidden from help and the reference guide.
*
* @deprecated Use {@code bin/solr snapshot create}; this spelling is removed in Solr 11.
*/
@Deprecated(since = "10.2")
@SuppressWarnings("UnnecessarilyFullyQualified")
@picocli.CommandLine.Command(
name = "snapshot-create",
hidden = true,
description = "Deprecated; use 'snapshot create'.")
public class SnapshotCreateShim extends SnapshotCreateTool {

@Override
public int callTool() throws Exception {
DeprecationLog.log(
"cli.snapshot-create",
"'bin/solr snapshot-create' is deprecated and will be removed in Solr 11; use 'bin/solr snapshot create'.");
return super.callTool();
}
}
43 changes: 42 additions & 1 deletion solr/core/src/java/org/apache/solr/cli/SnapshotCreateTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,22 @@
import org.apache.solr.client.solrj.response.CollectionAdminResponse;

/** Supports snapshot-create command in the bin/solr script. */
@SuppressWarnings("UnnecessarilyFullyQualified")
@picocli.CommandLine.Command(
name = "create",
description = "Creates a named snapshot of a collection.",
footerHeading = "%nExamples:%n",
footer = {
" # Create a snapshot",
" bin/solr snapshot create -c mycollection --snapshot-name snap1"
})
public class SnapshotCreateTool extends ToolBase {

/**
* @deprecated Only used by the commons-cli parser; the picocli path declares this as an annotated
* field.
*/
@Deprecated
private static final Option COLLECTION_NAME_OPTION =
Option.builder("c")
.longOpt("name")
Expand All @@ -35,6 +49,11 @@ public class SnapshotCreateTool extends ToolBase {
.desc("Name of collection to be snapshot.")
.get();

/**
* @deprecated Only used by the commons-cli parser; the picocli path declares this as an annotated
* field.
*/
@Deprecated
private static final Option SNAPSHOT_NAME_OPTION =
Option.builder()
.longOpt("snapshot-name")
Expand All @@ -48,6 +67,21 @@ public class SnapshotCreateTool extends ToolBase {
record SnapshotCreateParams(
String solrUrl, String credentials, String collectionName, String snapshotName) {}

// --- picocli fields ---

@picocli.CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
private ConnectionOptions connectionOptions;

@picocli.CommandLine.Mixin private CredentialsOptions credentialsOptions;

@picocli.CommandLine.Mixin private CollectionNameOptions collection;

@picocli.CommandLine.Mixin private SnapshotNameOptions snapshot;

public SnapshotCreateTool() {
this(new DefaultToolRuntime());
}

public SnapshotCreateTool(ToolRuntime runtime) {
super(runtime);
}
Expand Down Expand Up @@ -112,6 +146,13 @@ public void createSnapshot(SolrClient solrClient, String collectionName, String

@Override
public int callTool() throws Exception {
throw new UnsupportedOperationException("This tool does not yet support PicoCli");
SnapshotCreateParams params =
new SnapshotCreateParams(
CLIUtils.resolveSolrUrl(connectionOptions, credentialsOptions.credentials),
credentialsOptions.credentials,
collection.name,
snapshot.name);
createSnapshot(params);
return 0;
}
}
42 changes: 42 additions & 0 deletions solr/core/src/java/org/apache/solr/cli/SnapshotDeleteShim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.cli;

import org.apache.solr.logging.DeprecationLog;

/**
* The old top-level {@code snapshot-delete} spelling of {@code bin/solr snapshot delete}, kept so
* that existing scripts keep working. It is hidden from help and the reference guide.
*
* @deprecated Use {@code bin/solr snapshot delete}; this spelling is removed in Solr 11.
*/
@Deprecated(since = "10.2")
@SuppressWarnings("UnnecessarilyFullyQualified")
@picocli.CommandLine.Command(
name = "snapshot-delete",
hidden = true,
description = "Deprecated; use 'snapshot delete'.")
public class SnapshotDeleteShim extends SnapshotDeleteTool {

@Override
public int callTool() throws Exception {
DeprecationLog.log(
"cli.snapshot-delete",
"'bin/solr snapshot-delete' is deprecated and will be removed in Solr 11; use 'bin/solr snapshot delete'.");
return super.callTool();
}
}
43 changes: 42 additions & 1 deletion solr/core/src/java/org/apache/solr/cli/SnapshotDeleteTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,22 @@
import org.apache.solr.client.solrj.response.CollectionAdminResponse;

/** Supports snapshot-delete command in the bin/solr script. */
@SuppressWarnings("UnnecessarilyFullyQualified")
@picocli.CommandLine.Command(
name = "delete",
description = "Deletes a named snapshot of a collection.",
footerHeading = "%nExamples:%n",
footer = {
" # Delete a snapshot",
" bin/solr snapshot delete -c mycollection --snapshot-name snap1"
})
public class SnapshotDeleteTool extends ToolBase {

/**
* @deprecated Only used by the commons-cli parser; the picocli path declares this as an annotated
* field.
*/
@Deprecated
private static final Option COLLECTION_NAME_OPTION =
Option.builder("c")
.longOpt("name")
Expand All @@ -35,6 +49,11 @@ public class SnapshotDeleteTool extends ToolBase {
.desc("Name of collection to manage.")
.get();

/**
* @deprecated Only used by the commons-cli parser; the picocli path declares this as an annotated
* field.
*/
@Deprecated
private static final Option SNAPSHOT_NAME_OPTION =
Option.builder()
.longOpt("snapshot-name")
Expand All @@ -48,6 +67,21 @@ public class SnapshotDeleteTool extends ToolBase {
record SnapshotDeleteParams(
String solrUrl, String credentials, String collectionName, String snapshotName) {}

// --- picocli fields ---

@picocli.CommandLine.ArgGroup(exclusive = true, multiplicity = "0..1")
private ConnectionOptions connectionOptions;

@picocli.CommandLine.Mixin private CredentialsOptions credentialsOptions;

@picocli.CommandLine.Mixin private CollectionNameOptions collection;

@picocli.CommandLine.Mixin private SnapshotNameOptions snapshot;

public SnapshotDeleteTool() {
this(new DefaultToolRuntime());
}

public SnapshotDeleteTool(ToolRuntime runtime) {
super(runtime);
}
Expand Down Expand Up @@ -112,6 +146,13 @@ public void deleteSnapshot(SolrClient solrClient, String collectionName, String

@Override
public int callTool() throws Exception {
throw new UnsupportedOperationException("This tool does not yet support PicoCli");
SnapshotDeleteParams params =
new SnapshotDeleteParams(
CLIUtils.resolveSolrUrl(connectionOptions, credentialsOptions.credentials),
credentialsOptions.credentials,
collection.name,
snapshot.name);
deleteSnapshot(params);
return 0;
}
}
42 changes: 42 additions & 0 deletions solr/core/src/java/org/apache/solr/cli/SnapshotDescribeShim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.cli;

import org.apache.solr.logging.DeprecationLog;

/**
* The old top-level {@code snapshot-describe} spelling of {@code bin/solr snapshot describe}, kept
* so that existing scripts keep working. It is hidden from help and the reference guide.
*
* @deprecated Use {@code bin/solr snapshot describe}; this spelling is removed in Solr 11.
*/
@Deprecated(since = "10.2")
@SuppressWarnings("UnnecessarilyFullyQualified")
@picocli.CommandLine.Command(
name = "snapshot-describe",
hidden = true,
description = "Deprecated; use 'snapshot describe'.")
public class SnapshotDescribeShim extends SnapshotDescribeTool {

@Override
public int callTool() throws Exception {
DeprecationLog.log(
"cli.snapshot-describe",
"'bin/solr snapshot-describe' is deprecated and will be removed in Solr 11; use 'bin/solr snapshot describe'.");
return super.callTool();
}
}
Loading
Loading