Add json output listDevices and uploadStickerPack commands

This commit is contained in:
AsamK 2021-08-21 18:31:14 +02:00
parent 893b7f7f9d
commit 70fc2381d3
2 changed files with 49 additions and 13 deletions

View file

@ -3,6 +3,7 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException;
@ -15,8 +16,9 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
public class ListDevicesCommand implements LocalCommand {
public class ListDevicesCommand implements JsonRpcLocalCommand {
private final static Logger logger = LoggerFactory.getLogger(ListDevicesCommand.class);
@ -34,8 +36,6 @@ public class ListDevicesCommand implements LocalCommand {
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var writer = (PlainTextWriter) outputWriter;
List<Device> devices;
try {
devices = m.getLinkedDevices();
@ -44,13 +44,42 @@ public class ListDevicesCommand implements LocalCommand {
throw new IOErrorException("Failed to get linked devices: " + e.getMessage());
}
for (var d : devices) {
writer.println("- Device {}{}:", d.getId(), (d.getId() == m.getDeviceId() ? " (this device)" : ""));
writer.indent(w -> {
w.println("Name: {}", d.getName());
w.println("Created: {}", DateUtils.formatTimestamp(d.getCreated()));
w.println("Last seen: {}", DateUtils.formatTimestamp(d.getLastSeen()));
});
if (outputWriter instanceof PlainTextWriter) {
final var writer = (PlainTextWriter) outputWriter;
for (var d : devices) {
writer.println("- Device {}{}:", d.getId(), (d.getId() == m.getDeviceId() ? " (this device)" : ""));
writer.indent(w -> {
w.println("Name: {}", d.getName());
w.println("Created: {}", DateUtils.formatTimestamp(d.getCreated()));
w.println("Last seen: {}", DateUtils.formatTimestamp(d.getLastSeen()));
});
}
} else {
final var writer = (JsonWriter) outputWriter;
final var jsonDevices = devices.stream()
.map(d -> new JsonDevice(d.getId(), d.getName(), d.getCreated(), d.getLastSeen()))
.collect(Collectors.toList());
writer.write(jsonDevices);
}
}
private static final class JsonDevice {
public final long id;
public final String name;
public final long createdTimestamp;
public final long lastSeenTimestamp;
private JsonDevice(
final long id,
final String name,
final long createdTimestamp,
final long lastSeenTimestamp
) {
this.id = id;
this.name = name;
this.createdTimestamp = createdTimestamp;
this.lastSeenTimestamp = lastSeenTimestamp;
}
}
}

View file

@ -3,6 +3,7 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException;
@ -15,8 +16,9 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class UploadStickerPackCommand implements LocalCommand {
public class UploadStickerPackCommand implements JsonRpcLocalCommand {
private final static Logger logger = LoggerFactory.getLogger(UploadStickerPackCommand.class);
@ -36,12 +38,17 @@ public class UploadStickerPackCommand implements LocalCommand {
public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException {
final var writer = (PlainTextWriter) outputWriter;
var path = new File(ns.getString("path"));
try {
var url = m.uploadStickerPack(path);
writer.println("{}", url);
if (outputWriter instanceof PlainTextWriter) {
final var writer = (PlainTextWriter) outputWriter;
writer.println("{}", url);
} else {
final var writer = (JsonWriter) outputWriter;
writer.write(Map.of("url", url));
}
} catch (IOException e) {
throw new IOErrorException("Upload error (maybe image size too large):" + e.getMessage());
} catch (StickerPackInvalidException e) {