xaaha / hulak

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parsing `.env` files

xaaha opened this issue · 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:

  1. Environment File Parsing: Implement functionality to accurately parse .env files, supporting variables without quotes, or with quotes for string values. This includes interpreting lines like PASSWORD=mypassword and USERNAME="randomName" correctly, and handling edge cases such as spaces around the = sign.

  2. 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.

  3. 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. Variable Error Resolution: ✅

    • Throw clear errors if the variable is not found.
  1. 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.

  1. 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.
  2. 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.
  3. Implement Variable Reference Resolution: ✅

    • envparser.SubstituteVariable is used to replace the variable in the string surrounded with {{keyName}}
  4. Environment Switching Capability: Environment can be switched with -env flag. ✅

  5. Default to GLOBAL Environment: ✅

  6. Testing and Validation: ✅

  7. 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
image

this completes the parsing mechanics functionality. Other issues will be completed in the next ticket