-
Notifications
You must be signed in to change notification settings - Fork 248
/
main.go
197 lines (175 loc) · 5 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
// gen-lint is a program for auto generate .golangci.yml for mod
package main
import (
"bufio"
"fmt"
"os"
"path"
"regexp"
"strings"
"github.com/bitfield/script"
goyaml "github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/parser"
)
func getBlackList() ([]string, error) {
file, err := os.Open("./scripts/gen-lint/modules_black_list")
if err != nil {
return nil, err
}
defer file.Close()
// 创建Scanner来逐行读取
scanner := bufio.NewScanner(file)
// 创建一个string类型的切片来存储文件中的每一行
var lines []string
// 逐行扫描
for scanner.Scan() {
// 将扫描到的行添加到切片中
lines = append(lines, scanner.Text())
}
// 检查是否有可能的错误
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
func matchBlackList(line string, blackList []string) bool {
for _, m := range blackList {
if line == m {
return true
}
}
return false
}
func getGolangciYmlTemplate() (string, error) {
content, err := os.ReadFile("./scripts/gen-lint/.golangci.yml.tpl")
if err != nil {
return "", err
}
return string(content), nil
}
// get skip files from code cc
func getSkipFiles() ([]string, error) {
content, err := os.ReadFile(".code.yml")
if err != nil {
return nil, err
}
filePath, err := goyaml.PathString("$.source.test_source.filepath_regex")
if err != nil {
return nil, err
}
skipFiles := []string{}
_ = filePath.Read(strings.NewReader(string(content)), &skipFiles)
return skipFiles, nil
}
func findModuleSkipFilesAndDirs(moduleDir string, files []string) ([]string, []string) {
skipDirs := []string{}
skipFiles := []string{}
for _, file := range files {
file = strings.TrimPrefix(file, "/")
if strings.HasPrefix(file, moduleDir) {
file = strings.Replace(file, moduleDir, "", 1)
file = strings.TrimPrefix(file, "/")
if strings.HasSuffix(file, ".go") {
skipFiles = append(skipFiles, file)
}
if strings.HasSuffix(file, ".*") {
file = strings.ReplaceAll(file, ".*", "*")
skipDirs = append(skipDirs, file)
}
}
}
return skipFiles, skipDirs
}
func main() {
blackList, err := getBlackList()
if err != nil {
panic(err)
}
mods, err := script.FindFiles("./").Match("go.mod").String()
if err != nil {
panic(err)
}
tpl, err := getGolangciYmlTemplate()
if err != nil {
panic(err)
}
skipFiles, err := getSkipFiles()
if err != nil {
panic(err)
}
// parse origin yaml
f, err := parser.ParseBytes([]byte(tpl), 0)
if err != nil {
panic(err)
}
re := regexp.MustCompile(`module\s+(.*)`)
for _, line := range strings.Split(mods, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
dir := path.Dir(line)
if matchBlackList(dir, blackList) {
continue
}
v, err := script.File(line).String()
if err != nil {
continue
}
match := re.FindStringSubmatch(v)
if len(match) <= 1 {
continue
}
module := match[1]
// get skip files and dirs
gotSkipFiles, gotSkipDirs := findModuleSkipFilesAndDirs(dir, skipFiles)
skipDirsPath, err := goyaml.PathString("$.run.skip-dirs")
if err != nil {
continue
}
skipFilesPath, err := goyaml.PathString("$.run.skip-files")
if err != nil {
continue
}
// append skip files and dirs
skipDirs := []string{}
skipFiles := []string{}
_ = skipDirsPath.Read(strings.NewReader(tpl), &skipDirs)
_ = skipFilesPath.Read(strings.NewReader(tpl), &skipFiles)
_ = skipDirsPath.ReplaceWithReader(f, strings.NewReader(stringsToYAML(append(skipDirs, gotSkipDirs...))))
_ = skipFilesPath.ReplaceWithReader(f, strings.NewReader(stringsToYAML(append(skipFiles, gotSkipFiles...))))
// append gci local-prefixes
localPrefixesPath, err := goyaml.PathString("$.linters-settings.goimports.local-prefixes")
if err != nil {
continue
}
_ = localPrefixesPath.ReplaceWithReader(f, strings.NewReader(module))
// append gci prefix
gciPath, err := goyaml.PathString("$.linters-settings.gci.sections")
if err != nil {
continue
}
_ = gciPath.ReplaceWithReader(f, strings.NewReader(stringsToYAML([]string{"standard", "default",
fmt.Sprintf("prefix(%s)", module)})))
_ = os.WriteFile(path.Join(dir, "./.golangci.yml"), []byte(f.String()), 0644)
fmt.Printf("generate .golangci.yml to %s done.\n", dir)
}
}
func stringsToYAML(v []string) string {
s := ""
for _, i := range v {
s += fmt.Sprintf("- %s\n", i)
}
return s
}