From faea2c6662d1c12016638b99b7670f3caa138a3f Mon Sep 17 00:00:00 2001 From: Joone Hur Date: Thu, 21 Mar 2024 21:45:12 -0700 Subject: [PATCH] Enable safe mode for Linux commands Add a new flag to enable safe mode, requiring user confirmation before executing any Linux command. Generated by gpt-3.5-turbo --- src/index.ts | 8 ++++++++ src/loz.ts | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index e9f0b3a..3807bdf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import * as yargs from "yargs"; import { Loz } from "./loz"; import { DEBUG } from "./constant"; +const LOZ_SAFE = process.env.LOZ_SAFE === "true" ? true : false; const isRunningInMocha = process.env.MOCHA_ENV === "test"; @@ -23,6 +24,11 @@ const args = yargs alias: "a", describe: "Append the model name at the end of the Git commit message.", }, + safe: { + alias: "s", + describe: + "Safe mode requires user confirmation before executing any Linux command.", + }, }) .help() .parseSync(); @@ -116,6 +122,8 @@ async function handleInputFromPipe(prompt: any): Promise { (async () => { await loz.init(); + if (LOZ_SAFE) args.safe = true; + if (args.safe) loz.enableSafe(); await handleLozCommand(); loz.close(); })(); diff --git a/src/loz.ts b/src/loz.ts index 24c148c..9f86a37 100644 --- a/src/loz.ts +++ b/src/loz.ts @@ -15,7 +15,6 @@ import { } from "./config"; import { Git } from "./git"; -const LOZ_SAFE = process.env.LOZ_SAFE === "true" ? true : false; // Get the path to the home directory const HOME_PATH = os.homedir() || ""; @@ -38,6 +37,7 @@ export class Loz { config: Config = new Config(); git: Git = new Git(); attribution: boolean = false; + safeMode: boolean = false; constructor() { this.defaultSettings = { @@ -66,6 +66,10 @@ export class Loz { await this.initLLMfromConfig(); } + public enableSafe(): void { + this.safeMode = true; + } + // load config from JSON file private async initLLMfromConfig(): Promise { if (!fs.existsSync(this.configPath)) { @@ -362,7 +366,7 @@ export class Loz { }; this.chatHistoryManager.addChat(promptAndCompleteText); - if (!LOZ_SAFE) { + if (this.safeMode === false) { try { await runCommand(linuxCommand); } catch (error: any) {