All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.pulsar.admin.cli.CmdGenerateDocument Maven / Gradle / Ivy

There is a newer version: 4.0.0-SNAPSHOT.ursa
Show newest version
/**
 * 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.DefaultUsageFormatter;
import com.beust.jcommander.IUsageFormatter;
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;
import java.util.function.Supplier;

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

    private final JCommander baseJcommander;
    private final IUsageFormatter usageFormatter;

    private PulsarAdminTool tool;

    public CmdGenerateDocument(Supplier admin) {
        super("documents", admin);
        baseJcommander = new JCommander();
        usageFormatter = new DefaultUsageFormatter(baseJcommander);
        try {
            tool = new PulsarAdminTool(new Properties());
        } catch (Exception e) {
            System.err.println(e.getMessage());
            System.err.println();
            baseJcommander.usage();
            return;
        }
        for (Map.Entry> c : tool.commandMap.entrySet()) {
            try {
                if (!c.getKey().equals("documents")) {
                    baseJcommander.addCommand(
                            c.getKey(), c.getValue().getConstructor(Supplier.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 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(usageFormatter.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.getUsageFormatter().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 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());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy