]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/App.java
Reorder static final modifier
[signal-cli] / src / main / java / org / asamk / signal / App.java
index c31e6f49de5c9101f1746ca786109529df0c1e46..83c0ef56061d2418391d58151270f48159b64daf 100644 (file)
@@ -26,6 +26,7 @@ import org.asamk.signal.manager.api.NotRegisteredException;
 import org.asamk.signal.manager.api.ServiceEnvironment;
 import org.asamk.signal.manager.api.TrustNewIdentity;
 import org.asamk.signal.output.JsonWriterImpl;
+import org.asamk.signal.output.OutputWriter;
 import org.asamk.signal.output.PlainTextWriterImpl;
 import org.asamk.signal.util.IOUtils;
 import org.freedesktop.dbus.connections.impl.DBusConnection;
@@ -46,7 +47,7 @@ import static net.sourceforge.argparse4j.DefaultSettings.VERSION_0_9_0_DEFAULT_S
 
 public class App {
 
-    private final static Logger logger = LoggerFactory.getLogger(App.class);
+    private static final Logger logger = LoggerFactory.getLogger(App.class);
 
     private final Namespace ns;
 
@@ -121,19 +122,7 @@ public class App {
             throw new UserErrorException("Command not implemented!");
         }
 
-        var outputTypeInput = ns.<OutputType>get("output");
-        var outputType = outputTypeInput == null
-                ? command.getSupportedOutputTypes().stream().findFirst().orElse(null)
-                : outputTypeInput;
-        var writer = new BufferedWriter(new OutputStreamWriter(System.out, IOUtils.getConsoleCharset()));
-        var outputWriter = outputType == null
-                ? null
-                : outputType == OutputType.JSON ? new JsonWriterImpl(writer) : new PlainTextWriterImpl(writer);
-
-        if (outputWriter != null && !command.getSupportedOutputTypes().contains(outputType)) {
-            throw new UserErrorException("Command doesn't support output type " + outputType);
-        }
-
+        final var outputWriter = getOutputWriter(command);
         final var commandHandler = new CommandHandler(ns, outputWriter);
 
         var account = ns.getString("account");
@@ -150,36 +139,17 @@ public class App {
             throw new UserErrorException("Missing required native library dependency: libsignal-client");
         }
 
-        final File configPath;
-        var config = ns.getString("config");
-        if (config != null) {
-            configPath = new File(config);
-        } else {
-            configPath = getDefaultConfigPath();
-        }
-
-        final var serviceEnvironmentCli = ns.<ServiceEnvironmentCli>get("service-environment");
-        final var serviceEnvironment = serviceEnvironmentCli == ServiceEnvironmentCli.LIVE
-                ? ServiceEnvironment.LIVE
-                : ServiceEnvironment.STAGING;
-
-        final var trustNewIdentityCli = ns.<TrustNewIdentityCli>get("trust-new-identities");
-        final var trustNewIdentity = trustNewIdentityCli == TrustNewIdentityCli.ON_FIRST_USE
-                ? TrustNewIdentity.ON_FIRST_USE
-                : trustNewIdentityCli == TrustNewIdentityCli.ALWAYS ? TrustNewIdentity.ALWAYS : TrustNewIdentity.NEVER;
-
-        final var disableSendLog = Boolean.TRUE.equals(ns.getBoolean("disable-send-log"));
+        final var signalAccountFiles = loadSignalAccountFiles();
 
-        final SignalAccountFiles signalAccountFiles;
-        try {
-            signalAccountFiles = new SignalAccountFiles(configPath,
-                    serviceEnvironment,
-                    BaseConfig.USER_AGENT,
-                    new Settings(trustNewIdentity, disableSendLog));
-        } catch (IOException e) {
-            throw new IOErrorException("Failed to read local accounts list", e);
-        }
+        handleCommand(command, commandHandler, account, signalAccountFiles);
+    }
 
