Fix Command Line Errors Instantly with ChatGPT-Powered CLI Tool
It’s very common to face command line errors while coding. Sometimes we’re able to quickly fix them because we’re familiar with the issue and have dealt with it before.
Other times, however, we’re faced with errors that take hours of searching on Google or StackOverflow to resolve. I’ve spent countless hours trying to fix something, breaking other things in the process, and finally finding a solution but forgetting what changes I’ve made along the way.
I think the major issue is trying to understand what the actual problem is, what the error means, and where or what should you search for. An example would be, given many lines of errors in your terminal, how do you know which keyword to pick from the error message to search for? The result contains many similar error messages but on a different set of problems or a different coding environment than yours. This usually leads to many hours of frustration, and staring at the wall in front of you.
To help solve this problem, I’ve been experimenting with ChatGPT and have used it many times to find answers or guides to errors I’ve encountered while coding. I thought it would be helpful to create a CLI tool that could access ChatGPT directly from the terminal, saving time and effort. This would save me time trying to copy the error code to the ChatGPT client webpage and every time writing the long text of prompts of what I expected from ChatGPT.
So, I wrote gpt-comrade over the weekend, which helps me find a solution or fix to the last command I ran on my terminal.
When you ran into an error, just open this tool and type the following command:
comrade fix
The tool fetches the last command you ran, reruns it, and sends the error message from stderr to ChatGPT. ChatGPT then provides suggestions on what may be wrong with your command.
It's that simple!
To get you started, first clone this open-sourced repo — https://github.com/shahnk19/gpt-comrade. Then in the repository directory, run:
go install
Make sure you have go
is installed on your machine and the GOPATH
is properly set up. Next, insert the following alias
setup into your .bashrc
file.
alias comrade='gpt-comrade "$@" -k="$(fc -ln -1)"'
Make sure this new alias
is available by either restarting the terminal or running:
source ~/.bashrc
Next, you need to setup your OPENAI API key. You can get your API key from https://platform.openai.com/account/api-keys. Once you get the key, set it up in your terminal ENV:
export OPENAI_API_KEY=<your-api-key-here>
Now, you are ready to use gpt-comrade. In your terminal, if you run into some errors, immediately enter the following command to ask for help from ChatGPT.
comrade fix
This would make the tool rerun the last command you ran, and fetch its error. This information is passed to ChatGPT through its API.
Private or sensitive information
Be very careful with private or sensitive information. For example, sometime your CLI commands may contain keys, Ids, tokens or information that are private and sensitive. In this case, you should be careful not to expose this information to ChatGPT.
It's a good practice to review the OpenAI Terms and Policy. OpenAI has stated they do use your submitted data only in cases when you use their clients such as ChatGPT and not through the API.
However, it's wise to consider not sharing such information to a 3rd party provider and consult your organization's security policy.
In the same spirit, it's also wise to consider NOT running this tool on destructive commands. For example, the command to delete folders rm -rf some_folder/
should NOT be run using this tool. This is because this tool requires re-running the particular command to fetch the std error. So, if it previously was run and you let gpt-comrade re-run again, it may cause irreversible issues.
How helpful is it?
It's still early, I have not run into any big issues that I already do not know the solution to it. Again this is because of familiarity with my development environment. But I do find it useful when I was working with the go
code since its been many years since I last set up this environment.
I face a problem importing my local package. When I separated my OpenAPI client code into a separate file and package in client.go
I have no idea how to import this package into the fix.go
file. So I ran comrade and here is my CLI output:
❯ go install
cmd/fix.go:10:2: local import "./pkg" in non-local package
❯ comrade fix
Current Shell: /bin/zsh
gpt-comrade finding a fix for: go install
Based on the error message you provided, it looks like you have a local
import in a non-local package. You need to remove it and replace it with
a proper import statement.
To fix this error, follow these steps:
1. Open the file "cmd/fix.go" and find where you have imported the local
package "./pkg".
2. Replace the import statement "./pkg" with the full import path of
the package, for example: "github.com/yourusername/projectname/pkg".
3. Save the file and try running the "go install" command again.
If you encounter the same error, make sure that you don't have any other
similar import issues in your code, and that all your import statements
are using proper syntax.
Stream finished
As you can see from the response above, ChatGPT found out that I am clueless about how to import a local package into my fix.go
file. So it suggested that I change the import ./pkg
to import github.com/shahnk19/gpt-comrade/pkg
.
So I fixed the problem with that quick help. I certainly have to go and refresh and relearn how to properly import local packages in go.
How its made
First I have to choose what language should this CLI tool be written on. I do have the option to use node
, or python
. I choose go
because of familiarity since I worked with go
some years ago and I do like compiled languages for this kind of tool. But python
is a viable option if you are familiar with it since it has an official library from OpenAI.
go
has a powerful library called cobra
that can be used to build powerful CLI tools. You first need to install this library into your project and import it.
Then you can use the generators to create the CLI commands you wish to support. It would automatically generate a scaffold of the code needed to run the command. You can then edit the command as needed, for example, change the documentation, instructions, and flags supported.
Next, you need to use the community-maintained go-openai library that provides a simple interface to OpenAI APIs.
Finally, the most important part of this, is how we prompt ChatGPT to act as a CLI assistant. First, we need to assign a system role for ChatGPT, as shown in the code below:
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant. You can find solution for any command line errors given to you.",
}
Next, we need to send the request, with instructions and questions so that it can provide the answer that we need. This message contains the last run command in the parameter command
and the stderr received in the parameter errorMessage
.
{
Role: openai.ChatMessageRoleUser,
Content: "I tried to run this command in my terminal: '" + command +
"' It is giving this error: " + errorMessage + "." +
terminalStatusPrompt(terminalStatus) +
"Can you help to find the problem and what can I do to fix it?" +
cheekyPrompt(cheeky),
}
We are also providing the terminalStatus
which is what the terminal exit status is. Finally, if the cheeky
flag is set, we would add the instruction to make ChatGPT respond with some humor.
Here is an example of the full prompt is:
I tried to run this command in my terminal: ‘npm run start’. It is giving this error: npm ERR! path /Users/shah/MyProjects/gpt-comrade/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open ‘/Users/shah/MyProjects/gpt-comrade/package.json’
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoentnpm ERR! A complete log of this run can be found in:
npm ERR! /Users/shah/.npm/_logs/2023–04–10T02_23_41_844Z-debug.log.My terminal response status is ‘exit status 254’.Can you help to find the problem and what can I do to fix it?Make the response cheeky or add some humor.
Just for fun
As a bonus, just to have fun I added a flag to make gpt-comrade to as some humor to its response. Try to run this command instead and see if that can make you amused — I hardly find it to be funny, but it may deserve a few chuckles.
comrade fix --cheeky
Have fun!