Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-y parameter to execute script in -s mode #258

Open
vroby65 opened this issue Apr 16, 2024 · 1 comment
Open

-y parameter to execute script in -s mode #258

vroby65 opened this issue Apr 16, 2024 · 1 comment

Comments

@vroby65
Copy link

vroby65 commented Apr 16, 2024

I think that's good idea to add -y parameter to automatize shell request

@vroby65
Copy link
Author

vroby65 commented Apr 28, 2024

yes_support_patch.txt
this is the code:
in main.go
...
var temperature *string
var top_p *string
var max_length *string
var preprompt *string
var url *string
var yes = false
....
isShell := flag.Bool("s", false, "Generate and Execute shell commands.")
flag.BoolVar(isShell, "shell", false, "Generate and Execute shell commands.")

isYes := flag.Bool("y", false, "")
flag.BoolVar(isYes, "yes", false, "Deactivate confirm request.")

.....
case *isShell:
if len(prompt) > 1 {
go loading(&stopSpin)
trimmedPrompt := strings.TrimSpace(prompt)
if len(trimmedPrompt) < 1 {
fmt.Fprintln(os.Stderr, "You need to provide some text")
fmt.Fprintln(os.Stderr, Example: tgpt -s "How to update system")
os.Exit(1)
}
if *isYes {
yes=true
}
shellCommand(*preprompt + trimmedPrompt + contextText + pipedInput, yes)
.....

in helper.go

....

func shellCommand(input string, yes bool) {
// Get OS
operatingSystem := ""
if runtime.GOOS == "windows" {
operatingSystem = "Windows"
} else if runtime.GOOS == "darwin" {
operatingSystem = "MacOS"
} else if runtime.GOOS == "linux" {
result, err := exec.Command("lsb_release", "-si").Output()
distro := strings.TrimSpace(string(result))
if err != nil {
distro = ""
}
operatingSystem = "Linux" + "/" + distro
} else {
operatingSystem = runtime.GOOS
}

// Get Shell

shellName := "/bin/sh"

if runtime.GOOS == "windows" {
	shellName = "cmd.exe"

	if len(os.Getenv("PSModulePath")) > 0 {
		shellName = "powershell.exe"
	}
} else {
	shellEnv := os.Getenv("SHELL")
	if len(shellEnv) > 0 {
		shellName = shellEnv
	}
}

shellPrompt := fmt.Sprintf(
	"Your role: Provide only plain text without Markdown formatting. Do not show any warnings or information regarding your capabilities. Do not provide any description. If you need to store any data, assume it will be stored in the chat. Provide only %s command for %s without any description. If there is a lack of details, provide most logical solution. Ensure the output is a valid shell command. If multiple steps required try to combine them together. Prompt: %s\n\nCommand:", shellName, operatingSystem, input)

getCommand(shellPrompt)

}

// Get a command in response
func getCommand(shellPrompt string) {
checkInputLength(shellPrompt)

resp, err := providers.NewRequest(shellPrompt, structs.Params{ApiKey: *apiKey, ApiModel: *apiModel, Provider: *provider, Max_length: *max_length, Temperature: *temperature, Top_p: *top_p, Preprompt: *preprompt, Url: *url}, structs.ExtraOptions{})

if err != nil {
	stopSpin = true
	printConnectionErrorMsg(err)
}

defer resp.Body.Close()

stopSpin = true

code := resp.StatusCode

if code >= 400 {
	handleStatus400(resp)
}

fmt.Print("\r          \r")

scanner := bufio.NewScanner(resp.Body)

// Variables
fullLine := ""
previousText := ""

// Handling each part
for scanner.Scan() {
	newText := providers.GetMainText(scanner.Text(), *provider, shellPrompt)
	if len(newText) < 1 {
		continue
	}
	mainText := strings.Replace(newText, previousText, "", -1)
	previousText = newText
	fullLine += mainText

	bold.Print(mainText)
}
lineCount := strings.Count(fullLine, "\n") + 1
if lineCount == 1 {
	var userInput string
	if yes {
		userInput ="Y"
                    bold.Print("\n");
	} else {
		bold.Print("\n\nExecute shell command? [Y/n]: ")
		reader := bufio.NewReader(os.Stdin)
		userInput, _ = reader.ReadString('\n')
		userInput = strings.TrimSpace(userInput)
	}
	if userInput != "n" {			     
		var cmd *exec.Cmd
		if runtime.GOOS == "windows" {
			shellName := "cmd"

			if len(os.Getenv("PSModulePath")) > 0 {
				shellName = "powershell"
			}
			if shellName == "cmd" {
				cmd = exec.Command("cmd", "/C", fullLine)

			} else {
				cmd = exec.Command("powershell", fullLine)
			}

		} else {
			cmd = exec.Command("bash", "-c", fullLine)

		}

		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		err = cmd.Run()

		if err != nil {
			fmt.Fprintln(os.Stderr, err)
		}
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "Some error has occurred. Error:", err)
		os.Exit(1)
	}
}

}

.......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant