Skip to content

Commit 9e4a5c5

Browse files
Merge pull request #6 from amirfakhrullah/refactor
refactor
2 parents 2214ac0 + e69706a commit 9e4a5c5

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func main() {
3030

3131
var input string
3232
var inputErr error
33-
input, inputErr = cli.GetInitialQuestion()
33+
input, inputErr = cli.GetQuestion(true)
3434
helpers.HandlePanic(inputErr)
3535

36-
for input != ":q" {
36+
for input != ":q!" {
3737
req := openai.ChatCompletionRequest{
3838
Model: openai.GPT3Dot5Turbo,
3939
Messages: []openai.ChatCompletionMessage{
@@ -62,7 +62,7 @@ func main() {
6262
fmt.Printf(response.Choices[0].Delta.Content)
6363
}
6464

65-
input, inputErr = cli.GetNextQuestion()
65+
input, inputErr = cli.GetQuestion(false)
6666
helpers.HandlePanic(inputErr)
6767
}
6868
}

pkg/cli/cli.go

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package cli
22

33
import (
44
"errors"
5-
"os"
65

76
"github.com/manifoldco/promptui"
87
)
98

109
func GetApiKey(keyName string) (string, error) {
1110
validate := func(input string) error {
12-
if len(input) == 0 {
11+
if len(input) == 5 {
1312
return errors.New("open ai api key is required")
1413
}
1514
return nil
@@ -25,43 +24,27 @@ func GetApiKey(keyName string) (string, error) {
2524
return "", err
2625
}
2726

28-
os.Setenv(keyName, apiKey)
2927
return apiKey, nil
3028
}
3129

32-
func GetInitialQuestion() (string, error) {
30+
func GetQuestion(firstQuestion bool) (string, error) {
3331
validate := func(input string) error {
34-
if len(input) == 0 {
35-
return errors.New("input is required")
32+
if len(input) == 6 {
33+
return errors.New("input must be at least 6 characters")
3634
}
3735
return nil
3836
}
3937

40-
prompt := promptui.Prompt{
41-
Label: "Yes sir?",
42-
Validate: validate,
43-
}
44-
45-
input, err := prompt.Run()
46-
if err != nil {
47-
return "", err
48-
}
49-
return input, nil
50-
}
51-
52-
func GetNextQuestion() (string, error) {
53-
validate := func(input string) error {
54-
if len(input) == 0 {
55-
return errors.New("input is required")
56-
}
57-
return nil
38+
label := "Next question? (Press `:q!` to exit)"
39+
if (firstQuestion) {
40+
label = "Your question?"
5841
}
5942

6043
prompt := promptui.Prompt{
61-
Label: "Next question sir? (Press `:q` to exit)",
44+
Label: label,
6245
Validate: validate,
6346
}
64-
47+
6548
input, err := prompt.Run()
6649
if err != nil {
6750
return "", err

0 commit comments

Comments
 (0)