Check if output type is supported by command

This commit is contained in:
AsamK 2021-01-16 21:12:01 +01:00
parent c588130491
commit a2debdb234
6 changed files with 37 additions and 0 deletions

View file

@ -91,6 +91,12 @@ public class Cli {
return 1;
}
OutputType outputType = ns.get("output");
if (!command.getSupportedOutputTypes().contains(outputType)) {
logger.error("Command doesn't support output type {}", outputType.toString());
return 1;
}
String username = ns.getString("username");
final boolean useDbus = ns.getBoolean("dbus");

View file

@ -2,7 +2,15 @@ package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputType;
import java.util.Set;
public interface Command {
void attachToSubparser(Subparser subparser);
default Set<OutputType> getSupportedOutputTypes() {
return Set.of(OutputType.PLAIN_TEXT);
}
}

View file

@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class DaemonCommand implements MultiLocalCommand {
@ -37,6 +38,11 @@ public class DaemonCommand implements MultiLocalCommand {
.action(Arguments.storeTrue());
}
@Override
public Set<OutputType> getSupportedOutputTypes() {
return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON);
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
boolean inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");

View file

@ -14,6 +14,7 @@ import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class GetUserStatusCommand implements LocalCommand {
@ -29,6 +30,11 @@ public class GetUserStatusCommand implements LocalCommand {
.action(Arguments.storeTrue());
}
@Override
public Set<OutputType> getSupportedOutputTypes() {
return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON);
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
// Setup the json object mapper

View file

@ -62,6 +62,11 @@ public class ListGroupsCommand implements LocalCommand {
subparser.help("List group information including names, ids, active status, blocked status and members");
}
@Override
public Set<OutputType> getSupportedOutputTypes() {
return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON);
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
if (ns.get("output") == OutputType.JSON) {

View file

@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Base64;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
@ -41,6 +42,11 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
.action(Arguments.storeTrue());
}
@Override
public Set<OutputType> getSupportedOutputTypes() {
return Set.of(OutputType.PLAIN_TEXT, OutputType.JSON);
}
public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) {
boolean inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");