Skip to content

Commit b41261d

Browse files
committed
update execute sql script
1 parent bfc2d2d commit b41261d

File tree

1 file changed

+99
-58
lines changed

1 file changed

+99
-58
lines changed

skills/cloud-sql-postgres-data/scripts/execute_sql.js

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,81 +14,122 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17-
const { spawn, execSync } = require('child_process');
17+
const { spawn } = require('child_process');
1818
const path = require('path');
1919
const fs = require('fs');
20+
const os = require('os');
2021

21-
const toolName = "execute_sql";
22-
const configArgs = ["--prebuilt", "cloud-sql-postgres"];
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+
const additionalVars = {};
2341

24-
function getToolboxPath() {
2542
if (process.env.GEMINI_CLI === '1') {
26-
const localPath = path.resolve(__dirname, '../../../toolbox');
27-
if (fs.existsSync(localPath)) {
28-
return localPath;
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+
additionalVars[key] = value;
53+
});
2954
}
30-
}
31-
try {
32-
const checkCommand = process.platform === 'win32' ? 'where toolbox' : 'which toolbox';
33-
const globalPath = execSync(checkCommand, { stdio: 'pipe', encoding: 'utf-8' }).trim();
34-
if (globalPath) {
35-
return globalPath.split('\n')[0].trim();
55+
} else if (process.env.CLAUDE_CODE === 'true') {
56+
const prefix = 'CLAUDE_PLUGIN_';
57+
for (const key in process.env) {
58+
if (key.startsWith(prefix)) {
59+
additionalVars[key.substring(prefix.length)] = process.env[key];
60+
}
3661
}
37-
throw new Error("Toolbox binary not found");
38-
} catch (e) {
39-
throw new Error("Toolbox binary not found");
4062
}
41-
}
4263

43-
let toolboxBinary;
44-
try {
45-
toolboxBinary = getToolboxPath();
46-
} catch (err) {
47-
console.error("Error:", err.message);
48-
process.exit(1);
64+
for (const [key, value] of Object.entries(additionalVars)) {
65+
if (env[key] === undefined) {
66+
env[key] = value;
67+
}
68+
}
4969
}
5070

51-
function getEnv() {
52-
const envPath = path.resolve(__dirname, '../../../.env');
71+
/**
72+
* Prepares the environment and determines the user agent.
73+
* @returns {{ env: Object, userAgent: string }}
74+
*/
75+
function prepareEnvironment() {
5376
const env = { ...process.env };
54-
if (fs.existsSync(envPath)) {
55-
const envContent = fs.readFileSync(envPath, 'utf-8');
56-
envContent.split('\n').forEach(line => {
57-
const trimmed = line.trim();
58-
if (trimmed && !trimmed.startsWith('#')) {
59-
const splitIdx = trimmed.indexOf('=');
60-
if (splitIdx !== -1) {
61-
const key = trimmed.slice(0, splitIdx).trim();
62-
let value = trimmed.slice(splitIdx + 1).trim();
63-
value = value.replace(/(^['"]|['"]$)/g, '');
64-
if (env[key] === undefined) {
65-
env[key] = value;
66-
}
67-
}
68-
}
69-
});
77+
let userAgent = "skills";
78+
79+
if (process.env.GEMINI_CLI === '1') {
80+
userAgent = "skills-geminicli";
81+
} else if (process.env.CLAUDE_CODE === 'true') {
82+
userAgent = "skills-claudecode";
7083
}
71-
return env;
72-
}
7384

74-
let env = process.env;
75-
let userAgent = "skills";
76-
if (process.env.GEMINI_CLI === '1') {
77-
env = getEnv();
78-
userAgent = "skills-geminicli";
85+
mergeContextualVariables(env);
86+
87+
// Omit optional variables if they are empty strings to avoid misconfiguration
88+
OPTIONAL_VARS_TO_OMIT_IF_EMPTY.forEach(key => {
89+
if (env[key] === '') {
90+
delete env[key];
91+
}
92+
});
93+
94+
return { env, userAgent };
7995
}
8096

81-
const args = process.argv.slice(2);
97+
/**
98+
* Main execution function.
99+
*/
100+
function main() {
101+
const { env, userAgent } = prepareEnvironment();
102+
const args = process.argv.slice(2);
103+
const npxArgs = ["--yes", "@toolbox-sdk/server", "--log-level", "error", ...CONFIG_ARGS, "invoke", TOOL_NAME, "--user-agent-metadata", userAgent, ...args];
82104

83-
const toolboxArgs = ["--log-level", "error", ...configArgs, "invoke", toolName, "--user-agent-metadata", userAgent, ...args];
105+
let command = 'npx';
106+
let spawnArgs = npxArgs;
84107

85-
const child = spawn(toolboxBinary, toolboxArgs, { stdio: 'inherit', env });
108+
// The Windows Dependency-Free Bypass
109+
if (os.platform() === 'win32') {
110+
const nodeDir = path.dirname(process.execPath);
111+
const npxCliJs = path.join(nodeDir, 'node_modules', 'npm', 'bin', 'npx-cli.js');
86112

87-
child.on('close', (code) => {
88-
process.exit(code);
89-
});
113+
if (fs.existsSync(npxCliJs)) {
114+
command = process.execPath;
115+
spawnArgs = [npxCliJs, ...npxArgs];
116+
} else {
117+
console.error("Error: Could not find the npx executable to launch.");
118+
process.exit(1);
119+
}
120+
}
121+
122+
const child = spawn(command, spawnArgs, { stdio: 'inherit', env });
123+
124+
child.on('close', (code) => {
125+
process.exit(code);
126+
});
127+
128+
child.on('error', (err) => {
129+
console.error("Error executing toolbox:", err);
130+
process.exit(1);
131+
});
132+
}
90133

91-
child.on('error', (err) => {
92-
console.error("Error executing toolbox:", err);
93-
process.exit(1);
94-
});
134+
// Start the script
135+
main();

0 commit comments

Comments
 (0)