Browse Source

small refactor

Michaël Lemaire 4 months ago
parent
commit
7de367405f
1 changed files with 30 additions and 21 deletions
  1. 30 21
      cli.ts

+ 30 - 21
cli.ts

@@ -135,31 +135,24 @@ async function backupToRemote(config: RunConfig): Promise<void> {
   const borgpath = `ssh://${config.remote.host}${destpath}`;
 
   try {
-    await localRun(
-      ["borg", "check", borgpath],
-      "/tmp",
-      { BORG_PASSPHRASE: config.remote.passphrase },
-    );
+    await borgRun(["check", borgpath], config.remote);
   } catch {
-    await localRun(
-      ["borg", "init", "--encryption=repokey", "--make-parent-dirs", borgpath],
-      "/tmp",
-      { BORG_PASSPHRASE: config.remote.passphrase },
+    await borgRun(
+      ["init", "--encryption=repokey", "--make-parent-dirs", borgpath],
+      config.remote,
     );
   }
 
-  const command: string[] = [
-    "borg",
-    "create",
-    `${borgpath}::{now}`,
-  ];
-  for (let [name, directory] of Object.entries(config.program.saves)) {
-    command.push(makeAbsolute(directory, config.program.directory));
-  }
-  await localRun(
-    command,
-    "/tmp",
-    { BORG_PASSPHRASE: config.remote.passphrase },
+  await borgRun(
+    [
+      "create",
+      `${borgpath}::{now}`,
+    ].concat(
+      Object.values(config.program.saves).map((dir) =>
+        makeAbsolute(dir, config.program.directory)
+      ),
+    ),
+    config.remote,
   );
 }
 
@@ -177,6 +170,22 @@ export function makeAbsolute(dir: string, cwd: string): string {
   }
 }
 
+/**
+ * Run a borg command
+ */
+async function borgRun(
+  cmd: string[],
+  remote: RemoteConfig,
+  allow_fail = false,
+): Promise<void> {
+  await localRun(
+    ["borg"].concat(cmd),
+    "/tmp",
+    { BORG_PASSPHRASE: remote.passphrase },
+    allow_fail,
+  );
+}
+
 /**
  * Run a local program
  */