fix: js/py dotenv unable to parse quoted value or duplicated (#114)

This commit is contained in:
sigoden
2024-10-18 20:19:10 +08:00
committed by GitHub
parent 615aa25266
commit e098f7f43a
4 changed files with 98 additions and 38 deletions
+27 -9
View File
@@ -67,20 +67,38 @@ async function setupEnv(rootDir, agentName, agentFunc) {
} }
async function loadEnv(filePath) { async function loadEnv(filePath) {
let lines = [];
try { try {
const data = await readFile(filePath, "utf-8"); const data = await readFile(filePath, "utf-8");
const lines = data.split("\n"); lines = data.split("\n");
} catch {
return;
}
lines.forEach((line) => { const envVars = new Map();
if (line.trim().startsWith("#") || line.trim() === "") return;
const [key, ...value] = line.split("="); for (const line of lines) {
const envName = key.trim(); if (line.trim().startsWith("#") || line.trim() === "") {
if (!process.env[envName]) { continue;
process.env[envName] = value.join("=").trim(); }
const [key, ...valueParts] = line.split("=");
const envName = key.trim();
if (!process.env[envName]) {
let envValue = valueParts.join("=").trim();
if (envValue.startsWith('"') && envValue.endsWith('"')) {
envValue = envValue.slice(1, -1);
} else if (envValue.startsWith("'") && envValue.endsWith("'")) {
envValue = envValue.slice(1, -1);
} }
}); envVars.set(envName, envValue);
} catch { } }
}
for (const [envName, envValue] of envVars.entries()) {
process.env[envName] = envValue;
}
} }
async function run(agentPath, agentFunc, agentData) { async function run(agentPath, agentFunc, agentData) {
+22 -10
View File
@@ -61,17 +61,29 @@ def setup_env(root_dir, agent_name, agent_func):
def load_env(file_path): def load_env(file_path):
try: try:
with open(file_path, "r") as f: with open(file_path, "r") as f:
for line in f: lines = f.readlines()
line = line.strip() except:
if line.startswith("#") or line == "": return
continue
key, *value = line.split("=") env_vars = {}
env_name = key.strip()
if env_name not in os.environ: for line in lines:
os.environ[env_name] = "=".join(value).strip() line = line.strip()
except FileNotFoundError: if line.startswith("#") or not line:
pass continue
key, *value_parts = line.split("=")
env_name = key.strip()
if env_name not in os.environ:
env_value = "=".join(value_parts).strip()
if env_value.startswith('"') and env_value.endswith('"'):
env_value = env_value[1:-1]
elif env_value.startswith("'") and env_value.endswith("'"):
env_value = env_value[1:-1]
env_vars[env_name] = env_value
os.environ.update(env_vars)
def run(agent_path, agent_func, agent_data): def run(agent_path, agent_func, agent_data):
+27 -9
View File
@@ -54,20 +54,38 @@ async function setupEnv(rootDir, toolName) {
} }
async function loadEnv(filePath) { async function loadEnv(filePath) {
let lines = [];
try { try {
const data = await readFile(filePath, "utf-8"); const data = await readFile(filePath, "utf-8");
const lines = data.split("\n"); lines = data.split("\n");
} catch {
return;
}
lines.forEach((line) => { const envVars = new Map();
if (line.trim().startsWith("#") || line.trim() === "") return;
const [key, ...value] = line.split("="); for (const line of lines) {
const envName = key.trim(); if (line.trim().startsWith("#") || line.trim() === "") {
if (!process.env[envName]) { continue;
process.env[envName] = value.join("=").trim(); }
const [key, ...valueParts] = line.split("=");
const envName = key.trim();
if (!process.env[envName]) {
let envValue = valueParts.join("=").trim();
if (envValue.startsWith('"') && envValue.endsWith('"')) {
envValue = envValue.slice(1, -1);
} else if (envValue.startsWith("'") && envValue.endsWith("'")) {
envValue = envValue.slice(1, -1);
} }
}); envVars.set(envName, envValue);
} catch { } }
}
for (const [envName, envValue] of envVars.entries()) {
process.env[envName] = envValue;
}
} }
async function run(toolPath, toolFunc, toolData) { async function run(toolPath, toolFunc, toolData) {
+22 -10
View File
@@ -56,17 +56,29 @@ def setup_env(root_dir, tool_name):
def load_env(file_path): def load_env(file_path):
try: try:
with open(file_path, "r") as f: with open(file_path, "r") as f:
for line in f: lines = f.readlines()
line = line.strip() except:
if line.startswith("#") or line == "": return
continue
key, *value = line.split("=") env_vars = {}
env_name = key.strip()
if env_name not in os.environ: for line in lines:
os.environ[env_name] = "=".join(value).strip() line = line.strip()
except FileNotFoundError: if line.startswith("#") or not line:
pass continue
key, *value_parts = line.split("=")
env_name = key.strip()
if env_name not in os.environ:
env_value = "=".join(value_parts).strip()
if env_value.startswith('"') and env_value.endswith('"'):
env_value = env_value[1:-1]
elif env_value.startswith("'") and env_value.endswith("'"):
env_value = env_value[1:-1]
env_vars[env_name] = env_value
os.environ.update(env_vars)
def run(tool_path, tool_func, tool_data): def run(tool_path, tool_func, tool_data):