-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (102 loc) · 2.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/fatih/color"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
)
type ResponseValue struct {
Corrections []struct {
Text string `json:"text"`
BestCandidate string `json:"best_candidate"`
Candidates []string `json:"candidates"`
} `json:"corrections"`
OriginalText string `json:"original_text"`
}
func validateWord(url string, sentence string) (*http.Response, error) {
newURL := fmt.Sprintf("%v%v", url, sentence)
req, err := http.NewRequest("GET", newURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("apikey", "jfNRme3SHJ3WIGR29FpyhLJC5PT4qem0")
client := &http.Client{}
body, err := client.Do(req)
if err != nil {
return nil, err
}
return body, nil
}
func handleCommit(commitMessage string) {
// Get the current working directory
currentDir, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current working directory:", err)
os.Exit(1)
}
stageCmd := exec.Command("git", "add", ".")
commitCmd := exec.Command("git", "commit", "-m", commitMessage)
stageCmd.Dir = currentDir
commitCmd.Dir = currentDir
output1, err := stageCmd.CombinedOutput()
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
output2, err := commitCmd.CombinedOutput()
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
fmt.Println("Commit made")
fmt.Printf("%v\n%v", string(output1), string(output2))
}
func main() {
var responseValue ResponseValue
var commitMessage []string
url := "https://api.apilayer.com/spell/spellchecker?q="
if len(os.Args) >= 2 {
commitMessage = append(commitMessage, os.Args[1:]...)
}
messageString := strings.Join(commitMessage, "%20")
response, err := validateWord(url, strings.ToLower(messageString))
if err != nil {
panic(err)
}
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
if err := json.Unmarshal([]byte(responseBody), &responseValue); err != nil {
panic(err)
}
if len(responseValue.Corrections) <= 0 {
handleCommit(strings.Join(commitMessage, " "))
return
} else {
color.Red("Spelling errors in the commit message!!")
fmt.Println("Mistakes: ")
for index, mistake := range responseValue.Corrections {
fmt.Printf(" %v.wrong word: ", index+1)
color.Red("%v\n", mistake.Text)
fmt.Print(" Suggestions: ")
color.Green("%v\n", mistake.Candidates)
}
//ignoring the mistakes
fmt.Println("To ignore click ENTER: ")
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
if scanner.Text() == "" {
handleCommit(strings.Join(commitMessage, " "))
}
}
if scanner.Err() != nil {
fmt.Println("An error occurred:", scanner.Err())
}
}
}