forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloner.go
195 lines (150 loc) · 3.66 KB
/
cloner.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
package main
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"log"
"os"
"path/filepath"
"reflect"
"strings"
"golang.org/x/tools/imports"
)
const newPkgName = "versiontwo"
const (
srcDir = "./pkg/config"
dstDir = "./pkg/commands/internal/migrate/versiontwo"
)
func main() {
stat, err := os.Stat(srcDir)
if err != nil {
log.Fatal(err)
}
if !stat.IsDir() {
log.Fatalf("%s is not a directory", srcDir)
}
_ = os.RemoveAll(dstDir)
err = processPackage(srcDir, dstDir)
if err != nil {
log.Fatalf("Processing package error: %v", err)
}
}
func processPackage(srcDir, dstDir string) error {
return filepath.Walk(srcDir, func(srcPath string, _ os.FileInfo, err error) error {
if err != nil {
return err
}
if skipFile(srcPath) {
return nil
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, srcPath, nil, parser.AllErrors)
if err != nil {
return fmt.Errorf("parsing %s: %w", srcPath, err)
}
processFile(file)
return writeNewFile(fset, file, srcPath, dstDir)
})
}
func skipFile(path string) bool {
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return true
}
switch filepath.Base(path) {
case "base_loader.go", "loader.go":
return true
default:
return false
}
}
func processFile(file *ast.File) {
file.Name.Name = newPkgName
var newDecls []ast.Decl
for _, decl := range file.Decls {
d, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
switch d.Tok {
case token.CONST, token.VAR:
continue
case token.TYPE:
for _, spec := range d.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
processStructFields(structType)
}
default:
// noop
}
newDecls = append(newDecls, decl)
}
file.Decls = newDecls
}
func processStructFields(structType *ast.StructType) {
var newFields []*ast.Field
for _, field := range structType.Fields.List {
if len(field.Names) > 0 && !field.Names[0].IsExported() {
continue
}
if field.Tag == nil {
continue
}
field.Type = convertType(field.Type)
field.Tag.Value = convertStructTag(field.Tag.Value)
newFields = append(newFields, field)
}
structType.Fields.List = newFields
}
func convertType(expr ast.Expr) ast.Expr {
ident, ok := expr.(*ast.Ident)
if !ok {
return expr
}
switch ident.Name {
case "bool", "string", "int", "int8", "int16", "int32", "int64", "float32", "float64":
return &ast.StarExpr{X: ident}
default:
return expr
}
}
func convertStructTag(value string) string {
structTag := reflect.StructTag(strings.Trim(value, "`"))
key := structTag.Get("mapstructure")
if key == ",squash" {
return wrapStructTag(`yaml:",inline"`)
}
return wrapStructTag(fmt.Sprintf(`yaml:"%[1]s,omitempty" toml:"%[1]s,multiline,omitempty"`, key))
}
func wrapStructTag(s string) string {
return "`" + s + "`"
}
func writeNewFile(fset *token.FileSet, file *ast.File, srcPath, dstDir string) error {
var buf bytes.Buffer
buf.WriteString("// Code generated by pkg/commands/internal/migrate/cloner/cloner.go. DO NOT EDIT.\n\n")
err := printer.Fprint(&buf, fset, file)
if err != nil {
return fmt.Errorf("printing %s: %w", srcPath, err)
}
dstPath := filepath.Join(dstDir, filepath.Base(srcPath))
_ = os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
formatted, err := imports.Process(dstPath, buf.Bytes(), nil)
if err != nil {
return fmt.Errorf("formatting %s: %w", dstPath, err)
}
//nolint:gosec,mnd // The permission is right.
err = os.WriteFile(dstPath, formatted, 0o644)
if err != nil {
return fmt.Errorf("writing file %s: %w", dstPath, err)
}
return nil
}