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.Namespace;
|
||||||
import net.sourceforge.argparse4j.inf.Subparser;
|
import net.sourceforge.argparse4j.inf.Subparser;
|
||||||
|
|
||||||
|
import org.asamk.signal.JsonWriter;
|
||||||
import org.asamk.signal.OutputWriter;
|
import org.asamk.signal.OutputWriter;
|
||||||
import org.asamk.signal.PlainTextWriter;
|
import org.asamk.signal.PlainTextWriter;
|
||||||
import org.asamk.signal.commands.exceptions.CommandException;
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
|
@ -15,8 +16,9 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
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);
|
private final static Logger logger = LoggerFactory.getLogger(ListDevicesCommand.class);
|
||||||
|
|
||||||
|
@ -34,8 +36,6 @@ public class ListDevicesCommand implements LocalCommand {
|
||||||
public void handleCommand(
|
public void handleCommand(
|
||||||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
final var writer = (PlainTextWriter) outputWriter;
|
|
||||||
|
|
||||||
List<Device> devices;
|
List<Device> devices;
|
||||||
try {
|
try {
|
||||||
devices = m.getLinkedDevices();
|
devices = m.getLinkedDevices();
|
||||||
|
@ -44,13 +44,42 @@ public class ListDevicesCommand implements LocalCommand {
|
||||||
throw new IOErrorException("Failed to get linked devices: " + e.getMessage());
|
throw new IOErrorException("Failed to get linked devices: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var d : devices) {
|
if (outputWriter instanceof PlainTextWriter) {
|
||||||
writer.println("- Device {}{}:", d.getId(), (d.getId() == m.getDeviceId() ? " (this device)" : ""));
|
final var writer = (PlainTextWriter) outputWriter;
|
||||||
writer.indent(w -> {
|
for (var d : devices) {
|
||||||
w.println("Name: {}", d.getName());
|
writer.println("- Device {}{}:", d.getId(), (d.getId() == m.getDeviceId() ? " (this device)" : ""));
|
||||||
w.println("Created: {}", DateUtils.formatTimestamp(d.getCreated()));
|
writer.indent(w -> {
|
||||||
w.println("Last seen: {}", DateUtils.formatTimestamp(d.getLastSeen()));
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.asamk.signal.commands;
|
||||||
import net.sourceforge.argparse4j.inf.Namespace;
|
import net.sourceforge.argparse4j.inf.Namespace;
|
||||||
import net.sourceforge.argparse4j.inf.Subparser;
|
import net.sourceforge.argparse4j.inf.Subparser;
|
||||||
|
|
||||||
|
import org.asamk.signal.JsonWriter;
|
||||||
import org.asamk.signal.OutputWriter;
|
import org.asamk.signal.OutputWriter;
|
||||||
import org.asamk.signal.PlainTextWriter;
|
import org.asamk.signal.PlainTextWriter;
|
||||||
import org.asamk.signal.commands.exceptions.CommandException;
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
|
@ -15,8 +16,9 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
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);
|
private final static Logger logger = LoggerFactory.getLogger(UploadStickerPackCommand.class);
|
||||||
|
|
||||||
|
@ -36,12 +38,17 @@ public class UploadStickerPackCommand implements LocalCommand {
|
||||||
public void handleCommand(
|
public void handleCommand(
|
||||||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
final var writer = (PlainTextWriter) outputWriter;
|
|
||||||
var path = new File(ns.getString("path"));
|
var path = new File(ns.getString("path"));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var url = m.uploadStickerPack(path);
|
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) {
|
} catch (IOException e) {
|
||||||
throw new IOErrorException("Upload error (maybe image size too large):" + e.getMessage());
|
throw new IOErrorException("Upload error (maybe image size too large):" + e.getMessage());
|
||||||
} catch (StickerPackInvalidException e) {
|
} catch (StickerPackInvalidException e) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue