Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
script to check variables in env files before commit
import os.path
listvars=[]
listexvars=[]
varsnotlisted=[]
my_path = os.path.abspath(os.path.dirname(__file__))
filevar = os.path.join(my_path,"../../.env")
filevarex = os.path.join(my_path,"../../.env.example")
if not os.path.isfile(filevar):
print("File .env doesn't exist...")
exit(2)
if not os.path.isfile(filevarex):
print("File .env.example doesn't exist...")
exit(2)
with open(filevar, 'r') as fp:
read_lines = fp.readlines()
for var in read_lines:
if not var.strip():
continue
elif '#' in var:
continue
else:
start = step = None
stop = var.find('=')
mycleanvar = var[start:stop:step]
listvars.append(mycleanvar)
with open(filevarex, 'r') as fp:
read_lines = fp.readlines()
for var in read_lines:
if not var.strip():
continue
elif '#' in var:
continue
else:
start = step = None
stop = var.find('=')
mycleanvar = var[start:stop:step]
listexvars.append(mycleanvar)
for var in listvars:
if var not in listexvars:
varsnotlisted.append(var)
if len(varsnotlisted) > 0:
for var in varsnotlisted:
print("The variable {0} is not in var.example...".format(var))
exit(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.