Skip to content

Commit

Permalink
refactor: Extract error handling to errGuard function
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvanduocit committed Mar 29, 2023
1 parent 1e874a4 commit 756c139
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ func main() {
client := NewGptClient(apiKey, model)

diff, err := getDiff()
if err != nil {
if explain, explainErr := explainError(context.Background(), client, err); explainErr == nil {
printError(explain)
os.Exit(1)
}

printError(err.Error())
os.Exit(1)
}
errGuard(client, err)

if diff == "" {
if !isDirty() {
Expand All @@ -75,15 +67,8 @@ func main() {
}
}

if err := gitAdd(); err != nil {
if explain, explainErr := explainError(context.Background(), client, err); explainErr == nil {
printError(explain)
os.Exit(1)
}

printError(err.Error())
os.Exit(1)
}
err := gitAdd()
errGuard(client, err)
}

commitMessage := ""
Expand All @@ -96,16 +81,9 @@ func main() {
printNormal("Assistant: " + generateLoadingMessage())

commitMessage, err = client.ChatComplete(context.Background(), messages)
if err != nil {
if explain, explainErr := explainError(context.Background(), client, err); explainErr == nil {
printError(explain)
os.Exit(1)
}
printError(err.Error())
os.Exit(1)
}
errGuard(client, err)

fmt.Print("\033[1A\033[K")
deletePreviousLine(1)

if commitMessage == "" {
printNormal("Assistant: I don't know what to say about this diff, please give me a hint.")
Expand All @@ -118,15 +96,7 @@ func main() {
}
// Loop until the user response
userResponse, err := askForUserResponse()
if err != nil {
if explain, explainErr := explainError(context.Background(), client, err); explainErr == nil {
printError(explain)
os.Exit(1)
}

printError(err.Error())
os.Exit(1)
}
errGuard(client, err)

if isAgree := IsAgree(client, userResponse); isAgree {
break
Expand All @@ -139,13 +109,18 @@ func main() {
}

if err := commit(commitMessage); err != nil {
printError("Assistant: failed to commit: " + err.Error())
os.Exit(1)
errGuard(client, err)
}

printSuccess("Assistant: " + getSuccessMessage())
}

func deletePreviousLine(numOfLine uint) {
for i := 0; i < int(numOfLine); i++ {
fmt.Print("\033[1A\033[K")
}
}

func showHelp() {
fmt.Println("Usage: ai-commit [options]")

Expand Down Expand Up @@ -174,7 +149,10 @@ func askForUserResponse() (string, error) {
userResponse = strings.TrimSpace(userResponse)

// remove the 2nd last line
fmt.Print("\033[1A\033[K")
deletePreviousLine(2)

fmt.Println("You: " + userResponse)

if userResponse == "" {
printWarning("Assistant: Please enter your response, say yes if you want to use the message or press Ctrl+C to exit")
return askForUserResponse()
Expand Down Expand Up @@ -278,6 +256,20 @@ func isDirty() bool {
return out.Len() > 0
}

func errGuard(client *GptClient, err error) {
if err == nil {
return
}

if explain, explainErr := explainError(context.Background(), client, err); explainErr == nil {
printError(explain)
os.Exit(1)
}

printError(err.Error())
os.Exit(1)
}

var agreeWords = []string{
"yes",
"y",
Expand Down

0 comments on commit 756c139

Please sign in to comment.