-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
113 lines (108 loc) · 2.46 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
package main
import (
"codereviwe/helper"
"context"
"encoding/csv"
"flag"
"fmt"
"github.com/sashabaranov/go-openai"
"github.com/spf13/viper"
"io/ioutil"
"os"
"path/filepath"
)
func main() {
var root string
flag.StringVar(&root, "f", "", "specify the root folder path")
flag.Parse()
if root == "" {
fmt.Println("Please specify the root folder path using -f flag")
os.Exit(1)
}
err := filepath.Walk(root, visit)
if err != nil {
fmt.Println("error:", err)
}
}
func visit(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
fmt.Println("directory:", path)
} else {
setConfigPath, _ := os.UserHomeDir()
getConfigPathName := setConfigPath[0:len(setConfigPath)]
configPathName := getConfigPathName + "/.config/chatGPT-CodeReview/"
viper.AddConfigPath(configPathName)
viper.AddConfigPath("$HOME/")
viper.SetConfigName("config")
viper.SetConfigType("json")
err := viper.ReadInConfig()
if err != nil {
helper.CreatConfig()
os.Exit(0)
}
apiKey := fmt.Sprintf("%s", viper.Get("apikey.1"))
var record [][]string
csvFileTitle := [][]string{
{"Code", "Result"},
}
record = append(record, csvFileTitle...)
fileName := path + ".csv"
csvdatafile, err := os.Create(fileName)
if err != nil {
fmt.Println(err)
}
writer := csv.NewWriter(csvdatafile)
defer csvdatafile.Close()
contents, err := ioutil.ReadFile(path)
fmt.Println(string(contents))
if err != nil {
fmt.Println(err)
return nil
}
responseData := GetResponse(apiKey, string(contents))
if len(responseData) > 0 {
data := [][]string{
{
string(contents),
responseData,
},
}
fmt.Println(responseData)
record = append(record, data...)
} else {
fmt.Println("No choices available in response")
}
err = writer.WriteAll(record)
if err != nil {
return err
}
writer.Flush()
}
return nil
}
func GetResponse(apiKey string, contents string) string {
client := openai.NewClient(apiKey)
prompt := "使用中文分析一下这段代码存在什么漏洞:\n" + contents
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
}
if len(resp.Choices) > 0 {
return resp.Choices[0].Message.Content
}
return ""
}