-
Notifications
You must be signed in to change notification settings - Fork 13
Add .env integration #106
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
base: master
Are you sure you want to change the base?
Add .env integration #106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
namespace ThunderstoreCLI.Configuration; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public static class DotEnv | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public static void Load(string filePath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!File.Exists(filePath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach (var line in File.ReadAllLines(filePath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith('#')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var parts = line.Split('=', 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (parts.Length != 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var key = parts[0].Trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var value = parts[1].Trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+12
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Harden parsing: support “export”, skip empty keys, handle inline comments. Prevents malformed entries from polluting the env and aligns with common dotenv behavior. - var parts = line.Split('=', 2);
+ var parts = line.Split('=', 2);
if (parts.Length != 2)
continue;
- var key = parts[0].Trim();
- var value = parts[1].Trim();
+ var key = parts[0].Trim();
+ if (key.StartsWith("export ", StringComparison.Ordinal))
+ key = key.Substring(7).Trim();
+ if (string.IsNullOrWhiteSpace(key))
+ continue;
+
+ var value = parts[1].Trim();
+ // Remove inline comments for unquoted values: KEY=val # comment
+ if (!(value.StartsWith("\"") || value.StartsWith("'")))
+ {
+ var hashIdx = value.IndexOf('#');
+ if (hashIdx >= 0)
+ value = value.Substring(0, hashIdx).TrimEnd();
+ } 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (value.Length >= 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if ((value.StartsWith('"') && value.EndsWith('"')) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(value.StartsWith('\'') && value.EndsWith('\''))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value = value.Substring(1, value.Length - 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Environment.SetEnvironmentVariable(key, value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+22
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Don’t override existing OS environment variables by default. Standard dotenv practice: keep OS-provided vars; only fill missing. - if (value.Length >= 2)
+ if (value.Length >= 2)
{
if ((value.StartsWith('"') && value.EndsWith('"')) ||
(value.StartsWith('\'') && value.EndsWith('\'')))
{
value = value.Substring(1, value.Length - 2);
}
}
- Environment.SetEnvironmentVariable(key, value);
+ if (Environment.GetEnvironmentVariable(key) is null)
+ {
+ Environment.SetEnvironmentVariable(key, value);
+ } 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public static void LoadAll(string? environment = null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var filesToLoad = new List<string> { ".env" }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!string.IsNullOrEmpty(environment)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filesToLoad.Add($".env.{environment}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filesToLoad.Add($".env.{environment.ToLowerInvariant()}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filesToLoad.Add($".{environment}.env"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filesToLoad.Add($".{environment.ToLowerInvariant()}.env"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+39
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sanitize environment name to avoid path traversal and weird filenames. An attacker-controlled ENVIRONMENT could inject path separators. Allow only [A-Za-z0-9_-]. - if (!string.IsNullOrEmpty(environment))
+ if (!string.IsNullOrEmpty(environment))
{
- filesToLoad.Add($".env.{environment}");
- filesToLoad.Add($".env.{environment.ToLowerInvariant()}");
- filesToLoad.Add($".{environment}.env");
- filesToLoad.Add($".{environment.ToLowerInvariant()}.env");
+ var envSafe = new string(environment.Where(ch => char.IsLetterOrDigit(ch) || ch is '-' or '_').ToArray());
+ if (!string.IsNullOrEmpty(envSafe))
+ {
+ filesToLoad.Add($".env.{envSafe}");
+ filesToLoad.Add($".env.{envSafe.ToLowerInvariant()}");
+ filesToLoad.Add($".{envSafe}.env");
+ filesToLoad.Add($".{envSafe.ToLowerInvariant()}.env");
+ }
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filesToLoad.Add(".env.local"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filesToLoad.Add(".local.env"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
foreach (var file in filesToLoad) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Load(file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Load base .env before resolving environment; then load env-specific files.
Allows ENVIRONMENT/DOTNET_ENVIRONMENT values set in .env to influence which files are loaded next. Safe with the “don’t override existing vars” change.
📝 Committable suggestion
🤖 Prompt for AI Agents