mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Add PlainTextWriter interface
This commit is contained in:
parent
641dc7577c
commit
15e8029715
16 changed files with 69 additions and 66 deletions
|
@ -92,7 +92,7 @@ public class App {
|
|||
public void init() throws CommandException {
|
||||
var outputType = ns.<OutputType>get("output");
|
||||
var outputWriter = outputType == OutputType.JSON
|
||||
? new JsonWriter(System.out)
|
||||
? new JsonWriterImpl(System.out)
|
||||
: new PlainTextWriterImpl(System.out);
|
||||
|
||||
var commandKey = ns.getString("command");
|
||||
|
|
|
@ -1,43 +1,6 @@
|
|||
package org.asamk.signal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
public interface JsonWriter extends OutputWriter {
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class JsonWriter implements OutputWriter {
|
||||
|
||||
private final Writer writer;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JsonWriter(final OutputStream outputStream) {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
|
||||
|
||||
objectMapper = new ObjectMapper();
|
||||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
|
||||
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
|
||||
}
|
||||
|
||||
public void write(final Object object) {
|
||||
try {
|
||||
try {
|
||||
objectMapper.writeValue(writer, object);
|
||||
} catch (JsonProcessingException e) {
|
||||
// Some issue with json serialization, probably caused by a bug
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
writer.write(System.lineSeparator());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
void write(final Object object);
|
||||
}
|
||||
|
|
43
src/main/java/org/asamk/signal/JsonWriterImpl.java
Normal file
43
src/main/java/org/asamk/signal/JsonWriterImpl.java
Normal file
|
@ -0,0 +1,43 @@
|
|||
package org.asamk.signal;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class JsonWriterImpl implements JsonWriter {
|
||||
|
||||
private final Writer writer;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JsonWriterImpl(final OutputStream outputStream) {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
|
||||
|
||||
objectMapper = new ObjectMapper();
|
||||
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
|
||||
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
|
||||
}
|
||||
|
||||
public synchronized void write(final Object object) {
|
||||
try {
|
||||
try {
|
||||
objectMapper.writeValue(writer, object);
|
||||
} catch (JsonProcessingException e) {
|
||||
// Some issue with json serialization, probably caused by a bug
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
writer.write(System.lineSeparator());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
|
|||
import org.asamk.signal.JsonWriter;
|
||||
import org.asamk.signal.OutputType;
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
|
@ -60,7 +60,7 @@ public class GetUserStatusCommand implements LocalCommand {
|
|||
|
||||
jsonWriter.write(jsonUserStatuses);
|
||||
} else {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
for (var entry : registered.entrySet()) {
|
||||
writer.println("{}: {}", entry.getKey(), entry.getValue());
|
||||
|
|
|
@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
|
|||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
||||
|
@ -49,7 +49,7 @@ public class JoinGroupCommand implements LocalCommand {
|
|||
}
|
||||
|
||||
try {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
final var results = m.joinGroup(linkUrl);
|
||||
var newGroupId = results.first();
|
||||
|
|
|
@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
|
|||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
|
@ -32,7 +32,7 @@ public class LinkCommand implements ProvisioningCommand {
|
|||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final ProvisioningManager m) throws CommandException {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
var deviceName = ns.getString("name");
|
||||
if (deviceName == null) {
|
||||
|
|
|
@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
|
|||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
|
||||
import static org.asamk.signal.util.Util.getLegacyIdentifier;
|
||||
|
@ -23,7 +23,7 @@ public class ListContactsCommand implements LocalCommand {
|
|||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final Manager m) {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
var contacts = m.getContacts();
|
||||
for (var c : contacts) {
|
||||
|
|
|
@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
|
|||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
|
@ -31,7 +31,7 @@ public class ListDevicesCommand implements LocalCommand {
|
|||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
List<Device> devices;
|
||||
try {
|
||||
|
|
|
@ -5,7 +5,6 @@ import net.sourceforge.argparse4j.inf.Subparser;
|
|||
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
|
@ -46,7 +45,7 @@ public class ListIdentitiesCommand implements LocalCommand {
|
|||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
var number = ns.getString("number");
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
|
|||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
|
@ -48,7 +48,7 @@ public class QuitGroupCommand implements LocalCommand {
|
|||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
final GroupId groupId;
|
||||
try {
|
||||
|
|
|
@ -10,7 +10,6 @@ import org.asamk.signal.JsonWriter;
|
|||
import org.asamk.signal.OutputType;
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.ReceiveMessageHandler;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
|
@ -79,7 +78,7 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
|
|||
jsonWriter.write(object);
|
||||
});
|
||||
} else {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> {
|
||||
writer.println("Envelope from: {}", messageReceived.getSender());
|
||||
|
|
|
@ -5,7 +5,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
|
|||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
|
@ -49,7 +49,7 @@ public class RemoteDeleteCommand implements DbusCommand {
|
|||
|
||||
final long targetTimestamp = ns.getLong("target-timestamp");
|
||||
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
byte[] groupId = null;
|
||||
if (groupIdString != null) {
|
||||
|
|
|
@ -6,7 +6,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
|
|||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
|
@ -60,7 +60,7 @@ public class SendReactionCommand implements DbusCommand {
|
|||
final var targetAuthor = ns.getString("target-author");
|
||||
final long targetTimestamp = ns.getLong("target-timestamp");
|
||||
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
byte[] groupId = null;
|
||||
if (groupIdString != null) {
|
||||
|
|
|
@ -32,7 +32,6 @@ public class UnregisterCommand implements LocalCommand {
|
|||
m.unregister();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new IOErrorException("Unregister error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import org.asamk.Signal;
|
|||
import org.asamk.signal.GroupLinkState;
|
||||
import org.asamk.signal.GroupPermission;
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
|
@ -74,7 +74,7 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
|
|||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
GroupId groupId = null;
|
||||
final var groupIdString = ns.getString("group");
|
||||
if (groupIdString != null) {
|
||||
|
@ -142,7 +142,7 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
|
|||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
byte[] groupId = null;
|
||||
if (ns.getString("group") != null) {
|
||||
try {
|
||||
|
|
|
@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
|
|||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.PlainTextWriterImpl;
|
||||
import org.asamk.signal.PlainTextWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
|
@ -33,7 +33,7 @@ public class UploadStickerPackCommand implements LocalCommand {
|
|||
|
||||
@Override
|
||||
public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
|
||||
final var writer = (PlainTextWriterImpl) outputWriter;
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
var path = new File(ns.getString("path"));
|
||||
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue