Check for missing environment variables

Checks your .env and matches it to the .env.template to find missing variables.
pull/553/head
Owen Gaspard 3 years ago
parent 365ebef0d3
commit d7aa5d0cfb

3
.gitignore vendored

@ -169,4 +169,5 @@ results/*
reddit-bot-351418-5560ebc49cac.json
/.idea
*.pyc
/video_creation/data/videos.json
/video_creation/data/videos.json
utils/envUpdate.py.old

@ -6,6 +6,7 @@ from os import getenv, name
from reddit.subreddit import get_subreddit_threads
from utils.cleanup import cleanup
from utils.console import print_markdown, print_step
from utils.envUpdate import checkUpdate
from video_creation.background import download_background, chop_background_video
from video_creation.final_video import make_final_video
from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts
@ -22,6 +23,7 @@ print(
"""
)
load_dotenv()
envUpdate()
# Modified by JasonLovesDoggo
print_markdown(
"### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue. You can find solutions to many common problems in the [Documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/)"
@ -37,6 +39,7 @@ reddit2fa = getenv("REDDIT_2FA")
def main():
checkUpdate()
cleanup()
def get_obj():

@ -0,0 +1,37 @@
import os
import subprocess
import tempfile
from os import path
import logging
log = logging.getLogger(__name__)
def envUpdate():
if path.exists(".env.template"):
envTemplate = subprocess.check_output(
"awk -F '=' 'NF {print $1}' .env.template | grep --regexp=^[a-zA-Z]", # noqa
shell=True,
)
tempEnv = tempfile.TemporaryFile()
tempEnv.write(envTemplate)
tempEnv.seek(0)
envVars = tempEnv.readlines()
missing = []
isMissingEnvs = False
for env in envVars:
try:
env = env.decode("utf-8").strip()
except AttributeError:
env = env.strip()
if env not in os.environ:
isMissingEnvs = True
missing.append(env)
if isMissingEnvs:
log.error(
f"[ERROR] The following environment variables are missing: {missing}.)"
)
exit(-1)
Loading…
Cancel
Save