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
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public void setName(String name) {
public static class HelpCommand {

private boolean enabled = true;
private GroupingMode groupingMode = GroupingMode.GROUP;

public boolean isEnabled() {
return enabled;
Expand All @@ -134,6 +135,19 @@ public boolean isEnabled() {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public GroupingMode getGroupingMode() {
return groupingMode;
}

public void setGroupingMode(GroupingMode groupingMode) {
this.groupingMode = groupingMode;
}

public enum GroupingMode {
GROUP,
FLAT
}
}

public static class ClearCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.boot.SpringShellProperties.VersionCommand;
import org.springframework.shell.boot.SpringShellProperties.HelpCommand.GroupingMode;
import org.springframework.shell.boot.condition.OnCompletionCommandCondition;
import org.springframework.shell.result.ThrowableResultHandler;
import org.springframework.shell.standard.commands.Clear;
Expand All @@ -54,8 +55,12 @@ public class StandardCommandsAutoConfiguration {
@Bean
@ConditionalOnMissingBean(Help.Command.class)
@ConditionalOnProperty(prefix = "spring.shell.command.help", value = "enabled", havingValue = "true", matchIfMissing = true)
public Help help() {
return new Help();
public Help help(SpringShellProperties properties) {
Help help = new Help();
if (properties.getCommand().getHelp().getGroupingMode() == GroupingMode.FLAT) {
help.setShowGroups(false);
}
return help;
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.shell.boot.SpringShellProperties.HelpCommand.GroupingMode;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -38,6 +39,7 @@ public void defaultNoPropertiesSet() {
assertThat(properties.getTheme().getName()).isEqualTo("default");
assertThat(properties.getCommand().getClear().isEnabled()).isTrue();
assertThat(properties.getCommand().getHelp().isEnabled()).isTrue();
assertThat(properties.getCommand().getHelp().getGroupingMode()).isEqualTo(GroupingMode.GROUP);
assertThat(properties.getCommand().getHistory().isEnabled()).isTrue();
assertThat(properties.getCommand().getQuit().isEnabled()).isTrue();
assertThat(properties.getCommand().getScript().isEnabled()).isTrue();
Expand Down Expand Up @@ -67,6 +69,7 @@ public void setProperties() {
.withPropertyValues("spring.shell.theme.name=fake")
.withPropertyValues("spring.shell.command.clear.enabled=false")
.withPropertyValues("spring.shell.command.help.enabled=false")
.withPropertyValues("spring.shell.command.help.grouping-mode=flat")
.withPropertyValues("spring.shell.command.history.enabled=false")
.withPropertyValues("spring.shell.command.quit.enabled=false")
.withPropertyValues("spring.shell.command.script.enabled=false")
Expand All @@ -93,6 +96,7 @@ public void setProperties() {
assertThat(properties.getTheme().getName()).isEqualTo("fake");
assertThat(properties.getCommand().getClear().isEnabled()).isFalse();
assertThat(properties.getCommand().getHelp().isEnabled()).isFalse();
assertThat(properties.getCommand().getHelp().getGroupingMode()).isEqualTo(GroupingMode.FLAT);
assertThat(properties.getCommand().getHistory().isEnabled()).isFalse();
assertThat(properties.getCommand().getQuit().isEnabled()).isFalse();
assertThat(properties.getCommand().getScript().isEnabled()).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package org.springframework.shell.boot;

import java.lang.reflect.Field;
import java.util.function.Function;

import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.shell.standard.commands.Completion;
import org.springframework.shell.standard.commands.Help;
import org.springframework.util.ReflectionUtils;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -48,6 +51,21 @@ public void testCompletionCommand() {
});
}

@Test
public void testHelpCommand() {
this.contextRunner
.with(disableCommands("clear", "quit", "stacktrace", "script", "history", "completion"))
.withPropertyValues("spring.shell.command.help.grouping-mode=flat")
.run(context -> {
assertThat(context).hasSingleBean(Help.class);
Help help = context.getBean(Help.class);
Field showGroupsField = ReflectionUtils.findField(Help.class, "showGroups");
ReflectionUtils.makeAccessible(showGroupsField);
ReflectionUtils.getField(showGroupsField, help);
assertThat(ReflectionUtils.getField(showGroupsField, help)).isEqualTo(false);
});
}

private static Function<ApplicationContextRunner, ApplicationContextRunner> disableCommands(String... commands) {
return (cr) -> {
for (String command : commands) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public interface Command {
}

private MessageInterpolator messageInterpolator = Utils.defaultValidatorFactory().getMessageInterpolator();
private boolean showGroups = true;

public Help() {
}
Expand All @@ -101,6 +102,16 @@ public CharSequence help(

}

/**
* Sets if groups should be shown in a listing, defaults to true. If not enabled
* a simple list is shown without groups.
*
* @param showGroups the flag to show groups
*/
public void setShowGroups(boolean showGroups) {
this.showGroups = showGroups;
}

/**
* Return a description of a specific command. Uses a layout inspired by *nix man pages.
*/
Expand Down Expand Up @@ -263,32 +274,34 @@ private String first(List<String> keys) {
private CharSequence listCommands() {
Map<String, MethodTarget> commandsByName = getCommandRegistry().listCommands();

SortedMap<String, Map<String, MethodTarget>> commandsByGroupAndName = commandsByName.entrySet().stream()
.collect(groupingBy(e -> e.getValue().getGroup(), TreeMap::new, // group by and sort by command group
toMap(Entry::getKey, Entry::getValue)));

AttributedStringBuilder result = new AttributedStringBuilder();
result.append("AVAILABLE COMMANDS\n\n", AttributedStyle.BOLD);

SortedMap<String, Map<String, MethodTarget>> commandsByGroupAndName = commandsByName.entrySet().stream()
.collect(groupingBy(e -> e.getValue().getGroup(), TreeMap::new, // group by and sort by command group
toMap(Entry::getKey, Entry::getValue)));
// display groups, sorted alphabetically, "Default" first
commandsByGroupAndName.forEach((group, commandsInGroup) -> {
result.append("".equals(group) ? "Default" : group, AttributedStyle.BOLD).append('\n');

if (showGroups) {
result.append("".equals(group) ? "Default" : group, AttributedStyle.BOLD).append('\n');
}
Map<MethodTarget, SortedSet<String>> commandNamesByMethod = commandsInGroup.entrySet().stream()
.collect(groupingBy(Entry::getValue, // group by command method
mapping(Entry::getKey, toCollection(TreeSet::new)))); // sort command names

// display commands, sorted alphabetically by their first alias
commandNamesByMethod.entrySet().stream().sorted(sortByFirstCommandName()).forEach(e -> {
String prefix = showGroups ? " " : "";
prefix = prefix + (isAvailable(e.getKey()) ? " " : " *");
result
.append(isAvailable(e.getKey()) ? " " : " * ")
.append(prefix)
.append(String.join(", ", e.getValue()), AttributedStyle.BOLD)
.append(": ")
.append(e.getKey().getHelp())
.append('\n');
});

result.append('\n');
if (showGroups) {
result.append('\n');
}
});

if (commandsByName.values().stream().distinct().anyMatch(m -> !isAvailable(m))) {
Expand Down