cli.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import argparse
  2. from argparse import Namespace
  3. import sys
  4. from pathlib import Path
  5. from pc_backup.container import BorgmaticContainer, Configuration
  6. from pc_backup.keepass import KeePass
  7. from pc_backup.secret import Secret
  8. class CliArguments:
  9. @staticmethod
  10. def read_command(parser):
  11. match parser.parse_known_args():
  12. case (Namespace(command=command) as args, extra_args) if (
  13. command == CommandExec
  14. ):
  15. args.extra = extra_args
  16. return command(args)
  17. case (Namespace(command=_) as args, []):
  18. return args.command(args)
  19. case (Namespace(command=_), extra_args):
  20. return parser.error("You passed extra arguments")
  21. case _:
  22. return parser.error("You should call at least one of the commands")
  23. @staticmethod
  24. def new() -> argparse.ArgumentParser:
  25. parser = argparse.ArgumentParser(prog=sys.argv[0])
  26. subparsers = parser.add_subparsers()
  27. for sub in [
  28. CommandStart,
  29. CommandConfig,
  30. CommandBorgmaticConfig,
  31. CommandRm,
  32. CommandExec,
  33. CommandBash,
  34. CommandCreateRepo,
  35. CommandExportKey,
  36. CommandCreateSecrets,
  37. ]:
  38. p = subparsers.add_parser(sub.command, help=sub.help)
  39. sub.init_subparser(p)
  40. p.set_defaults(command=sub)
  41. return parser
  42. class Command:
  43. def __init__(self, namespace) -> None:
  44. for k, v in vars(namespace).items():
  45. if k != "type_":
  46. setattr(self, k, v)
  47. @classmethod
  48. def init_subparser(cls, p): ...
  49. class CommandStart(Command):
  50. command = "start"
  51. help = "start container"
  52. def run(
  53. self,
  54. *,
  55. container: BorgmaticContainer,
  56. config: Configuration,
  57. **kwargs,
  58. ):
  59. container.run(config)
  60. class CommandConfig(Command):
  61. command = "config"
  62. help = "dump current config"
  63. def run(
  64. self,
  65. *,
  66. config: Configuration,
  67. **kwargs,
  68. ):
  69. print(config)
  70. class CommandBorgmaticConfig(Command):
  71. command = "config_borgmatic"
  72. help = "dump current borgmatic config"
  73. def run(
  74. self,
  75. *,
  76. container: BorgmaticContainer,
  77. **kwargs,
  78. ):
  79. container.exec(["borgmatic", "config", "validate", "-s"])
  80. class CommandRm(Command):
  81. command = "rm"
  82. help = "remove container"
  83. def run(self, *, container: BorgmaticContainer, **kwargs):
  84. container.rm()
  85. class CommandExec(Command):
  86. command = "exec"
  87. help = "run command in container"
  88. def run(self, *, container: BorgmaticContainer, **kwargs):
  89. container.exec(self.extra)
  90. class CommandBash(Command):
  91. command = "bash"
  92. help = "run shell in container"
  93. def run(self, *, container: BorgmaticContainer, **kwargs):
  94. container.exec(["bash"], interactive=True)
  95. class CommandCreateRepo(Command):
  96. command = "create_repo"
  97. help = "create repository"
  98. def run(self, *, container: BorgmaticContainer, **kwargs):
  99. container.exec(["borgmatic", "repo-create", "--encryption", "repokey"])
  100. class CommandExportKey(Command):
  101. command = "export_key"
  102. help = "export the repository key"
  103. def run(self, *, container: BorgmaticContainer, **kwargs):
  104. container.exec(["borgmatic", "export", "key"])
  105. class CommandCreateSecrets(Command):
  106. command = "create_secrets"
  107. help = "create podman secrets"
  108. def run(self, *, secret_sources: list[Secret], **kwargs):
  109. keepass = KeePass.new(self.keepass_path)
  110. for s in secret_sources:
  111. s.create(keepass)
  112. @classmethod
  113. def init_subparser(cls, p):
  114. p.add_argument("keepass_path", type=Path, help="Path to the keepass")