| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import os
- from tabulate import tabulate
- def find_env_files(directory):
- """Traverse the directory and find all .env files."""
- env_files = []
- for root, _, files in os.walk(directory):
- for file in files:
- if file.endswith('.env'):
- env_files.append(os.path.join(root, file))
- return env_files
- def read_env_file(file_path):
- """Read the contents of an .env file and return a list of key-value pairs."""
- key_value_pairs = []
- with open(file_path, 'r') as file:
- for line in file:
- line = line.strip()
- if line and not line.startswith('#') and '=' in line:
- key, value = line.split('=', 1)
- key_value_pairs.append((key.strip(), value.strip()))
- return key_value_pairs
- def create_table(env_files, root_directory):
- """Create a table with .env file name, environment key, and values."""
- table = []
- for env_file in env_files:
- key_value_pairs = read_env_file(env_file)
- directory_name = os.path.dirname(os.path.relpath(env_file, start=root_directory))
- file_name = os.path.basename(env_file)
- for key, value in key_value_pairs:
- table.append([directory_name, file_name, key, value])
- return table
- def main(directory):
- """Main function to find .env files, read their contents, and display the table."""
- env_files = find_env_files(directory)
- if not env_files:
- print("No .env files found.")
- return
-
- table = create_table(env_files, directory)
- headers = ["Directory", ".env File Name", "Environment Key", "Value"]
- print(tabulate(table, headers=headers, tablefmt="grid"))
- if __name__ == "__main__":
- directory_to_search = input("Enter the directory to search for .env files: ")
- main(directory_to_search)
|