-
Notifications
You must be signed in to change notification settings - Fork 0
feat: #5 add .env config file support #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9c13f94
chore: #5 add dotenv dependency for .env config file support
TheRealAgentK 2dd4546
feat: #5 add ConfigFile loader with .env discovery
TheRealAgentK 4a35955
feat: #5 read from .env config file as third resolution tier
TheRealAgentK ba593c9
feat: #5 add --config-file global flag and initialize ConfigFile in main
TheRealAgentK 5864fb5
test: #5 add comprehensive tests for ConfigFile (discovery, parsing, β¦
TheRealAgentK 0f3e8fe
test: #5 add ConfigProp .env precedence and missing-source failure tests
TheRealAgentK dc89afb
style: #5 apply dart format to new config-file files
TheRealAgentK 1899dc0
docs: #5 document .env config file support in README and AGENT.md
TheRealAgentK b62d4a0
test: #5 add E2E tests covering happy-path .env discovery and precedence
TheRealAgentK 139bcd7
test: add tests for Environment singleton and operator []
TheRealAgentK bee3c78
test: add tests for RaygunMultipartRequestBuilder and RaygunPostRequeβ¦
TheRealAgentK 0252727
fix: #5 treat empty/whitespace config values as missing and add verboβ¦
TheRealAgentK 1970e6a
fix: #5 cap .env discovery to CWD when HOME/USERPROFILE is unset
TheRealAgentK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Raygun CLI configuration | ||
| # | ||
| # Copy this file to `.env` in your project root (or any parent directory) | ||
| # and fill in your real credentials. raygun-cli will load it automatically. | ||
| # | ||
| # IMPORTANT: add `.env` to your .gitignore β it usually contains secrets. | ||
|
|
||
| # The Application ID in Raygun.com (e.g. "abc123"). | ||
| RAYGUN_APP_ID= | ||
|
|
||
| # A Personal Access Token from https://app.raygun.com/user/tokens | ||
| RAYGUN_TOKEN= | ||
|
|
||
| # The API key of your application in Raygun.com | ||
| RAYGUN_API_KEY= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:dotenv/dotenv.dart'; | ||
| import 'package:path/path.dart' as p; | ||
|
|
||
| /// Wraps access to a `.env` config file. | ||
| /// | ||
| /// Lookup order during discovery: | ||
| /// 1. An explicit path provided via `--config-file=<path>`. | ||
| /// 2. A `.env` in the current working directory. | ||
| /// 3. A `.env` in any parent directory, walking up to (but not past) the | ||
| /// user's home directory (`$HOME` / `%USERPROFILE%`). | ||
| /// | ||
| /// When neither `$HOME` nor `%USERPROFILE%` is set (e.g. in sandboxed CI | ||
| /// runners or minimal containers), discovery is restricted to the current | ||
| /// working directory only β we deliberately do not walk to the filesystem | ||
| /// root, which could otherwise pick up a stray `/.env` or `/etc/.env`. | ||
| /// Use `--config-file=<path>` for explicit control in those environments. | ||
| /// | ||
| /// If no file is found, an empty instance is returned (silent no-op). | ||
| /// | ||
| /// Allows faking via [setInstance] for testing. | ||
| class ConfigFile { | ||
| static const String fileName = '.env'; | ||
|
|
||
| static ConfigFile? _instance; | ||
|
|
||
| final DotEnv _env; | ||
|
|
||
| /// The absolute path to the loaded `.env`, or `null` if no file was loaded. | ||
| final String? path; | ||
|
|
||
| ConfigFile._(this._env, this.path); | ||
|
|
||
| /// Empty instance used when no `.env` was found. | ||
| factory ConfigFile.empty() => ConfigFile._(DotEnv(quiet: true), null); | ||
|
|
||
| /// Singleton instance access. | ||
| /// | ||
| /// Will perform discovery if not already initialized. To override discovery | ||
| /// (e.g. via `--config-file`), call [setInstance] before accessing. | ||
| static ConfigFile get instance => _instance ??= load(); | ||
|
|
||
| /// For testing or to inject a pre-loaded instance. | ||
| static void setInstance(ConfigFile instance) { | ||
| _instance = instance; | ||
| } | ||
|
|
||
| /// For testing: clear the singleton so the next [instance] access re-discovers. | ||
| static void resetForTest() { | ||
| _instance = null; | ||
| } | ||
|
|
||
| /// Read a value for [key]. Returns `null` if not present. | ||
| String? operator [](String key) => _env[key]; | ||
|
TheRealAgentK marked this conversation as resolved.
|
||
|
|
||
| /// Discover and load a `.env` file. | ||
| /// | ||
| /// - [explicitPath]: if non-null, load that file directly. If the file does | ||
| /// not exist, prints an error and exits with code 2. | ||
| /// - [startDir]: directory to start searching from (defaults to CWD). | ||
| /// - [stopDir]: directory to stop searching at, inclusive (defaults to the | ||
| /// user's home directory). When `null` and `HOME`/`USERPROFILE` are also | ||
| /// unset, discovery is restricted to [startDir] only β no walking up. | ||
| /// - [environmentOverride]: optional map used in place of | ||
| /// `Platform.environment` for resolving `HOME`/`USERPROFILE`. Intended | ||
| /// for tests; production callers should leave this `null`. | ||
| /// - [verbose]: if true, prints the path of the loaded file. | ||
| static ConfigFile load({ | ||
| String? explicitPath, | ||
| String? startDir, | ||
| String? stopDir, | ||
| Map<String, String>? environmentOverride, | ||
| bool verbose = false, | ||
| }) { | ||
| if (explicitPath != null) { | ||
| final file = File(explicitPath); | ||
| if (!file.existsSync()) { | ||
| print('Error: --config-file points to a missing file: $explicitPath'); | ||
| exit(2); | ||
| } | ||
| final env = DotEnv(quiet: true)..load([file.absolute.path]); | ||
| if (verbose) print('Loaded config from ${file.absolute.path}'); | ||
| return ConfigFile._(env, file.absolute.path); | ||
| } | ||
|
|
||
| final effectiveStartDir = startDir ?? Directory.current.path; | ||
| final effectiveStopDir = stopDir ?? _defaultStopDir(environmentOverride); | ||
|
|
||
| final String? discovered; | ||
| if (effectiveStopDir == null) { | ||
| // No home boundary available β only check the CWD. Walking up to the | ||
| // filesystem root would risk silently picking up a stray /.env or | ||
| // /etc/.env in sandboxed CI runners. | ||
| if (verbose) { | ||
| print( | ||
| '[VERBOSE] HOME/USERPROFILE not set; restricting .env discovery ' | ||
| 'to current directory', | ||
| ); | ||
| } | ||
| discovered = _checkSingleDir(effectiveStartDir); | ||
| } else { | ||
| discovered = _discover( | ||
| startDir: effectiveStartDir, | ||
| stopDir: effectiveStopDir, | ||
| ); | ||
| } | ||
|
|
||
| if (discovered == null) return ConfigFile.empty(); | ||
|
|
||
| final env = DotEnv(quiet: true)..load([discovered]); | ||
| if (verbose) print('Loaded config from $discovered'); | ||
| return ConfigFile._(env, discovered); | ||
| } | ||
|
|
||
| /// Returns the absolute path to a `.env` directly in [dir], or null. | ||
| /// Used when no upward walk is performed. | ||
| static String? _checkSingleDir(String dir) { | ||
| final candidate = File(p.join(p.absolute(dir), fileName)); | ||
| return candidate.existsSync() ? candidate.absolute.path : null; | ||
| } | ||
|
|
||
| /// Walk up from [startDir] looking for [fileName]. Stops once the parent | ||
| /// of the current directory is outside [stopDir]'s subtree, or once the | ||
| /// filesystem root is reached. | ||
| static String? _discover({ | ||
| required String startDir, | ||
| required String stopDir, | ||
| }) { | ||
| final stopAbsolute = p.absolute(stopDir); | ||
| var dir = Directory(p.absolute(startDir)); | ||
|
|
||
| while (true) { | ||
| final candidate = File(p.join(dir.path, fileName)); | ||
| if (candidate.existsSync()) { | ||
| return candidate.absolute.path; | ||
| } | ||
|
|
||
| // Stop if we've reached the configured boundary. | ||
| if (p.equals(dir.path, stopAbsolute)) return null; | ||
|
|
||
| final parent = dir.parent; | ||
| // Reached filesystem root. | ||
| if (p.equals(parent.path, dir.path)) return null; | ||
|
|
||
| // Don't escape the stopDir's subtree. | ||
| if (!p.isWithin(stopAbsolute, parent.path) && | ||
| !p.equals(parent.path, stopAbsolute)) { | ||
| return null; | ||
| } | ||
|
|
||
| dir = parent; | ||
| } | ||
| } | ||
|
|
||
| /// Default boundary for upward discovery: the user's home directory. | ||
| /// Returns `null` if neither `HOME` nor `USERPROFILE` is set, in which | ||
| /// case [load] will restrict discovery to the start directory only. | ||
| /// | ||
| /// [environmentOverride] is for tests; when `null`, reads from | ||
| /// `Platform.environment`. | ||
| static String? _defaultStopDir([Map<String, String>? environmentOverride]) { | ||
| final src = environmentOverride ?? Platform.environment; | ||
| final home = src['HOME'] ?? src['USERPROFILE']; | ||
| return (home != null && home.isNotEmpty) ? home : null; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.