Skip to content

Commit

Permalink
feat: support dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jun 6, 2024
1 parent bfb7d75 commit a629a2a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
21 changes: 20 additions & 1 deletion run/tool.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

const path = require("path");
const fs = require('fs');

function parseArgv() {
let func_file = process.argv[1];
Expand All @@ -22,7 +23,7 @@ function parseArgv() {
}

function loadFunc(func_file) {
const func_path = path.resolve(__dirname, `../tools/${func_file}`)
const func_path = path.resolve(process.env["LLM_FUNCTIONS_DIR"], `tools/${func_file}`)
try {
return require(func_path);
} catch {
Expand All @@ -31,6 +32,24 @@ function loadFunc(func_file) {
}
}

function loadEnv(filePath) {
try {
const data = fs.readFileSync(filePath, 'utf-8');
const lines = data.split('\n');

lines.forEach(line => {
if (line.trim().startsWith('#') || line.trim() === '') return;

const [key, ...value] = line.split('=');
process.env[key.trim()] = value.join('=').trim();
});
} catch {}
}

process.env["LLM_FUNCTIONS_DIR"] = path.resolve(__dirname, "..");

loadEnv(path.resolve(process.env["LLM_FUNCTIONS_DIR"], ".env"));

const [func_file, func_data] = parseArgv();

if (process.env["LLM_FUNCTION_ACTION"] == "declarate") {
Expand Down
20 changes: 18 additions & 2 deletions run/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def parse_argv():
return func_file, func_data

def load_func(func_file):
base_dir = os.path.dirname(os.path.abspath(__file__))
func_path = os.path.join(base_dir, f"../tools/{func_file}")
func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{func_file}")
if os.path.exists(func_path):
spec = importlib.util.spec_from_file_location(func_file, func_path)
module = importlib.util.module_from_spec(spec)
Expand All @@ -33,6 +32,23 @@ def load_func(func_file):
print(f"Invalid function: {func_file}")
sys.exit(1)

def load_env(file_path):
try:
with open(file_path, 'r') as f:
for line in f:
line = line.strip()
if line.startswith('#') or line == '':
continue

key, *value = line.split('=')
os.environ[key.strip()] = '='.join(value).strip()
except FileNotFoundError:
pass

os.environ["LLM_FUNCTIONS_DIR"] = os.path.join(os.path.dirname(__file__), "..")

load_env(os.path.join(os.environ["LLM_FUNCTIONS_DIR"], ".env"))

func_file, func_data = parse_argv()

if os.getenv("LLM_FUNCTION_ACTION") == "declarate":
Expand Down
17 changes: 17 additions & 0 deletions run/tool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ def load_func(func_file)
end
end

def load_env(file_path)
return unless File.exist?(file_path)

File.readlines(file_path).each do |line|
line = line.strip
next if line.empty? || line.start_with?('#')

key, *value = line.split('=', 2)
ENV[key.strip] = value.join('=').strip
end
rescue StandardError
end

ENV['LLM_FUNCTIONS_DIR'] = Pathname.new(__dir__).join('..').expand_path.to_s

load_env(Pathname.new(ENV['LLM_FUNCTIONS_DIR']).join('.env').to_s)

func_file, func_data = parse_argv

if ENV["LLM_FUNCTION_ACTION"] == "declarate"
Expand Down
9 changes: 7 additions & 2 deletions run/tool.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/usr/bin/env bash
set -e

export LLM_FUNCTIONS_DIR="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)"

if [[ -f "$LLM_FUNCTIONS_DIR/.env" ]]; then
source "$LLM_FUNCTIONS_DIR/.env"
fi

if [[ "$0" == *tool.sh ]]; then
FUNC_FILE="$1"
FUNC_DATA="$2"
Expand All @@ -12,8 +18,7 @@ if [[ "$FUNC_FILE" != *'.sh' ]]; then
FUNC_FILE="$FUNC_FILE.sh"
fi

PROJECT_DIR="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)"
FUNC_FILE="$PROJECT_DIR/tools/$FUNC_FILE"
FUNC_FILE="$LLM_FUNCTIONS_DIR/tools/$FUNC_FILE"

if [[ "$OS" == "Windows_NT" ]]; then
FUNC_FILE="$(cygpath -w "$FUNC_FILE")"
Expand Down

0 comments on commit a629a2a

Please sign in to comment.