-
-
Notifications
You must be signed in to change notification settings - Fork 136
feat (cli): implementing environment variable handling #652
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: dev
Are you sure you want to change the base?
Changes from 1 commit
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,70 @@ | ||||||||||||||||||||||||||
| use eyre::Result; | ||||||||||||||||||||||||||
| use std::collections::HashMap; | ||||||||||||||||||||||||||
| use std::path::Path; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// Load environment variables from .env file and shell environment | ||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||
| /// Precedence order (highest to lowest): | ||||||||||||||||||||||||||
| /// 1. Shell environment variables | ||||||||||||||||||||||||||
| /// 2. .env file variables | ||||||||||||||||||||||||||
| pub fn load_env_variables( | ||||||||||||||||||||||||||
| env_file_path: Option<&Path>, | ||||||||||||||||||||||||||
| project_root: &Path, | ||||||||||||||||||||||||||
| ) -> Result<HashMap<String, String>> { | ||||||||||||||||||||||||||
| let mut vars = HashMap::new(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Load from .env file if specified | ||||||||||||||||||||||||||
| if let Some(env_path) = env_file_path { | ||||||||||||||||||||||||||
| let full_path = project_root.join(env_path); | ||||||||||||||||||||||||||
| if full_path.exists() { | ||||||||||||||||||||||||||
| // Parse the .env file with improved handling | ||||||||||||||||||||||||||
| let env_content = std::fs::read_to_string(&full_path)?; | ||||||||||||||||||||||||||
| for (line_num, line) in env_content.lines().enumerate() { | ||||||||||||||||||||||||||
| let trimmed = line.trim(); | ||||||||||||||||||||||||||
| // Skip empty lines and comments | ||||||||||||||||||||||||||
| if trimmed.is_empty() || trimmed.starts_with('#') { | ||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Parse KEY=VALUE pairs (find first = to handle values with = in them) | ||||||||||||||||||||||||||
| if let Some(eq_pos) = trimmed.find('=') { | ||||||||||||||||||||||||||
| let key = trimmed[..eq_pos].trim(); | ||||||||||||||||||||||||||
| let value = trimmed[eq_pos + 1..].trim(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Validate key | ||||||||||||||||||||||||||
| if key.is_empty() { | ||||||||||||||||||||||||||
| eprintln!("Warning: Skipping empty key at line {} in {}", | ||||||||||||||||||||||||||
| line_num + 1, full_path.display()); | ||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Remove surrounding quotes if present (both single and double) | ||||||||||||||||||||||||||
| let value = if (value.starts_with('"') && value.ends_with('"')) | ||||||||||||||||||||||||||
| || (value.starts_with('\'') && value.ends_with('\'')) { | ||||||||||||||||||||||||||
| &value[1..value.len() - 1] | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| value | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
Comment on lines
+42
to
+47
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. logic: quote removal logic could fail on single-character quoted strings due to potential underflow in
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: helix-cli/src/env_utils.rs
Line: 42:47
Comment:
**logic:** quote removal logic could fail on single-character quoted strings due to potential underflow in `value.len() - 1`
```suggestion
let value = if value.len() >= 2 && ((value.starts_with('"') && value.ends_with('"'))
|| (value.starts_with('\'') && value.ends_with('\''))) {
&value[1..value.len() - 1]
} else {
value
};
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| vars.insert(key.to_string(), value.to_string()); | ||||||||||||||||||||||||||
| } else if !trimmed.starts_with("export ") { | ||||||||||||||||||||||||||
| // Skip lines that start with "export " (shell syntax) | ||||||||||||||||||||||||||
| eprintln!("Warning: Skipping malformed line {} in {}: {}", | ||||||||||||||||||||||||||
| line_num + 1, full_path.display(), trimmed); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| eprintln!("Warning: env_file '{}' not found, skipping", full_path.display()); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Add shell environment variables (higher priority - overwrites .env) | ||||||||||||||||||||||||||
| for (key, value) in std::env::vars() { | ||||||||||||||||||||||||||
| // Skip HELIX_ prefixed variables as they're managed internally | ||||||||||||||||||||||||||
| if !key.starts_with("HELIX_") { | ||||||||||||||||||||||||||
| vars.insert(key, value); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Ok(vars) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
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. style: missing newline at end of file Prompt To Fix With AIThis is a comment left during a code review.
Path: helix-cli/src/env_utils.rs
Line: 70:70
Comment:
**style:** missing newline at end of file
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||
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.
style: Missing newline at end of file
Prompt To Fix With AI