Skip to content

Commit e7a9dd7

Browse files
committed
improve error handling, update readme
1 parent cd53b37 commit e7a9dd7

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ How to do bash commands you always forget. OpenAI-powered.
66
## Build from source
77

88
```bash
9-
go build .
10-
go install
9+
go build
1110
```
1211

1312
Then you can move the binary to your path, e.g., `mv howto /usr/local/bin/`
1413

14+
Or, if you have your `$GOPATH/bin` in your path, you can just run `go install .`
15+
1516
## Environment variables
1617

1718
You need to connect your OpenAI API key to the program by setting the `OPENAI_API_KEY` environment variable. Get your OpenAI API key [here](https://beta.openai.com/docs/quickstart/add-your-api-key).

main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ type Choice struct {
4141

4242
func main() {
4343
httpkey := os.Getenv("OPENAI_API_KEY")
44+
if httpkey == "" {
45+
fmt.Println("OPENAI_API_KEY not set")
46+
os.Exit(1)
47+
}
4448

4549
// get env variable HOWTO_OPENAI_MODEL if it exists, else use code-davinci-002
4650
modelName := os.Getenv("HOWTO_OPENAI_MODEL")
@@ -55,7 +59,7 @@ func main() {
5559
input.WriteString(" ")
5660
}
5761

58-
prompt := fmt.Sprintf("Bash command to %s:```", input)
62+
prompt := fmt.Sprintf("Bash command to %s:```", input.String())
5963
suffix := "```"
6064

6165
body := []byte(fmt.Sprintf(`{
@@ -92,6 +96,13 @@ func main() {
9296
fmt.Println("Error decoding response: ", err)
9397
}
9498

99+
choices := openaiResponse.Choices
100+
if len(choices) == 0 {
101+
fmt.Println("OpenAI API disn't respont correctly. Did you correctly set you OPENAI_API_KEY?")
102+
fmt.Println("Response: ", resp)
103+
os.Exit(1)
104+
}
105+
95106
command := openaiResponse.Choices[0].Text
96107
// if "```" in command, cut out everything after it
97108
if index := bytes.Index([]byte(command), []byte("```")); index != -1 {

0 commit comments

Comments
 (0)