Skip to content

Commit

Permalink
[Pulsar Client Tools]Support generate documentation of pulsar admin c…
Browse files Browse the repository at this point in the history
…li automatically (apache#5738)

### Motivation

Currently, the commands on the page http://pulsar.apache.org/docs/en/pulsar-admin/ are all manually added. when the document changes, there are often wrong contents. in order to solve this problem, we expect the commands on this page to be automatically generated. this pr is the first step. If it passes, we will add a new page on the website to show the automatically generated commands later.
  • Loading branch information
tuteng authored and sijie committed Dec 20, 2019
1 parent 17e22d1 commit 805a421
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* 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.pulsar.admin.cli;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterDescription;
import com.beust.jcommander.Parameters;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;

import java.util.List;
import java.util.Map;
import java.util.Properties;

@Getter
@Parameters(commandDescription = "Generate documents automatically.")
@Slf4j
public class CmdGenerateDocument extends CmdBase {

private final JCommander baseJcommander;

private PulsarAdminTool tool;

public CmdGenerateDocument(PulsarAdmin admin) {
super("documents", admin);
baseJcommander = new JCommander();
try {
tool = new PulsarAdminTool(new Properties());
} catch (Exception e) {
System.err.println(e.getMessage());
System.err.println();
baseJcommander.usage();
return;
}
for (Map.Entry<String, Class<?>> c : tool.commandMap.entrySet()) {
try {
if (!c.getKey().equals("documents")) {
baseJcommander.addCommand(
c.getKey(), c.getValue().getConstructor(PulsarAdmin.class).newInstance(admin));
}
} catch (Exception e) {
System.err.println(e.getMessage());
System.err.println();
baseJcommander.usage();
return;
}
}
jcommander.addCommand("generate", new GenerateDocument());
}

@Parameters(commandDescription = "Generate document for modules")
private class GenerateDocument extends CliCommand {

@Parameter(description = "Please specify the module name, if not, documents will be generated for all modules." +
"Optional modules(clusters, tenants, brokers, broker-stats, namespaces, topics, schemas, bookies," +
"functions, ns-isolation-policy, resource-quotas, functions, sources, sinks)")
private java.util.List<String> modules;

@Override
void run() throws PulsarAdminException {
StringBuilder sb = new StringBuilder();
if (modules == null || modules.isEmpty()) {
baseJcommander.getCommands().forEach((k, v) ->
this.generateDocument(sb, k, v)
);
} else {
String module = getOneArgument(modules);
JCommander obj = baseJcommander.getCommands().get(module);
this.generateDocument(sb, module, obj);
}
}

private void generateDocument(StringBuilder sb, String module, JCommander obj) {
sb.append("------------\n\n");
sb.append("# ").append(module).append("\n\n");
sb.append("### Usage\n\n");
sb.append("`$").append(module).append("`\n\n");
sb.append("------------\n\n");
sb.append(baseJcommander.getCommandDescription(module)).append("\n");
sb.append("\n\n```bdocs-tab:example_shell\n")
.append("$ pulsar-admin ").append(module).append(" subcommand")
.append("\n```");
sb.append("\n\n");
CmdBase cmdObj = (CmdBase) obj.getObjects().get(0);
for (String s : cmdObj.jcommander.getCommands().keySet()) {
sb.append("* `").append(s).append("`\n");
}
cmdObj.jcommander.getCommands().forEach((subK, subV) -> {
sb.append("\n\n## ").append(subK).append("\n\n");
sb.append(cmdObj.jcommander.getCommandDescription(subK)).append("\n\n");
sb.append("### Usage\n\n");
sb.append("------------\n\n\n");
sb.append("```bdocs-tab:example_shell\n$ pulsar-admin ").append(module).append(" ")
.append(subK).append(" options").append("\n```\n\n");
List<ParameterDescription> options = cmdObj.jcommander.getCommands().get(subK).getParameters();
if (options.size() > 0) {
sb.append("Options\n\n\n");
sb.append("|Flag|Description|Default|\n");
sb.append("|---|---|---|\n");
}
options.forEach((option) ->
sb.append("| `").append(option.getNames())
.append("` | ").append(option.getDescription().replace("\n", " "))
.append("|").append(option.getDefault()).append("|\n")
);
});
System.out.println(sb.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public class PulsarAdminTool {
commandMap.put("sources", CmdSources.class);
commandMap.put("sinks", CmdSinks.class);

// Automatically generate documents for pulsar-admin
commandMap.put("documents", CmdGenerateDocument.class);

// To remain backwards compatibility for "source" and "sink" commands
// TODO eventually remove this
commandMap.put("source", CmdSources.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,31 @@ public void testListNonPersistentTopicsCmd() throws Exception {
}
}

@Test
public void testGenerateDocForModule() throws Exception {
String[] moduleNames = {
"clusters",
"tenants",
"brokers",
"broker-stats",
"namespaces",
"topics",
"schemas",
"bookies",
"functions",
"ns-isolation-policy",
"resource-quotas",
"functions",
"sources",
"sinks"
};
BrokerContainer container = pulsarCluster.getAnyBroker();
for (int i = 0; i < moduleNames.length; i++) {
ContainerExecResult result = container.execCmd(
PulsarCluster.ADMIN_SCRIPT,
"documents", "generate", moduleNames[i]);
Assert.assertTrue(result.getStdout().contains("------------\n\n# " + moduleNames[i]));
}
}

}

0 comments on commit 805a421

Please sign in to comment.