|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Copyright 2026 Google LLC |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +const { spawn } = require('child_process'); |
| 18 | +const path = require('path'); |
| 19 | +const fs = require('fs'); |
| 20 | +const os = require('os'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Configuration Constants |
| 24 | + */ |
| 25 | +const TOOL_NAME = "execute_sql"; |
| 26 | +const CONFIG_ARGS = ["--prebuilt", "cloud-sql-postgres"]; |
| 27 | +const OPTIONAL_VARS_TO_OMIT_IF_EMPTY = [ |
| 28 | + 'CLOUD_SQL_POSTGRES_USER', |
| 29 | + 'CLOUD_SQL_POSTGRES_PASSWORD', |
| 30 | + 'CLOUD_SQL_POSTGRES_IP_TYPE' |
| 31 | +]; |
| 32 | + |
| 33 | +/** |
| 34 | + * Merges external variables into the environment based on the current context. |
| 35 | + * For GEMINI_CLI, it loads from a .env file. |
| 36 | + * For CLAUDE_CODE, it transforms CLAUDE_PLUGIN_ prefixed variables. |
| 37 | + * @param {Object} env The environment object to populate. |
| 38 | + */ |
| 39 | +function mergeContextualVariables(env) { |
| 40 | + var env = {}; |
| 41 | + |
| 42 | + if (process.env.GEMINI_CLI === '1') { |
| 43 | + const envPath = path.resolve(__dirname, '../../../.env'); |
| 44 | + if (fs.existsSync(envPath)) { |
| 45 | + fs.readFileSync(envPath, 'utf-8').split('\n').forEach(line => { |
| 46 | + const trimmed = line.trim(); |
| 47 | + if (!trimmed || trimmed.startsWith('#')) return; |
| 48 | + const splitIdx = trimmed.indexOf('='); |
| 49 | + if (splitIdx === -1) return; |
| 50 | + const key = trimmed.slice(0, splitIdx).trim(); |
| 51 | + const value = trimmed.slice(splitIdx + 1).trim().replace(/(^['"]|['"]$)/g, ''); |
| 52 | + env[key] = value; |
| 53 | + }); |
| 54 | + } |
| 55 | + } else if (process.env.CLAUDECODE === '1') { |
| 56 | + const prefix = 'CLAUDE_PLUGIN_OPTION_'; |
| 57 | + for (const key in process.env) { |
| 58 | + if (key.startsWith(prefix)) { |
| 59 | + env[key.substring(prefix.length)] = process.env[key]; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Prepares the environment and determines the user agent. |
| 67 | + * @returns {{ env: Object, userAgent: string }} |
| 68 | + */ |
| 69 | +function prepareEnvironment() { |
| 70 | + var env = { ...process.env }; |
| 71 | + let userAgent = "skills"; |
| 72 | + |
| 73 | + if (process.env.GEMINI_CLI === '1') { |
| 74 | + userAgent = "skills-geminicli"; |
| 75 | + } else if (process.env.CLAUDECODE === '1') { |
| 76 | + userAgent = "skills-claudecode"; |
| 77 | + } |
| 78 | + |
| 79 | + mergeContextualVariables(env); |
| 80 | + |
| 81 | + // Omit optional variables if they are empty strings to avoid misconfiguration |
| 82 | + OPTIONAL_VARS_TO_OMIT_IF_EMPTY.forEach(key => { |
| 83 | + if (env[key] === '') { |
| 84 | + delete env[key]; |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + return { env, userAgent }; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Main execution function. |
| 93 | + */ |
| 94 | +function main() { |
| 95 | + const { env, userAgent } = prepareEnvironment(); |
| 96 | + const args = process.argv.slice(2); |
| 97 | + |
| 98 | + const command = os.platform() === 'win32' ? 'npx.cmd' : 'npx'; |
| 99 | + const processedArgs = os.platform() === 'win32' ? args.map(arg => arg.includes('"') ? '"' + arg.replace(/"/g, '""') + '"' : arg) : args; |
| 100 | + const npxArgs = ["--yes", "@toolbox-sdk/server@latest", "--log-level", "error", ...CONFIG_ARGS, "invoke", TOOL_NAME, "--user-agent-metadata", userAgent, ...processedArgs]; |
| 101 | + |
| 102 | + |
| 103 | + const child = spawn(command, npxArgs, { shell: os.platform() === 'win32', stdio: 'inherit', env }); |
| 104 | + |
| 105 | + |
| 106 | + child.on('close', (code) => { |
| 107 | + process.exit(code); |
| 108 | + }); |
| 109 | + |
| 110 | + child.on('error', (err) => { |
| 111 | + console.error("Error executing toolbox:", err); |
| 112 | + process.exit(1); |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +// Start the script |
| 117 | +main(); |
0 commit comments