Sort CLI commands alphabetically (#2020)

Sort CLI commands alphabetically to show them in consistent order.
This commit is contained in:
Eduard Heimbuch
2022-05-04 09:36:01 +02:00
committed by GitHub
parent 689b191ec3
commit 1c540e1fbd
4 changed files with 37 additions and 16 deletions

View File

@@ -26,10 +26,11 @@ package sonia.scm.cli;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Singleton
public class CommandRegistry {
@@ -41,17 +42,17 @@ public class CommandRegistry {
this.commandCollector = commandCollector;
}
public Set<RegisteredCommandNode> createCommandTree() {
Set<RegisteredCommandNode> rootCommands = new HashSet<>();
Set<RegisteredCommand> registeredCommands = commandCollector.collect();
public List<RegisteredCommandNode> createCommandTree() {
List<RegisteredCommandNode> rootCommands = new ArrayList<>();
List<RegisteredCommand> sortedCommands = collectSortedCommands();
Map<Class<?>, RegisteredCommandNode> commandNodes = new HashMap<>();
for (RegisteredCommand command : registeredCommands) {
for (RegisteredCommand command : sortedCommands) {
commandNodes.put(command.getCommand(), new RegisteredCommandNode(command.getName(), command.getCommand()));
}
for (RegisteredCommand command : registeredCommands) {
for (RegisteredCommand command : sortedCommands) {
RegisteredCommandNode node = commandNodes.get(command.getCommand());
if (command.getParent() == null) {
rootCommands.add(node);
@@ -66,4 +67,11 @@ public class CommandRegistry {
}
return rootCommands;
}
private List<RegisteredCommand> collectSortedCommands() {
return commandCollector.collect()
.stream()
.sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName()))
.collect(Collectors.toList());
}
}