Skip to content

Latest commit

 

History

History
160 lines (117 loc) · 6.97 KB

File metadata and controls

160 lines (117 loc) · 6.97 KB

ChatGPT o3

Draft a detailed, step-by-step blueprint for building this project. Then, once you have a solid plan, break it down into small, iterative chunks that build on each other. Look at these chunks and then go another round to break it into small steps. review the results and make sure that the steps are small enough to be implemented safely, but big enough to move the project forward. Iterate until you feel that the steps are right sized for this project.

From here you should have the foundation to provide a series of prompts for a code-generation LLM that will implement each step. Prioritize best practices, and incremental progress, ensuring no big jumps in complexity at any stage. Make sure that each prompt builds on the previous prompts, and ends with wiring things together. There should be no hanging or orphaned code that isn't integrated into a previous step.

Make sure and separate each prompt section. Use markdown. Each prompt should be tagged as text using code tags. The goal is to output prompts, but context, etc is important as well.

Specification: Ruby Script to Sync .env with .env.example (Pre-commit Hook Helper)

Overview

Create a Ruby script that compares .env.example and .env files in a specified directory (defaulting to .). The script detects keys present in .env.example but missing from .env, prompting the user whether to append those missing keys with their default values to .env. Intended to run as a git pre-commit hook helper, it prevents runtime errors from missing environment variables in local development setups.

Functional Requirements

  1. Input and Invocation • Script accepts an optional directory path parameter; defaults to current directory (.). • Supports a command-line flag --auto-accept to skip prompts and add all missing keys automatically. • Supports a command-line flag --quiet to suppress summary output after execution.

  2. File Handling • Reads .env.example and .env files from the specified directory. • If .env does not exist, create a new .env file. • Before modifying .env, create a timestamped backup file in the same directory with format like .env.bak.YYYYMMDDHHMMSS.

  3. Parsing .env.example • Parse .env.example ignoring: • Comment lines (lines starting with #). • Blank lines. • Lines with export keyword. • Keys that are commented out (e.g., #KEY=val). • Extract keys and associated values as plain strings. • Detect and preserve quoting style (", ', or none) from .env.example values.

  4. Parsing .env • Parse .env similarly (ignoring comments, blank lines). • Extract keys and their values as plain strings. • Do not attempt environment variable expansion or complex parsing.

  5. Key Comparison • Identify all keys present in .env.example but missing from .env. • Ignore keys present in .env but with different values; only presence matters.

  6. User Interaction (when not in --auto-accept mode) • For each missing key: • Prompt the user with the key and its default value from .env.example in the format:

Add missing key KEY="value"? [Y/n]:

•	Accept case-insensitive inputs: y, yes, n, no.
•	Re-prompt until a valid response is received.
  1. Adding Missing Keys • Insert missing keys in .env preserving the key order from .env.example. • When inserting: • Append the key and value pair with the quoting style from .env.example. • Add one key-value pair per line in the format KEY=value.

  2. Output • After processing, output a summary list of added keys and their values. • If --quiet flag is provided, suppress the summary. • Provide clear error messages and fail fast if issues arise.

Non-Functional Requirements • Language: Ruby, compatible with Ruby 2.5+. • Dependencies: Use only Ruby standard library unless a gem provides significant simplification. • Performance: Efficient for typical .env file sizes (<100 keys). • Robustness: Fail fast on file permission errors, malformed .env or .env.example lines, or unexpected input. • User Experience: Minimal output except for prompts and summary.

Architecture & Implementation Details

  1. File Reading & Writing • Read files line-by-line to preserve structure. • Use regex to parse key-value pairs, ignoring lines that do not match the pattern. • For backup, copy .env content to .env.bak.YYYYMMDDHHMMSS before modifying.

  2. Data Structures • Use ordered hashes or arrays to store keys and their values for .env.example and .env to maintain ordering. • Store original lines of .env in memory to facilitate insertion of missing keys at the correct positions.

  3. User Prompting • Use standard input/output. • Normalize input by downcasing and trimming whitespace. • Implement loop to enforce valid yes/no responses.

  4. Command-Line Interface • Use Ruby’s OptionParser or manual argument parsing to handle flags and directory argument.

Error Handling Strategy • Check for existence and readability of .env.example before proceeding; exit with an error if missing. • On file write failures or permission errors, output descriptive error and exit. • Detect malformed lines (e.g., lines that don’t match KEY=VALUE pattern) in .env.example and .env and exit with an error. • Invalid user inputs during prompts are handled by re-prompting. • Any unexpected runtime error should print a message and exit non-zero.

Testing Plan

  1. Unit Tests (If implemented as a library) • Parsing of .env and .env.example lines: • Correctly ignore comments, blank lines, export statements. • Extract keys and values with quoting. • Reject malformed lines. • Key comparison logic: • Detect missing keys accurately. • Ignore keys with value differences. • User prompt parsing: • Accept valid yes/no inputs. • Reject invalid inputs. • File backup creation: • Confirm backup filename format and content. • Insertion of missing keys preserving order.

  2. Integration Tests • Run script with .env.example containing various keys and .env missing some keys. • Test user prompt flow by simulating input. • Test --auto-accept mode adds all keys without prompt. • Test behavior when .env does not exist (new file created). • Test error conditions (missing .env.example, unreadable files, malformed lines).

  3. Manual Testing • Run as git pre-commit hook to validate real workflow. • Test edge cases such as: • Empty .env.example. • .env.example with all keys present in .env. • Large .env.example file. • Keys with different quoting styles.

Example Usage

Run script on current directory (interactive prompts)

ruby env_sync.rb

Run script on specified directory, auto-accept all missing keys

ruby env_sync.rb /path/to/project --auto-accept

Run script quietly, no summary output

ruby env_sync.rb --quiet

This specification should provide all the necessary context, rules, and edge cases for a developer to begin implementing the script confidently and correctly.