Skip to content
Closed
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 @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;
Expand Down Expand Up @@ -126,6 +127,18 @@ enum ExecState {
IDLE,
RUNNING
}
enum ShellMode {
ADMIN("admin"),
CLIENT("client"),
CONFIG("config"),
DEFAULT("");

final String command;

ShellMode(String command) {
this.command = command;
}
}
private Properties properties;
@Getter
private final ConfigStore configStore;
Expand All @@ -137,6 +150,9 @@ enum ExecState {
private InteractiveLineReader reader;
private final ConfigShell configShell;
private ExecState execState = ExecState.IDLE;
private ShellMode shellMode = ShellMode.DEFAULT;
private String prompt;
private String promptMessage;

public PulsarShell(String args[]) throws IOException {
this(args, new Properties());
Expand Down Expand Up @@ -282,14 +298,13 @@ public void run() throws Exception {
new AttributedStringBuilder().style(AttributedStyle.BOLD).append("exit").toAnsi(),
new AttributedStringBuilder().style(AttributedStyle.BOLD).append("quit").toAnsi());
output(welcomeMessage, terminal);
String promptMessage;
if (configShell.getCurrentConfig() != null) {
promptMessage = String.format("%s(%s)",
configShell.getCurrentConfig(), getHostFromUrl(serviceUrl));
} else {
promptMessage = getHostFromUrl(serviceUrl);
}
final String prompt = createPrompt(promptMessage);
prompt = createPrompt(promptMessage);
return new InteractiveLineReader() {
@Override
public String readLine() {
Expand Down Expand Up @@ -429,18 +444,38 @@ public List<String> readCommand() {
return;
}
execState = ExecState.RUNNING;
final String line = words.stream().collect(Collectors.joining(" "));
if (StringUtils.isBlank(line)) {
final String inputLine = words.stream().collect(Collectors.joining(" "));
if (StringUtils.isBlank(inputLine)) {
continue;
}
if (isQuitCommand(line)) {
if (isQuitCommand(inputLine)) {
exit(0);
return;
}
if (isExitModeCommand(inputLine)) {
if (shellMode == ShellMode.DEFAULT){
output("Cant exit from default shell mode", terminal);
continue;
}
promptMessage = promptMessage.substring(0, promptMessage.lastIndexOf(shellMode.command) - 1);
prompt = createPrompt(promptMessage);
shellMode = ShellMode.DEFAULT;
continue;
}
if (shellMode != ShellMode.DEFAULT) {
words.add(0, shellMode.command);
}
final String line = words.stream().collect(Collectors.joining(" "));
if (shellOptions.help) {
shellCommander.usage();
continue;
}
if (isChangeModeCommand(line)){
shellMode = ShellMode.valueOf(line.toUpperCase(Locale.ROOT));
promptMessage = promptMessage.concat(" ").concat(shellMode.command);
prompt = createPrompt(promptMessage);
continue;
}

final ShellCommandsProvider pulsarShellCommandsProvider = getProviderFromArgs(shellCommander, words);
if (pulsarShellCommandsProvider == null) {
Expand Down Expand Up @@ -567,6 +602,14 @@ private static boolean isQuitCommand(String line) {
return line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit");
}

private static boolean isExitModeCommand(String line) {
return line.equalsIgnoreCase("exitmode");
}
Comment on lines +605 to +607
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there shouldn't be a separate exitmode command. Simple exit or pressing CTRL-D should do that.


private static boolean isChangeModeCommand(String line) {
return line.equalsIgnoreCase("admin") || line.equalsIgnoreCase("client") || line.equalsIgnoreCase("config");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like duplication. I'd assume that the ShellMode enum would have a status lookup method instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example https://www.baeldung.com/java-enum-values contains some advice how this could be handled.

}

private static String[] extractAndConvertArgs(List<String> words) {
List<String> parsed = new ArrayList<>();
for (String s : words.subList(1, words.size())) {
Expand Down