diff options
| -rwxr-xr-x | scripts/bin.js | 72 | ||||
| -rwxr-xr-x | scripts/bin.py | 33 | ||||
| -rwxr-xr-x | scripts/bin.sh | 29 |
3 files changed, 72 insertions, 62 deletions
diff --git a/scripts/bin.js b/scripts/bin.js index 22005ee..c7a440c 100755 --- a/scripts/bin.js +++ b/scripts/bin.js @@ -1,47 +1,51 @@ #!/usr/bin/env node const path = require("path"); -const fs = require('fs'); +const fs = require("fs"); function parseArgv() { - let func_file = process.argv[1]; - let func_data = null; + let funcName = process.argv[1]; + let funcData = null; - if (func_file.endsWith("bin.js")) { - func_file = process.argv[2] - func_data = process.argv[3] + if (funcName.endsWith("bin.js")) { + funcName = process.argv[2]; + funcData = process.argv[3]; } else { - func_file = path.basename(func_file) - func_data = process.argv[2]; + funcName = path.basename(funcName); + funcData = process.argv[2]; } - if (!func_file.endsWith(".js")) { - func_file += '.js' + if (funcName.endsWith(".js")) { + funcName = funcName.slice(0, -3); } - return [func_file, func_data] + return [funcName, funcData]; } -function loadFunc(func_file) { - const func_path = path.resolve(process.env["LLM_FUNCTIONS_DIR"], `tools/${func_file}`) +function loadFunc(funcName) { + const funcFileName = `${funcName}.js`; + const funcPath = path.resolve( + process.env["LLM_FUNCTIONS_DIR"], + `tools/${funcFileName}`, + ); try { - return require(func_path); + return require(funcPath); } catch { - console.log(`Invalid function: ${func_file}`) - process.exit(1) + console.log(`Invalid function: ${funcFileName}`); + process.exit(1); } } function loadEnv(filePath) { try { - const data = fs.readFileSync(filePath, 'utf-8'); - const lines = data.split('\n'); + const data = fs.readFileSync(filePath, "utf-8"); + const lines = data.split("\n"); - lines.forEach(line => { - if (line.trim().startsWith('#') || line.trim() === '') return; + lines.forEach((line) => { + if (line.trim().startsWith("#") || line.trim() === "") return; - const [key, ...value] = line.split('='); - process.env[key.trim()] = value.join('=').trim(); + const [key, ...value] = line.split("="); + process.env[key.trim()] = value.join("=").trim(); }); } catch {} } @@ -50,25 +54,27 @@ process.env["LLM_FUNCTIONS_DIR"] = path.resolve(__dirname, ".."); loadEnv(path.resolve(process.env["LLM_FUNCTIONS_DIR"], ".env")); -const [func_file, func_data] = parseArgv(); +const [funcName, funcData] = parseArgv(); + +process.env["LLM_FUNCTION_NAME"] = funcName; if (process.env["LLM_FUNCTION_ACTION"] == "declarate") { - const { declarate } = loadFunc(func_file); - console.log(JSON.stringify(declarate(), null, 2)) + const { declarate } = loadFunc(funcName); + console.log(JSON.stringify(declarate(), null, 2)); } else { - if (!func_data) { + if (!funcData) { console.log("No json data"); - process.exit(1) + process.exit(1); } let args; try { - args = JSON.parse(func_data) + args = JSON.parse(funcData); } catch { - console.log("Invalid json data") - process.exit(1) + console.log("Invalid json data"); + process.exit(1); } - const { execute } = loadFunc(func_file); - execute(args) -}
\ No newline at end of file + const { execute } = loadFunc(funcName); + execute(args); +} diff --git a/scripts/bin.py b/scripts/bin.py index 6c56ea0..0ea49ed 100755 --- a/scripts/bin.py +++ b/scripts/bin.py @@ -6,30 +6,31 @@ import sys import importlib.util def parse_argv(): - func_file = sys.argv[0] + func_name = sys.argv[0] func_data = None - if func_file.endswith("bin.py"): - func_file = sys.argv[1] if len(sys.argv) > 1 else None + if func_name.endswith("bin.py"): + func_name = sys.argv[1] if len(sys.argv) > 1 else None func_data = sys.argv[2] if len(sys.argv) > 2 else None else: - func_file = os.path.basename(func_file) + func_name = os.path.basename(func_name) func_data = sys.argv[1] if len(sys.argv) > 1 else None - if not func_file.endswith(".py"): - func_file += ".py" + if func_name.endswith(".py"): + func_name = func_name[:-3] - return func_file, func_data + return func_name, func_data -def load_func(func_file): - func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{func_file}") +def load_func(func_name): + func_file_name = f"{func_name}.py" + func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{func_file_name}") if os.path.exists(func_path): - spec = importlib.util.spec_from_file_location(func_file, func_path) + spec = importlib.util.spec_from_file_location(f"{func_file_name}", func_path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module else: - print(f"Invalid function: {func_file}") + print(f"Invalid function: {func_file_name}") sys.exit(1) def load_env(file_path): @@ -45,14 +46,16 @@ def load_env(file_path): except FileNotFoundError: pass -os.environ["LLM_FUNCTIONS_DIR"] = os.path.join(os.path.dirname(__file__), "..") +os.environ["LLM_FUNCTIONS_DIR"] = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) load_env(os.path.join(os.environ["LLM_FUNCTIONS_DIR"], ".env")) -func_file, func_data = parse_argv() +func_name, func_data = parse_argv() + +os.environ["LLM_FUNCTION_NAME"] = func_name if os.getenv("LLM_FUNCTION_ACTION") == "declarate": - module = load_func(func_file) + module = load_func(func_name) print(json.dumps(module.declarate(), indent=2)) else: if not func_data: @@ -66,5 +69,5 @@ else: print("Invalid json data") sys.exit(1) - module = load_func(func_file) + module = load_func(func_name) module.execute(args)
\ No newline at end of file diff --git a/scripts/bin.sh b/scripts/bin.sh index b5b1075..807343a 100755 --- a/scripts/bin.sh +++ b/scripts/bin.sh @@ -8,26 +8,27 @@ if [[ -f "$LLM_FUNCTIONS_DIR/.env" ]]; then fi if [[ "$0" == *bin.sh ]]; then - FUNC_FILE="$1" - FUNC_DATA="$2" + func_name="$1" + func_data="$2" else - FUNC_FILE="$(basename "$0")" - FUNC_DATA="$1" + func_name="$(basename "$0")" + func_data="$1" fi -if [[ "$FUNC_FILE" != *'.sh' ]]; then - FUNC_FILE="$FUNC_FILE.sh" +if [[ "$func_name" == *.sh ]]; then + func_name="${func_name:0:$((${#func_name}-3))}" fi -FUNC_FILE="$LLM_FUNCTIONS_DIR/tools/$FUNC_FILE" +export LLM_FUNCTION_NAME="$func_name" +func_file="$LLM_FUNCTIONS_DIR/tools/$func_name.sh" export JQ=jq if [[ "$OS" == "Windows_NT" ]]; then export JQ="jq -b" - FUNC_FILE="$(cygpath -w "$FUNC_FILE")" + func_file="$(cygpath -w "$func_file")" fi if [[ "$LLM_FUNCTION_ACTION" == "declarate" ]]; then - argc --argc-export "$FUNC_FILE" | \ + argc --argc-export "$func_file" | \ $JQ -r ' def parse_description(flag_option): if flag_option.describe == "" then @@ -69,13 +70,13 @@ if [[ "$LLM_FUNCTION_ACTION" == "declarate" ]]; then parameters: parse_parameter([.flag_options[] | select(.id != "help" and .id != "version")]) }' else - if [[ -z "$FUNC_DATA" ]]; then + if [[ -z "$func_data" ]]; then echo "No json data" exit 1 fi data="$( - echo "$FUNC_DATA" | \ + echo "$func_data" | \ $JQ -r ' to_entries | .[] | (.key | split("_") | join("-")) as $key | @@ -92,10 +93,10 @@ else } while IFS= read -r line; do if [[ "$line" == '--'* ]]; then - ARGS+=("$line") + args+=("$line") else - ARGS+=("$(echo "$line" | $JQ -r '.')") + args+=("$(echo "$line" | $JQ -r '.')") fi done <<< "$data" - "$FUNC_FILE" "${ARGS[@]}" + "$func_file" "${args[@]}" fi
\ No newline at end of file |
