Parcourir la source

Fix missing interactive shell for bash command

jherve il y a 1 mois
Parent
commit
45471b7778
3 fichiers modifiés avec 24 ajouts et 18 suppressions
  1. 1 1
      pc_backup/cli.py
  2. 2 2
      pc_backup/container.py
  3. 21 15
      pc_backup/podman.py

+ 1 - 1
pc_backup/cli.py

@@ -118,7 +118,7 @@ class CommandBash(Command):
     help = "run shell in container"
 
     def run(self, *, container: BorgmaticContainer, **kwargs):
-        container.exec(["bash"])
+        container.exec(["bash"], interactive=True)
 
 
 class CommandCreateRepo(Command):

+ 2 - 2
pc_backup/container.py

@@ -137,8 +137,8 @@ class BorgmaticContainer:
     def rm(self):
         Podman.rm(self.name)
 
-    def exec(self, cmd: list[str]):
-        Podman.exec(self.name, *cmd)
+    def exec(self, cmd: list[str], *, interactive=False):
+        Podman.exec(self.name, *cmd, interactive=interactive)
 
     @staticmethod
     def to_source_path(path: Path):

+ 21 - 15
pc_backup/podman.py

@@ -39,7 +39,7 @@ class Podman:
 
         args += [image]
 
-        cls._call(args)
+        cls._check_output(args)
 
     @classmethod
     def rm(cls, name, *, force=True):
@@ -47,12 +47,18 @@ class Podman:
         if force:
             args += ["-f"]
         args += [name]
-        cls._call(args)
+        cls._check_output(args)
 
     @classmethod
-    def exec(cls, name, *cmd):
-        args = ["exec", "-ti", name] + list(cmd)
-        cls._call(args)
+    def exec(cls, name, /, *cmd, interactive):
+        args = ["exec"]
+        common_suffix = [name] + list(cmd)
+        if interactive:
+            args += ["-ti"] + common_suffix
+            cls._run(args)
+        else:
+            args += common_suffix
+            cls._check_output(args)
 
     @classmethod
     def secret_create(
@@ -80,14 +86,7 @@ class Podman:
         elif host_path is not None:
             args += [host_path]
 
-        cls._call(args, **kwargs)
-
-    @classmethod
-    def _call(cls, args: list, **kwargs):
-        args = ["podman"] + args
-        print(f"Executing `{" ".join(args)}`")
-        out = subprocess.check_output(args, **kwargs)
-        print(out.decode())
+        cls._check_output(args, **kwargs)
 
     @classmethod
     def machine_is_running(cls) -> bool:
@@ -101,10 +100,17 @@ class Podman:
 
     @classmethod
     def machine_start(cls):
-        cls._call(["machine", "start"])
+        cls._check_output(["machine", "start"])
+
+    @classmethod
+    def _run(cls, args: list, **kwargs):
+        args = ["podman"] + args
+        print(f"Executing `{" ".join(args)}`")
+
+        return subprocess.run(args, **kwargs)
 
     @classmethod
-    def _call(cls, args: list, **kwargs):
+    def _check_output(cls, args: list, **kwargs):
         args = ["podman"] + args
         print(f"Executing `{" ".join(args)}`")