forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Pulsar Client Tools]Support generate documentation of pulsar admin c…
…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
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdGenerateDocument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters