From 7736d441b498454ec73d364fc0e42cf0ac820799 Mon Sep 17 00:00:00 2001
From: Kai Reinhard <K.Reinhard@micromata.de>
Date: Sat, 08 Dec 2018 09:48:12 +0000
Subject: [PATCH] borg list repo

---
 borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java |   63 +++++++++++++++++++------------
 1 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java
index a0508c0..734c97a 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java
+++ b/borgbutler-core/src/main/java/de/micromata/borgbutler/BorgCommands.java
@@ -5,6 +5,7 @@
 import de.micromata.borgbutler.config.ConfigurationHandler;
 import de.micromata.borgbutler.json.JsonUtils;
 import de.micromata.borgbutler.json.borg.RepoInfo;
+import de.micromata.borgbutler.json.borg.RepoList;
 import org.apache.commons.exec.*;
 import org.apache.commons.exec.environment.EnvironmentUtils;
 import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -20,32 +21,46 @@
     private static Logger log = LoggerFactory.getLogger(BorgCommands.class);
 
     public static RepoInfo info(BorgRepoConfig repoConfig) {
-        try {
-            CommandLine cmdLine = new CommandLine(ConfigurationHandler.getConfiguration().getBorgCommand());
-            cmdLine.addArgument("info");
-            cmdLine.addArgument("--json");
-            cmdLine.addArgument(repoConfig.getRepo());
-            DefaultExecutor executor = new DefaultExecutor();
-            //executor.setExitValue(2);
-            ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
-            executor.setWatchdog(watchdog);
-            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-            ExecuteResultHandler handler = new DefaultExecuteResultHandler();
-            PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
-            executor.setStreamHandler(streamHandler);
-            log.info("Executing '" + cmdLine.getExecutable() + " " + StringUtils.join(cmdLine.getArguments(), " ") + "'...");
-            executor.execute(cmdLine, getEnvironment(repoConfig));
-            String json = outputStream.toString(Charset.forName("UTF-8"));
-            RepoInfo repoInfo = JsonUtils.fromJson(RepoInfo.class, json);
-            repoInfo.setOriginalJson(json);
-            return repoInfo;
-        } catch (IOException ex) {
-            log.error("Error while executing borg command: " + ex.getMessage(), ex);
-            return null;
-        }
+        String json = execute(repoConfig, "info", "--json");
+        RepoInfo repoInfo = JsonUtils.fromJson(RepoInfo.class, json);
+        repoInfo.setOriginalJson(json);
+        return repoInfo;
     }
 
-    public static Map<String, String> getEnvironment(BorgRepoConfig repoConfig) throws IOException {
+    public static RepoList list(BorgRepoConfig repoConfig) {
+        String json = execute(repoConfig, "list", "--json");
+        RepoList repoList = JsonUtils.fromJson(RepoList.class, json);
+        repoList.setOriginalJson(json);
+        return repoList;
+    }
+
+    private static String execute(BorgRepoConfig repoConfig, String command, String... args) {
+        CommandLine cmdLine = new CommandLine(ConfigurationHandler.getConfiguration().getBorgCommand());
+        cmdLine.addArgument(command);
+        for (String arg : args) {
+            cmdLine.addArgument(arg);
+        }
+        cmdLine.addArgument(repoConfig.getRepo());
+        DefaultExecutor executor = new DefaultExecutor();
+        //executor.setExitValue(2);
+        ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
+        executor.setWatchdog(watchdog);
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        ExecuteResultHandler handler = new DefaultExecuteResultHandler();
+        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
+        executor.setStreamHandler(streamHandler);
+        String borgCall = cmdLine.getExecutable() + " " + StringUtils.join(cmdLine.getArguments(), " ");
+        log.info("Executing '" + borgCall + "'...");
+        try {
+            executor.execute(cmdLine, getEnvironment(repoConfig));
+        } catch (IOException ex) {
+            log.error("Error while creating environment for borg call '" + borgCall + "': " + ex.getMessage(), ex);
+        }
+        String json = outputStream.toString(Charset.forName("UTF-8"));
+        return json;
+    }
+
+    private static Map<String, String> getEnvironment(BorgRepoConfig repoConfig) throws IOException {
         Configuration config = ConfigurationHandler.getConfiguration();
         Map<String, String> env = EnvironmentUtils.getProcEnvironment();
         addEnvironmentVariable(env, "BORG_REPO", repoConfig.getRepo());

--
Gitblit v1.10.0