Skip to content

Commit 9917813

Browse files
committed
Merge command utilities in the existing Utils class
1 parent 858c00e commit 9917813

File tree

5 files changed

+41
-77
lines changed

5 files changed

+41
-77
lines changed

spring-shell-core/src/main/java/org/springframework/shell/core/InteractiveShellRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.springframework.shell.core.command.CommandRegistry;
2929
import org.springframework.shell.core.command.ExitStatus;
3030
import org.springframework.shell.core.command.ParsedInput;
31-
import org.springframework.shell.core.utils.CommandUtils;
31+
import org.springframework.shell.core.utils.Utils;
3232

3333
/**
3434
* Base class for interactive shell runners. Implementations must provide concrete methods
@@ -111,7 +111,7 @@ public void run(String[] args) throws Exception {
111111
}
112112
catch (CommandNotFoundException exception) {
113113
print(String.format("Command %s not found", exception.getCommandName()));
114-
print(CommandUtils.formatAvailableCommands(this.commandRegistry));
114+
print(Utils.formatAvailableCommands(this.commandRegistry));
115115
}
116116
finally {
117117
flush();

spring-shell-core/src/main/java/org/springframework/shell/core/command/Help.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import java.io.PrintWriter;
1919

20-
import org.springframework.shell.core.utils.CommandUtils;
20+
import org.springframework.shell.core.utils.Utils;
2121

2222
/**
2323
* A command to display help about all available commands.
@@ -41,7 +41,7 @@ public String getGroup() {
4141

4242
@Override
4343
public ExitStatus execute(CommandContext commandContext) throws Exception {
44-
String helpMessage = CommandUtils.formatAvailableCommands(commandContext.commandRegistry());
44+
String helpMessage = Utils.formatAvailableCommands(commandContext.commandRegistry());
4545
PrintWriter outputWriter = commandContext.outputWriter();
4646
outputWriter.println(helpMessage);
4747
outputWriter.flush();

spring-shell-core/src/main/java/org/springframework/shell/core/command/Script.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.List;
2020

2121
import org.springframework.shell.core.FileInputProvider;
22-
import org.springframework.shell.core.utils.CommandUtils;
22+
import org.springframework.shell.core.utils.Utils;
2323

2424
/**
2525
* A command that can read and execute other commands from a file.
@@ -50,7 +50,7 @@ public ExitStatus doExecute(CommandContext commandContext) throws Exception {
5050
private void executeCommand(CommandContext commandContext, String input) throws Exception {
5151
Command command = commandContext.commandRegistry().getCommandByName(input);
5252
if (command == null) {
53-
String availableCommands = CommandUtils.formatAvailableCommands(commandContext.commandRegistry());
53+
String availableCommands = Utils.formatAvailableCommands(commandContext.commandRegistry());
5454
throw new CommandNotFoundException("No command found for name: " + input + ". " + availableCommands);
5555
}
5656
CommandContext singleCommandContext = new CommandContext(commandContext.parsedInput(),

spring-shell-core/src/main/java/org/springframework/shell/core/utils/CommandUtils.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

spring-shell-core/src/main/java/org/springframework/shell/core/utils/Utils.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,14 +34,48 @@
3434

3535
import org.springframework.core.DefaultParameterNameDiscoverer;
3636
import org.springframework.core.MethodParameter;
37+
import org.springframework.shell.core.command.Command;
38+
import org.springframework.shell.core.command.CommandRegistry;
3739

3840
/**
3941
* Some text utilities.
4042
*
4143
* @author Eric Bottard
44+
* @author Mahmoud Ben Hassine
4245
*/
4346
public class Utils {
4447

48+
/**
49+
* Get a formatted string of available non-hidden commands from the command registry.
50+
* @param commandRegistry the command registry
51+
* @return a string of available commands with their descriptions
52+
* @since 4.0.0
53+
*/
54+
public static String formatAvailableCommands(CommandRegistry commandRegistry) {
55+
StringBuilder stringBuilder = new StringBuilder("Available commands: ");
56+
stringBuilder.append(System.lineSeparator());
57+
List<String> groups = commandRegistry.getCommands()
58+
.stream()
59+
.filter(command -> !command.isHidden())
60+
.map(Command::getGroup)
61+
.distinct()
62+
.sorted()
63+
.toList();
64+
for (String group : groups) {
65+
stringBuilder.append(group).append(System.lineSeparator());
66+
for (Command command : commandRegistry.getCommands()) {
67+
if (command.getGroup().equals(group)) {
68+
stringBuilder.append("\t")
69+
.append(command.getName())
70+
.append(": ")
71+
.append(command.getDescription())
72+
.append(System.lineSeparator());
73+
}
74+
}
75+
}
76+
return stringBuilder.toString();
77+
}
78+
4579
/**
4680
* Turn CamelCaseText into gnu-style-lowercase.
4781
*/

0 commit comments

Comments
 (0)