ソースを参照

Add a run function for each command type

jherve 1 ヶ月 前
コミット
38d12325fa
1 ファイル変更23 行追加23 行削除
  1. 23 23
      start.py

+ 23 - 23
start.py

@@ -226,31 +226,48 @@ class CliArgumentsStart(CliArguments):
     command = "start"
     help = "start container"
 
+    def run(self, *, container: BorgmaticContainer, data_sources: list[Path], secret_sources: list[Secret], **kwargs):
+        container.run(data_sources, secret_sources)
 
 class CliArgumentsRm(CliArguments):
     command = "rm"
     help = "remove container"
 
+    def run(self, *, container: BorgmaticContainer, **kwargs):
+        container.rm()
 
 class CliArgumentsBash(CliArguments):
     command = "bash"
     help = "run shell in container"
 
+    def run(self, *, container: BorgmaticContainer, **kwargs):
+        container.exec(["bash"])
 
 class CliArgumentsCreateRepo(CliArguments):
     command = "create_repo"
     help = "create repository"
+    env_vars=["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
 
+    def run(self, *, container: BorgmaticContainer, **kwargs):
+        container.exec(["borgmatic", "repo-create", "--encryption", "repokey"], self.env_vars)
 
 class CliArgumentsExportKey(CliArguments):
     command = "export_key"
     help = "export the repository key"
+    env_vars=["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
 
+    def run(self, *, container: BorgmaticContainer, **kwargs):
+        container.exec(["borgmatic", "export", "key"], self.env_vars)
 
 class CliArgumentsCreateSecrets(CliArguments):
     command="create_secrets"
     help="create podman secrets"
 
+    def run(self, *, secret_sources: list[Secret], **kwargs):
+        keepass = KeePass.new(self.keepass_path)
+        for s in secret_sources:
+            s.create(keepass)
+
     @classmethod
     def init_subparser(cls, p):
         p.add_argument("keepass_path", type=Path, help="Path to the keepass")
@@ -263,34 +280,17 @@ def main():
     secret_sources = Secret.read_sources(hostname, login)
     data_sources = read_data_sources(hostname, login)
 
-    args = CliArguments.new()
-
     if not secret_sources:
         print("no secret required ?")
 
     container = BorgmaticContainer.new(hostname, login)
-    env_vars = ["BORG_PASSPHRASE_NAME", "STORAGE_BOX_USER", "SSH_KEY_NAME"]
-
-    if isinstance(args, CliArgumentsCreateSecrets):
-        keepass = KeePass.new(args.keepass_path)
-        for s in secret_sources:
-            s.create(keepass)
-
-    elif isinstance(args, CliArgumentsStart):
-        container.run(data_sources, secret_sources)
-
-    elif isinstance(args, CliArgumentsRm):
-        container.rm()
-
-    elif isinstance(args, CliArgumentsBash):
-        container.exec(["bash"])
-
-    elif isinstance(args, CliArgumentsCreateRepo):
-        container.exec(["borgmatic", "repo-create", "--encryption", "repokey"], env_vars)
-
-    elif isinstance(args, CliArgumentsExportKey):
-        container.exec(["borgmatic", "export", "key"], env_vars)
 
+    args = CliArguments.new()
+    args.run(
+        secret_sources=secret_sources,
+        data_sources=data_sources,
+        container=container,
+    )
 
 if __name__ == "__main__":
     main()