From 8efdf5cd43eaee197a3b3851763fdbf950f95433 Mon Sep 17 00:00:00 2001 From: "flyingscorpio@arch-desktop" Date: Tue, 23 Feb 2021 13:29:30 +0100 Subject: [PATCH] Move verify_secrets() out of class, verify secrets before intantiating --- .talismanrc | 2 ++ install.py | 96 +++++++++++++++++++++++++++-------------------------- 2 files changed, 51 insertions(+), 47 deletions(-) diff --git a/.talismanrc b/.talismanrc index 64979bb..ddc716b 100644 --- a/.talismanrc +++ b/.talismanrc @@ -19,6 +19,8 @@ fileignoreconfig: - filename: dotfiles/zsh/zshrc ignore_detectors: - filename +- filename: install.py + checksum: f1432d0fcb920e0c6b1aa8fddf0f657aa8af5a67175b42dbc941298f16e5b4a9 - filename: pacman_hooks/borg_backup.hook ignore_detectors: - filename diff --git a/install.py b/install.py index a84882e..39cd7c2 100755 --- a/install.py +++ b/install.py @@ -26,7 +26,6 @@ class Installer: hide_commands: bool = False, update: bool = False, ): - self.verify_secrets() self.base_dir = Path.getcwd() yaml = YAML(typ="safe") self.conf = yaml.load(Path(config).text()) @@ -36,52 +35,6 @@ class Installer: self.operating_system = self.define_os() self.update = update - def verify_secrets(self) -> None: - """The repository contains a secrets.template, that must be taken care of - by the user. If the secrets is wrong, or missing, fail. - """ - - try: - with open("secrets.template", "r") as template_file: - template_content = [ - line.strip() - for line in template_file.read().split("\n\n") - if not line.startswith("#") and line.strip() - ] - except FileNotFoundError: - print("No 'secrets.template' file found. Did you delete it?") - sys.exit(1) - try: - with open("secrets", "r") as secrets_file: - secrets_content = [ - line.strip() - for line in secrets_file.readlines() - if not line.startswith("#") and line.strip() - ] - except FileNotFoundError: - print("No 'secrets' file found. Did you forget to create it?") - sys.exit(1) - - template_keys = [line.split("=")[0] for line in template_content if "=" in line] - secrets_keys = [line.split("=")[0] for line in secrets_content if "=" in line] - - # Check that the template file and secrets file have the same keys - if template_keys != secrets_keys: - print( - "'secrets.template' and 'secrets' don't have the same keys.\n" - "Perhaps you have forgotten to add some?" - ) - sys.exit(1) - - secrets_values = [line.split("=")[1] for line in secrets_content if "=" in line] - - # Check that each key has a value that has been set: - for i, value in enumerate(secrets_values): - if not value: - key = secrets_keys[i] - print(f" 'secrets' file has no value for {key}, please add one.") - sys.exit(1) - def define_os(self) -> str: """Define what OS we are using.""" @@ -457,6 +410,53 @@ def read_programs_from_process_list() -> List[str]: return programs +def verify_secrets() -> None: + """The repository contains a secrets.template, that must be taken care of + by the user. If the secrets is wrong, or missing, fail. + """ + + try: + with open("secrets.template", "r") as template_file: + template_content = [ + line.strip() + for line in template_file.read().split("\n\n") + if not line.startswith("#") and line.strip() + ] + except FileNotFoundError: + print("No 'secrets.template' file found. Did you delete it?") + sys.exit(1) + try: + with open("secrets", "r") as secrets_file: + secrets_content = [ + line.strip() + for line in secrets_file.readlines() + if not line.startswith("#") and line.strip() + ] + except FileNotFoundError: + print("No 'secrets' file found. Did you forget to create it?") + sys.exit(1) + + template_keys = [line.split("=")[0] for line in template_content if "=" in line] + secrets_keys = [line.split("=")[0] for line in secrets_content if "=" in line] + + # Check that the template file and secrets file have the same keys + if template_keys != secrets_keys: + print( + "'secrets.template' and 'secrets' don't have the same keys.\n" + "Perhaps you have forgotten to add some?" + ) + sys.exit(1) + + secrets_values = [line.split("=")[1] for line in secrets_content if "=" in line] + + # Check that each key has a value that has been set: + for i, value in enumerate(secrets_values): + if not value: + key = secrets_keys[i] + print(f" 'secrets' file has no value for {key}, please add one.") + sys.exit(1) + + def main() -> None: """Parse args and instantiate the Installer.""" @@ -524,6 +524,8 @@ def main() -> None: process_list = args.process_list update = args.update + verify_secrets() + installer = Installer( config=config, force=force,