-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[improve][cli]Support for pulsar-shell persistent modes #22515
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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()); | ||
|
|
@@ -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() { | ||
|
|
@@ -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) { | ||
|
|
@@ -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"); | ||
| } | ||
|
|
||
| private static boolean isChangeModeCommand(String line) { | ||
| return line.equalsIgnoreCase("admin") || line.equalsIgnoreCase("client") || line.equalsIgnoreCase("config"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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())) { | ||
|
|
||
There was a problem hiding this comment.
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
exitmodecommand. Simpleexitor pressing CTRL-D should do that.