Skip to content

Commit

Permalink
Refactor command execution logic for user safety
Browse files Browse the repository at this point in the history
Improve the logic for executing commands based on user input to ensure
safe execution and user interaction.

Generated by gpt-3.5-turbo
  • Loading branch information
joone committed Feb 24, 2024
1 parent cdd282d commit c465a3e
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,35 +456,43 @@ export class Loz {
return;
}

try {
if (DEBUG) console.log(completion.content);
const json = JSON.parse(completion.content);
if (DEBUG) console.log(completion.content);

let linuxCommand = json.commands ? json.commands : json.command;
if (json.arguments && json.arguments.length > 0) {
linuxCommand += " " + json.arguments.join(" ");
}
let json;
try {
json = JSON.parse(completion.content);
if (DEBUG) console.log(JSON.stringify(json, null, 2));
} catch (error) {
console.error("Error parsing JSON:", error);
return;
}

if (LOZ_SAFE) {
const rl = readlinePromises.createInterface({
input: process.stdin,
output: process.stdout,
});
const answer = await rl.question(
`Do you want to run this command?: ${linuxCommand} (y/n) `
);
rl.close();
let linuxCommand = json.commands ? json.commands : json.command;
if (json.arguments && json.arguments.length > 0) {
linuxCommand += " " + json.arguments.join(" ");
}

if (answer.toLowerCase() === "y") {
await runCommand(linuxCommand);
}
} else {
await runCommand(linuxCommand);
}
if (!LOZ_SAFE) {
await runCommand(linuxCommand);
return;
}

if (DEBUG) console.log(JSON.stringify(json, null, 2));
let answer = "n";
try {
const rl = readlinePromises.createInterface({
input: process.stdin,
output: process.stdout,
});
answer = await rl.question(
`Do you want to run this command?: ${linuxCommand} (y/n) `
);
rl.close();
} catch (error) {
console.error(error);
console.error("Error during user interaction:", error);
}

if (answer.toLowerCase() === "y") {
await runCommand(linuxCommand);
}
}

Expand Down

0 comments on commit c465a3e

Please sign in to comment.