Skip to content

Commit

Permalink
fix: commentaries not being discarted in envfiles
Browse files Browse the repository at this point in the history
Signed-off-by: Lincoln Wallace <[email protected]>
  • Loading branch information
locnnil committed Aug 5, 2024
1 parent a0a9314 commit c2bd475
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
1 change: 1 addition & 0 deletions extensions/env-injector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
dotenv = "0.15.0"
http-body-util = "0.1.2"
hyper = {git = "https://github.com/hyperium/hyper", tag = "v1.4.1"}
hyper-util = "0.1.6"
Expand Down
37 changes: 9 additions & 28 deletions extensions/env-injector/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::error::Error;
use std::process::Command;
use std::collections::HashMap;
use std::path::Path;
use std::io::BufRead;
use dotenv;

const SNAPD_SOCKET: &str = "/run/snapd-snap.socket";

Expand Down Expand Up @@ -64,9 +64,6 @@ fn process_env(env: &serde_json::Value) -> HashMap<String, String> {
map
}




fn set_env_vars(app: &str, json: &serde_json::Value) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let stdout_str = json["result"]["stdout"].as_str().ok_or("Invalid stdout")?;
let stdout_json: serde_json::Value = serde_json::from_str(stdout_str)?;
Expand All @@ -87,7 +84,7 @@ fn set_env_vars(app: &str, json: &serde_json::Value) -> Result<(), Box<dyn std::
Ok(())
}

fn source_env_file(file_path: &str) -> std::io::Result<HashMap<String, String>> {
fn source_env_file(file_path: &str) -> std::io::Result<()> {
let path = Path::new(file_path);

if !path.exists() {
Expand All @@ -100,20 +97,12 @@ fn source_env_file(file_path: &str) -> std::io::Result<HashMap<String, String>>
return Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "File is not readable"));
}

let file = std::fs::File::open(file_path)?;
let reader = std::io::BufReader::new(file);
dotenv::from_path(path).map_err(|e| {
eprintln!("Failed to load environment file: {}", e);
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to load environment file")
})?;

let mut env_vars = HashMap::new();
for line in reader.lines() {
let line = line?;
if !line.trim().is_empty() && !line.starts_with('#') {
if let Some((key, value)) = line.split_once('=') {
env_vars.insert(key.to_string(), value.to_string());
}
}
}

Ok(env_vars)
Ok(())
}

fn set_env_vars_from_file(app: &str, json: &serde_json::Value) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Expand All @@ -123,20 +112,12 @@ fn set_env_vars_from_file(app: &str, json: &serde_json::Value) -> Result<(), Bo

// Source the global envfile first
if let Some(global_envfile) = stdout_json["envfile"].as_str() {
if let Ok(env_vars) = source_env_file(global_envfile) {
for (key, value) in env_vars {
std::env::set_var(key, value);
}
}
source_env_file(global_envfile)?;
}

// Source the app-specific envfile
if let Some(app_envfile) = stdout_json["apps"][app]["envfile"].as_str() {
if let Ok(env_vars) = source_env_file(app_envfile) {
for (key, value) in env_vars {
std::env::set_var(key, value);
}
}
source_env_file(app_envfile)?;
}

Ok(())
Expand Down

0 comments on commit c2bd475

Please sign in to comment.