Skip to content

Commit

Permalink
Merge pull request #25 from hivemq/feature/get_input_parameter_via_ST…
Browse files Browse the repository at this point in the history
…DIN-#9

Feature/get input parameter via stdin #9
  • Loading branch information
gitseti committed Aug 9, 2019
2 parents c9d9512 + 6a4eb63 commit e12b2c0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
<version>3.11.0</version>
</dependency>


<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli-shell-jline2</artifactId>
<artifactId>picocli-shell-jline3</artifactId>
<version>4.0.0-alpha-1</version>
</dependency>

Expand Down
40 changes: 23 additions & 17 deletions src/main/java/com/hivemq/cli/commands/shell/Shell.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package com.hivemq.cli.commands.shell;

import com.hivemq.cli.commands.*;
import jline.console.ConsoleReader;
import jline.console.UserInterruptException;
import jline.console.completer.ArgumentCompleter.ArgumentList;
import jline.console.completer.ArgumentCompleter.WhitespaceArgumentDelimiter;
import org.jline.reader.EndOfFileException;
import org.jline.reader.*;
import org.jline.reader.impl.DefaultParser;
import org.jline.reader.impl.LineReaderImpl;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import picocli.CommandLine;
import picocli.shell.jline2.PicocliJLineCompleter;
import picocli.shell.jline3.PicocliJLineCompleter;

import java.io.PrintWriter;

@CommandLine.Command(name = "shell",
description = "Starts the mqtt cli in shell mode, to enable interactive working with further sub commands.",
footer = {"", "Press Ctl-C to exit."},
subcommands = {Connect.class, Subscribe.class, Publish.class, Disconnect.class, ClearScreen.class})
public class Shell extends AbstractCommand implements Runnable {

ConsoleReader reader;
LineReaderImpl reader;
PrintWriter out;
private static final String prompt = "hivemq-cli> ";
private static final String rightPrompt = "null";

Shell() {
}
Expand All @@ -29,22 +34,23 @@ public void run() {
public void interact(CommandLine cmd) {
try {

reader = new ConsoleReader();
reader.setPrompt("mqtt> ");

// set up the completion ;
reader.addCompleter(new PicocliJLineCompleter(cmd.getCommandSpec()));
Terminal terminal = TerminalBuilder.builder().build();
LineReader reader = LineReaderBuilder.builder()
.terminal(terminal)
.completer(new PicocliJLineCompleter(cmd.getCommandSpec()))
.parser(new DefaultParser())
.build();

// start the shell and process input until the user quits with Ctl-C
String line;
while (true) {
try {
if ((line = reader.readLine()) != null) {
ArgumentList list = new WhitespaceArgumentDelimiter().delimit(line, line.length());
CommandLine.run(this, list.getArguments());
}
line = reader.readLine(prompt, null, (MaskingCallback) null, null);
ParsedLine pl = reader.getParser().parse(line, prompt.length());
String [] arguments = pl.words().toArray(new String[0]);
CommandLine.run(this, arguments);
} catch (UserInterruptException e) {
// Ignore
return;
} catch (EndOfFileException e) {
// exit shell
// TODO all clients were disconnected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.hivemq.cli.utils.PasswordUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import picocli.CommandLine;

import java.nio.ByteBuffer;

public class PasswordConverter implements CommandLine.ITypeConverter<ByteBuffer> {

@Override
public ByteBuffer convert(@NotNull String s) throws Exception {
public ByteBuffer convert(@Nullable String s) throws Exception {
if (s.isEmpty() || s.charAt(0) == '$') {
s = new String(PasswordUtils.readPassword("Please enter the authentication password:"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5WillPublish;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5WillPublishBuilder;
import jline.internal.Log;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jline.utils.Log;
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.LoggingContext;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/hivemq/cli/mqtt/MqttClientExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient;
import com.hivemq.client.mqtt.mqtt5.message.connect.Mqtt5Connect;
import com.hivemq.client.mqtt.mqtt5.message.connect.connack.Mqtt5ConnAck;
import jline.internal.Log;

import org.jetbrains.annotations.NotNull;
import org.jline.utils.Log;
import org.pmw.tinylog.Logger;

public class MqttClientExecutor extends AbstractMqttClientExecutor {
Expand Down

0 comments on commit e12b2c0

Please sign in to comment.