Parsing `.env` files
xaaha opened this issue · 3 comments
Work Summary: Parsing .env
Files and Variable Setup
The task focuses on enabling the application to read and parse environment variable files (.env
), including the ability to manage and switch between different environment contexts (e.g., GLOBAL and collection-specific environments). The goal is to facilitate a flexible and dynamic configuration system that allows users to define variables within .env
files and reference these variables within their application, thereby tailoring their API testing environment to specific needs.
Objectives:
-
Environment File Parsing: Implement functionality to accurately parse
.env
files, supporting variables without quotes, or with quotes for string values. This includes interpreting lines likePASSWORD=mypassword
andUSERNAME="randomName"
correctly, and handling edge cases such as spaces around the=
sign. -
Variable Reference Resolution: Develop a mechanism to replace variable references within the application (e.g.,
{{PASSWORD}}
) with their corresponding values from the active environment. This involves scanning input strings for variable patterns and substituting them with the actual values stored in memory. -
Environment Context Management: Create a system that allows users to switch between different environment contexts. By default, variables from the GLOBAL environment (
env/.env.global
) should be accessible, but users should be able to switch to collection-specific environments as needed.
Tasks to Complete:
-
Parse
.env
Files: ✅- Write a parser that reads
.env
files line by line, extracting key-value pairs. - Ensure the parser can handle both quoted and unquoted values, stripping quotes where necessary.
- Write a parser that reads
-
Store and Manage Environment Variables: ✅
- Implement a storage mechanism (e.g., a map or dictionary) in memory to hold environment variables.
- Include functionality to update this storage with variables from parsed
.env
files.
-
Implement Variable Reference Resolution: ✅
- Develop a function to search for
{{variableName}}
patterns in strings and replace them with the actual variable values from the current active environment.
- Develop a function to search for
-
Environment Switching Capability: ✅
- Enable command-line arguments or commands within the application for users to switch the active environment context.
- Ensure that switching contexts updates the set of available environment variables accordingly.
-
Default to GLOBAL Environment: ✅
- Set the application to use variables from
env/.env.global
by default upon startup. - Allow for these global variables to be overridden by collection-specific variables when a different environment context is activated.
- Set the application to use variables from
-
Testing and Validation: ✅
- Write unit tests for the
.env
file parser to cover various edge cases. - Test the variable reference resolution feature with different input scenarios.
- Verify the environment switching functionality works as expected, with proper fallbacks to the GLOBAL environment.
- Write unit tests for the
-
Variable Error Resolution: ✅
- Throw clear errors if the variable is not found.
- Initially working on manually parsing each
.env
file since I don't want any fluff. But, if this becomes too much of a headache, here are two options.
a. https://github.com/caarlos0/env
b. https://github.com/kelseyhightower/envconfig
Completed Tasks.
-
Parse
.env
Files ✅- Using Scanner to read file line by line.
- Then splitting the line around =.
- Trimming all the empty spaces. So, no empty spaces are supported.
- Currently only string is supported but I did write a function to parse the input string and convert it to either
- Float, int or bool
- Empty lines are not considered a secret.
-
Store and Manage Environment Variables: ✅
- Variables are kept in Map and needs to be called each time the program runs. It's not saved in
os.SetEnv.
- Variables are kept in Map and needs to be called each time the program runs. It's not saved in
-
Implement Variable Reference Resolution: ✅
envparser.SubstituteVariable
is used to replace the variable in the string surrounded with{{keyName}}
-
Environment Switching Capability: Environment can be switched with
-env
flag. ✅ -
Default to GLOBAL Environment: ✅
-
Testing and Validation: ✅
-
Variable Error Resolution: ✅
- If variables are not found, then the error is throws in red color.
Error: unresolved variable
and program exists with error code 1
- If variables are not found, then the error is throws in red color.
this completes the parsing mechanics functionality. Other issues will be completed in the next ticket