]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/App.java
Refactor output writers
[signal-cli] / src / main / java / org / asamk / signal / App.java
index 86d9a9719ad94cc298a710ca75ec22f93d1c9d74..33338a4c29db42b945f550fa117c328d19930746 100644 (file)
@@ -76,11 +76,10 @@ public class App {
 
         var subparsers = parser.addSubparsers().title("subcommands").dest("command");
 
-        final var commands = Commands.getCommands();
-        for (var entry : commands.entrySet()) {
-            var subparser = subparsers.addParser(entry.getKey());
-            entry.getValue().attachToSubparser(subparser);
-        }
+        Commands.getCommandSubparserAttachers().forEach((key, value) -> {
+            var subparser = subparsers.addParser(key);
+            value.attachToSubparser(subparser);
+        });
 
         return parser;
     }
@@ -90,13 +89,17 @@ public class App {
     }
 
     public void init() throws CommandException {
+        var outputType = ns.<OutputType>get("output");
+        var outputWriter = outputType == OutputType.JSON
+                ? new JsonWriter(System.out)
+                : new PlainTextWriterImpl(System.out);
+
         var commandKey = ns.getString("command");
-        var command = Commands.getCommand(commandKey);
+        var command = Commands.getCommand(commandKey, outputWriter);
         if (command == null) {
             throw new UserErrorException("Command not implemented!");
         }
 
-        var outputType = ns.<OutputType>get("output");
         if (!command.getSupportedOutputTypes().contains(outputType)) {
             throw new UserErrorException("Command doesn't support output type " + outputType.toString());
         }
@@ -305,32 +308,9 @@ public class App {
     }
 
     /**
-     * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
-     * - $HOME/.config/signal
-     * - $HOME/.config/textsecure
-     *
-     * @return the data directory to be used by signal-cli.
+     * @return the default data directory to be used by signal-cli.
      */
     private static File getDefaultDataPath() {
-        var dataPath = new File(IOUtils.getDataHomeDir(), "signal-cli");
-        if (dataPath.exists()) {
-            return dataPath;
-        }
-
-        var configPath = new File(System.getProperty("user.home"), ".config");
-
-        var legacySettingsPath = new File(configPath, "signal");
-        if (legacySettingsPath.exists()) {
-            logger.warn("Using legacy data path \"{}\", please move it to \"{}\".", legacySettingsPath, dataPath);
-            return legacySettingsPath;
-        }
-
-        legacySettingsPath = new File(configPath, "textsecure");
-        if (legacySettingsPath.exists()) {
-            logger.warn("Using legacy data path \"{}\", please move it to \"{}\".", legacySettingsPath, dataPath);
-            return legacySettingsPath;
-        }
-
-        return dataPath;
+        return new File(IOUtils.getDataHomeDir(), "signal-cli");
     }
 }