Selaa lähdekoodia

Add an exec command

jherve 1 kuukausi sitten
vanhempi
commit
7aa5d4532b
1 muutettua tiedostoa jossa 23 lisäystä ja 6 poistoa
  1. 23 6
      pc_backup/cli.py

+ 23 - 6
pc_backup/cli.py

@@ -1,4 +1,5 @@
 import argparse
+from argparse import Namespace
 import sys
 from pathlib import Path
 
@@ -10,12 +11,18 @@ from pc_backup.secret import Secret
 class CliArguments:
     @staticmethod
     def read_command(parser):
-        args = parser.parse_args()
-
-        if hasattr(args, "command"):
-            return args.command(args)
-        else:
-            return parser.error("You should call at least one of the commands")
+        match parser.parse_known_args():
+            case (Namespace(command=command) as args, extra_args) if (
+                command == CommandExec
+            ):
+                args.extra = extra_args
+                return command(args)
+            case (Namespace(command=_) as args, []):
+                return args.command(args)
+            case (Namespace(command=_), extra_args):
+                return parser.error("You passed extra arguments")
+            case _:
+                return parser.error("You should call at least one of the commands")
 
     @staticmethod
     def new() -> argparse.ArgumentParser:
@@ -25,6 +32,7 @@ class CliArguments:
         for sub in [
             CommandStart,
             CommandRm,
+            CommandExec,
             CommandBash,
             CommandCreateRepo,
             CommandExportKey,
@@ -69,6 +77,15 @@ class CommandRm(Command):
         container.rm()
 
 
+class CommandExec(Command):
+    command = "exec"
+    help = "run command in container"
+    env_vars = ["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
+
+    def run(self, *, container: BorgmaticContainer, **kwargs):
+        container.exec(self.extra, self.env_vars)
+
+
 class CommandBash(Command):
     command = "bash"
     help = "run shell in container"