mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Add json output listDevices and uploadStickerPack commands
This commit is contained in:
parent
893b7f7f9d
commit
70fc2381d3
2 changed files with 49 additions and 13 deletions
|
@ -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,6 +44,8 @@ public class ListDevicesCommand implements LocalCommand {
|
|||
throw new IOErrorException("Failed to get linked devices: " + e.getMessage());
|
||||
}
|
||||
|
||||
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 -> {
|
||||
|
@ -52,5 +54,32 @@ public class ListDevicesCommand implements LocalCommand {
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue