Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tools] improve bkctl help message #1793

Merged
merged 3 commits into from
Nov 10, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.bookkeeper.tools.cli.commands;

import static org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_INFRA_SERVICE;

import org.apache.bookkeeper.tools.cli.BKCtl;
import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
import org.apache.bookkeeper.tools.common.BKFlags;
Expand All @@ -36,6 +38,7 @@ public class BookieCommandGroup extends CliCommandGroup<BKFlags> {
.withName(NAME)
.withDescription(DESC)
.withParent(BKCtl.NAME)
.withCategory(CATEGORY_INFRA_SERVICE)
.addCommand(new LastMarkCommand())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.bookkeeper.tools.cli.commands;

import static org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_INFRA_SERVICE;

import org.apache.bookkeeper.tools.cli.BKCtl;
import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand;
import org.apache.bookkeeper.tools.common.BKFlags;
Expand All @@ -36,6 +38,7 @@ public class BookiesCommandGroup extends CliCommandGroup<BKFlags> {
.withName(NAME)
.withDescription(DESC)
.withParent(BKCtl.NAME)
.withCategory(CATEGORY_INFRA_SERVICE)
.addCommand(new ListBookiesCommand())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.bookkeeper.tools.cli.commands;

import static org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_LEDGER_SERVICE;

import org.apache.bookkeeper.tools.cli.BKCtl;
import org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand;
import org.apache.bookkeeper.tools.common.BKFlags;
Expand All @@ -36,6 +38,7 @@ public class LedgerCommandGroup extends CliCommandGroup<BKFlags> {
.withName(NAME)
.withDescription(DESC)
.withParent(BKCtl.NAME)
.withCategory(CATEGORY_LEDGER_SERVICE)
.addCommand(new SimpleTestCommand())
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.bookkeeper.tools.common;

/**
* Classes to keep a list of command categories.
*/
public final class BKCommandCategories {

// commands that operate cluster and nodes
public static final String CATEGORY_INFRA_SERVICE = "Infrastructure commands";

// commands that operate ledger service
public static final String CATEGORY_LEDGER_SERVICE = "Ledger service commands";

// commands that operate table service
public static final String CATEGORY_TABLE_SERVICE = "Table service commands";

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public void setParent(String parent) {
.build();
}

@Override
public String category() {
return spec.category();
}

@Override
public String name() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ public static class Builder<CliFlagsT extends CliFlags> {
private PrintStream console = System.out;
private boolean isCommandGroup = false;
private String argumentsUsage = "";
private String category = "";

private Builder() {}

private Builder(CliSpec<CliFlagsT> spec) {
this.category = spec.category;
this.name = spec.name;
this.parent = spec.parent;
this.usage = spec.usage;
Expand All @@ -85,6 +87,11 @@ private Builder(CliSpec<CliFlagsT> spec) {
this.isCommandGroup = spec.isCommandGroup;
}

public Builder<CliFlagsT> withCategory(String category) {
this.category = category;
return this;
}

public Builder<CliFlagsT> withName(String name) {
this.name = name;
return this;
Expand Down Expand Up @@ -142,6 +149,7 @@ public Builder<CliFlagsT> setCommandGroup(boolean enabled) {

public CliSpec<CliFlagsT> build() {
return new CliSpec<>(
category,
name,
parent,
usage,
Expand All @@ -158,6 +166,7 @@ public CliSpec<CliFlagsT> build() {

}

private final String category;
private final String name;
private final String parent;
private final String usage;
Expand All @@ -171,7 +180,8 @@ public CliSpec<CliFlagsT> build() {
// whether the cli spec is for a command group.
private final boolean isCommandGroup;

private CliSpec(String name,
private CliSpec(String category,
String name,
String parent,
String usage,
String argumentsUsage,
Expand All @@ -182,6 +192,7 @@ private CliSpec(String name,
Function<CliFlagsT, Boolean> runFunc,
PrintStream console,
boolean isCommandGroup) {
this.category = category;
this.name = name;
this.parent = parent;
this.usage = usage;
Expand All @@ -195,6 +206,10 @@ private CliSpec(String name,
this.isCommandGroup = isCommandGroup;
}

public String category() {
return category;
}

public String name() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ default boolean hidden() {
return false;
}

/**
* Return category of this command belongs to.
*
* @return category name
*/
default String category() {
return "";
}

/**
* Return command name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.IntStream;
import org.apache.commons.lang.StringUtils;

/**
* Utils to process a commander.
Expand Down Expand Up @@ -164,27 +166,51 @@ public static void printAvailableCommands(Map<String, Command> commands,
return;
}

printer.println("Commands:");
printer.println();

int longestCommandName = commands
.keySet()
.stream()
.mapToInt(name -> name.length())
.max()
.orElse(0);

// group the commands by category
Map<String, Map<String, Command>> categorizedCommands = new TreeMap<>();
for (Map.Entry<String, Command> commandEntry : commands.entrySet()) {
if ("help".equals(commandEntry.getKey())) {
// don't print help message along with available other commands
// don't add help message along with other commands
continue;
}
printCommand(printer, commandEntry.getKey(), commandEntry.getValue(), longestCommandName);
String category = commandEntry.getValue().category();
Map<String, Command> subCommands = categorizedCommands.get(category);
if (null == subCommands) {
subCommands = new TreeMap<>();
categorizedCommands.put(category, subCommands);
}
subCommands.put(commandEntry.getKey(), commandEntry.getValue());
}

// there is only one category, print all of them under `Commands`.
if (categorizedCommands.size() <= 1) {
printer.println("Commands:");
printer.println();
}

for (Map.Entry<String, Map<String, Command>> categoryEntry : categorizedCommands.entrySet()) {
String category = categoryEntry.getKey();
printCategoryHeader(printer, category);
Map<String, Command> subCommands = categoryEntry.getValue();
for (Map.Entry<String, Command> commandEntry : subCommands.entrySet()) {
printCommand(printer, commandEntry.getKey(), commandEntry.getValue(), longestCommandName);
}
printer.println();
}

Command helpCmd = commands.get("help");
if (null != helpCmd) {
printer.println();
if (categorizedCommands.size() > 1) {
// if commands has been categorized, put help
printCategoryHeader(printer, "Other commands");
}
printCommand(printer, "help", helpCmd, longestCommandName);
}

Expand Down Expand Up @@ -215,4 +241,19 @@ private static void printCommand(PrintStream printer,
command.description());
}

private static void printCategoryHeader(PrintStream printer,
String category) {
if (StringUtils.isEmpty(category)) {
return;
}

printIndent(printer, 0);
printDescription(
printer,
0,
0,
category + " :");
printer.println();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.bookkeeper.stream.cli;

import static org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_INFRA_SERVICE;

import org.apache.bookkeeper.stream.cli.commands.cluster.InitClusterCommand;
import org.apache.bookkeeper.tools.common.BKFlags;
import org.apache.bookkeeper.tools.framework.CliCommandGroup;
Expand All @@ -34,6 +36,7 @@ public class ClusterCommandGroup extends CliCommandGroup<BKFlags> {
.withName(NAME)
.withDescription(DESC)
.withParent("bkctl")
.withCategory(CATEGORY_INFRA_SERVICE)
.addCommand(new InitClusterCommand())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.bookkeeper.stream.cli;

import static org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_TABLE_SERVICE;

import org.apache.bookkeeper.stream.cli.commands.namespace.CreateNamespaceCommand;
import org.apache.bookkeeper.stream.cli.commands.namespace.DeleteNamespaceCommand;
import org.apache.bookkeeper.stream.cli.commands.namespace.GetNamespaceCommand;
Expand All @@ -36,6 +38,7 @@ public class NamespaceCommandGroup extends CliCommandGroup<BKFlags> {
.withName(NAME)
.withDescription(DESC)
.withParent("bkctl")
.withCategory(CATEGORY_TABLE_SERVICE)
.addCommand(new CreateNamespaceCommand())
.addCommand(new GetNamespaceCommand())
.addCommand(new DeleteNamespaceCommand())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.bookkeeper.stream.cli;

import static org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_TABLE_SERVICE;

import org.apache.bookkeeper.stream.cli.commands.table.CreateTableCommand;
import org.apache.bookkeeper.stream.cli.commands.table.DeleteTableCommand;
import org.apache.bookkeeper.stream.cli.commands.table.GetTableCommand;
Expand All @@ -36,6 +38,7 @@ public class TableAdminCommandGroup extends CliCommandGroup<BKFlags> {
.withName(NAME)
.withDescription(DESC)
.withParent("bkctl")
.withCategory(CATEGORY_TABLE_SERVICE)
.addCommand(new CreateTableCommand())
.addCommand(new GetTableCommand())
.addCommand(new DeleteTableCommand())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.bookkeeper.stream.cli;

import static org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_TABLE_SERVICE;

import org.apache.bookkeeper.stream.cli.commands.table.DelCommand;
import org.apache.bookkeeper.stream.cli.commands.table.GetCommand;
import org.apache.bookkeeper.stream.cli.commands.table.IncrementCommand;
Expand All @@ -37,6 +39,7 @@ public class TableCommandGroup extends CliCommandGroup<BKFlags> {
.withName(NAME)
.withDescription(DESC)
.withParent("bkctl")
.withCategory(CATEGORY_TABLE_SERVICE)
.addCommand(new PutCommand())
.addCommand(new GetCommand())
.addCommand(new IncrementCommand())
Expand Down