Skip to content

Commit

Permalink
feat: add dotenv (#8)
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
bhargavnova authored Oct 4, 2023
1 parent bb86408 commit 213df75
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions codeqai/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,43 @@
from codeqai.llm import LLM
from codeqai.vector_store import VectorStore

from dotenv import dotenv_values

def env_loader(env_path, required_keys=None):
"""
Args :
env_path = source path of .env file.
required_keys = ["OPENAI_KEY"] #change this according to need
#running/calling the function.
configs = env_loader('.env', required_keys)
"""

#create env file if does not exists
#parse required keys in the file if it's not None
if not os.path.exists(env_path):
with open(env_path, 'w') as env_f:
if required_keys:
for key in required_keys:
env_f.write(f'{key}=""\n')
else:
pass

configs = dotenv_values(env_path)
changed = False
for key, value in configs.items():
if not value:
value = input(f'[+] Key {key} is required enter it\'s value :: > ')
configs[key] = value
changed = True

#update the .env file if config is changed/taken from user
if changed:
with open(env_path, 'w') as env_f:
for key, value in configs.items():
env_f.write(f'{key}="{value}"\n')

return configs

def run():
parser = argparse.ArgumentParser()
Expand Down

0 comments on commit 213df75

Please sign in to comment.