+    private void handleCommand(
+            final Command command,
+            final CommandHandler commandHandler,
+            String account,
+            final SignalAccountFiles signalAccountFiles
+    ) throws CommandException {
         if (command instanceof ProvisioningCommand provisioningCommand) {
             if (account != null) {
                 throw new UserErrorException("You cannot specify a account (phone number) when linking");
@@ -195,20 +165,7 @@ public class App {
                 return;
             }
 
-            Set<String> accounts;
-            try {
-                accounts = signalAccountFiles.getAllLocalAccountNumbers();
-            } catch (IOException e) {
-                throw new IOErrorException("Failed to load local accounts file", e);
-            }
-            if (accounts.size() == 0) {
-                throw new UserErrorException("No local users found, you first need to register or link an account");
-            } else if (accounts.size() > 1) {
-                throw new UserErrorException(
-                        "Multiple users found, you need to specify an account (phone number) with -a");
-            }
-
-            account = accounts.stream().findFirst().get();
+            account = getAccountIfOnlyOne(signalAccountFiles);
         } else if (!Manager.isValidNumber(account, null)) {
             throw new UserErrorException("Invalid account (phone number), make sure you include the country code.");
         }
@@ -218,11 +175,75 @@ public class App {
             return;
         }
 
-        if (!(command instanceof LocalCommand)) {
-            throw new UserErrorException("Command only works in multi-account mode");
+        if (command instanceof LocalCommand localCommand) {
+            handleLocalCommand(localCommand, account, signalAccountFiles, commandHandler);
+            return;
         }
 
-        handleLocalCommand((LocalCommand) command, account, signalAccountFiles, commandHandler);
+        throw new UserErrorException("Command only works in multi-account mode");
+    }
+
+    private static String getAccountIfOnlyOne(final SignalAccountFiles signalAccountFiles) throws IOErrorException, UserErrorException {
+        Set<String> accounts;
+        try {
+            accounts = signalAccountFiles.getAllLocalAccountNumbers();
+        } catch (IOException e) {
+            throw new IOErrorException("Failed to load local accounts file", e);
+        }
+        if (accounts.isEmpty()) {
+            throw new UserErrorException("No local users found, you first need to register or link an account");
+        } else if (accounts.size() > 1) {
+            throw new UserErrorException("Multiple users found, you need to specify an account (phone number) with -a");
+        }
+        return accounts.stream().findFirst().get();
+    }
+
+    private OutputWriter getOutputWriter(final Command command) throws UserErrorException {
+        final var outputTypeInput = ns.<OutputType>get("output");
+        final var outputType = outputTypeInput == null ? command.getSupportedOutputTypes()
+                .stream()
+                .findFirst()
+                .orElse(null) : outputTypeInput;
+        final var writer = new BufferedWriter(new OutputStreamWriter(System.out, IOUtils.getConsoleCharset()));
+        final var outputWriter = outputType == null
+                ? null
+                : outputType == OutputType.JSON ? new JsonWriterImpl(writer) : new PlainTextWriterImpl(writer);
+
+        if (outputWriter != null && !command.getSupportedOutputTypes().contains(outputType)) {
+            throw new UserErrorException("Command doesn't support output type " + outputType);
+        }
+        return outputWriter;
+    }
+
+    private SignalAccountFiles loadSignalAccountFiles() throws IOErrorException {
+        final File configPath;
+        final var config = ns.getString("config");
+        if (config != null) {
+            configPath = new File(config);
+        } else {
+            configPath = getDefaultConfigPath();
+        }
+
+        final var serviceEnvironmentCli = ns.<ServiceEnvironmentCli>get("service-environment");
+        final var serviceEnvironment = serviceEnvironmentCli == ServiceEnvironmentCli.LIVE
+                ? ServiceEnvironment.LIVE
+                : ServiceEnvironment.STAGING;
+
+        final var trustNewIdentityCli = ns.<TrustNewIdentityCli>get("trust-new-identities");
+        final var trustNewIdentity = trustNewIdentityCli == TrustNewIdentityCli.ON_FIRST_USE
+                ? TrustNewIdentity.ON_FIRST_USE
+                : trustNewIdentityCli == TrustNewIdentityCli.ALWAYS ? TrustNewIdentity.ALWAYS : TrustNewIdentity.NEVER;
+
+        final var disableSendLog = Boolean.TRUE.equals(ns.getBoolean("disable-send-log"));
+
+        try {
+            return new SignalAccountFiles(configPath,
+                    serviceEnvironment,
+                    BaseConfig.USER_AGENT,
+                    new Settings(trustNewIdentity, disableSendLog));
+        } catch (IOException e) {
+            throw new IOErrorException("Failed to read local accounts list", e);
+        }
     }
 
     private void handleProvisioningCommand(
@@ -255,8 +276,6 @@ public class App {
     ) throws CommandException {
         try (var m = loadManager(account, signalAccountFiles)) {
             commandHandler.handleLocalCommand(command, m);
-        } catch (IOException e) {
-            logger.warn("Cleanup failed", e);
         }
     }