Skip to content
Merged
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
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 @@ -23,3 +23,5 @@ links:
url: https://issues.apache.org/jira/browse/SOLR-18513
- name: SOLR-18514
url: https://issues.apache.org/jira/browse/SOLR-18514
- name: SOLR-18516
url: https://issues.apache.org/jira/browse/SOLR-18516
63 changes: 62 additions & 1 deletion solr/core/src/java/org/apache/solr/cli/PostLogsTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.jetty.HttpJettySolrClient;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.common.SolrInputDocument;
Expand All @@ -45,8 +46,23 @@
import org.apache.solr.handler.component.ShardRequest;

/** A command line tool for indexing Solr logs in the out-of-the-box log format. */
@SuppressWarnings("UnnecessarilyFullyQualified")
@picocli.CommandLine.Command(
name = "postlogs",
description = "Indexes Solr logs in the out-of-the-box log format.",
footerHeading = "%nExamples:%n",
footer = {
" # Index all logs found under a directory",
" bin/solr postlogs -c gettingstarted --rootdir /var/solr/logs --solr-url"
+ " http://localhost:8983"
})
public class PostLogsTool 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 @@ -56,6 +72,11 @@ public class PostLogsTool extends ToolBase {
.desc("Name of the collection.")
.get();

/**
* @deprecated Only used by the commons-cli parser; the picocli path declares this as an annotated
* field.
*/
@Deprecated
private static final Option ROOT_DIR_OPTION =
Option.builder()
.longOpt("rootdir")
Expand All @@ -68,6 +89,33 @@ public class PostLogsTool extends ToolBase {
/** Parameters for the postlogs command, independent of the command line parser. */
record PostLogsParams(String url, String rootDir, String credentials) {}

// --- picocli fields ---
// The connection group is mandatory (multiplicity "1"), mirroring the commons-cli path's
// manual IllegalArgumentException when no connection target is given.

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

@picocli.CommandLine.Mixin private CredentialsOptions credentialsOptions;

@picocli.CommandLine.Option(
names = {"-c", "--name"},
required = true,
paramLabel = "NAME",
description = "Name of the collection.")
private String nameOpt;

@picocli.CommandLine.Option(
names = "--rootdir",
required = true,
paramLabel = "DIRECTORY",
description = "All files found at or below the root directory will be indexed.")
private String rootDirOpt;

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

public PostLogsTool(ToolRuntime runtime) {
super(runtime);
}
Expand Down Expand Up @@ -639,6 +687,19 @@ public static String[] getRequestPurposeNames(Integer reqPurpose) {

@Override
public int callTool() throws Exception {
throw new UnsupportedOperationException("This tool does not yet support PicoCli");
String url = resolveSolrUrl(credentialsOptions.credentials) + "/solr/" + nameOpt;
PostLogsParams params = new PostLogsParams(url, rootDirOpt, credentialsOptions.credentials);
runCommand(params);
return 0;
}

private String resolveSolrUrl(String credentials) throws Exception {
String solrUrlArg = connectionOptions.effectiveSolrUrl();
if (solrUrlArg != null) {
return CLIUtils.normalizeSolrUrl(solrUrlArg);
}
return CLIUtils.solrUrlFromConnection(
CloudSolrClient.CloudSolrClientConnection.parse(connectionOptions.effectiveZkHost()),
credentials);
}
}
Loading
Loading