Move verify_secrets() out of class, verify secrets before intantiating
This commit is contained in:
parent
095db59c7e
commit
8efdf5cd43
2 changed files with 51 additions and 47 deletions
|
@ -19,6 +19,8 @@ fileignoreconfig:
|
||||||
- filename: dotfiles/zsh/zshrc
|
- filename: dotfiles/zsh/zshrc
|
||||||
ignore_detectors:
|
ignore_detectors:
|
||||||
- filename
|
- filename
|
||||||
|
- filename: install.py
|
||||||
|
checksum: f1432d0fcb920e0c6b1aa8fddf0f657aa8af5a67175b42dbc941298f16e5b4a9
|
||||||
- filename: pacman_hooks/borg_backup.hook
|
- filename: pacman_hooks/borg_backup.hook
|
||||||
ignore_detectors:
|
ignore_detectors:
|
||||||
- filename
|
- filename
|
||||||
|
|
96
install.py
96
install.py
|
@ -26,7 +26,6 @@ class Installer:
|
||||||
hide_commands: bool = False,
|
hide_commands: bool = False,
|
||||||
update: bool = False,
|
update: bool = False,
|
||||||
):
|
):
|
||||||
self.verify_secrets()
|
|
||||||
self.base_dir = Path.getcwd()
|
self.base_dir = Path.getcwd()
|
||||||
yaml = YAML(typ="safe")
|
yaml = YAML(typ="safe")
|
||||||
self.conf = yaml.load(Path(config).text())
|
self.conf = yaml.load(Path(config).text())
|
||||||
|
@ -36,52 +35,6 @@ class Installer:
|
||||||
self.operating_system = self.define_os()
|
self.operating_system = self.define_os()
|
||||||
self.update = update
|
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:
|
def define_os(self) -> str:
|
||||||
"""Define what OS we are using."""
|
"""Define what OS we are using."""
|
||||||
|
|
||||||
|
@ -457,6 +410,53 @@ def read_programs_from_process_list() -> List[str]:
|
||||||
return programs
|
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:
|
def main() -> None:
|
||||||
"""Parse args and instantiate the Installer."""
|
"""Parse args and instantiate the Installer."""
|
||||||
|
|
||||||
|
@ -524,6 +524,8 @@ def main() -> None:
|
||||||
process_list = args.process_list
|
process_list = args.process_list
|
||||||
update = args.update
|
update = args.update
|
||||||
|
|
||||||
|
verify_secrets()
|
||||||
|
|
||||||
installer = Installer(
|
installer = Installer(
|
||||||
config=config,
|
config=config,
|
||||||
force=force,
|
force=force,
|
||||||
|
|
Loading…
Add table
Reference in a new issue