Skip to content

Commit 1a74a2a

Browse files
committed
feat: migration command
1 parent 450752d commit 1a74a2a

15 files changed

+3177
-0
lines changed

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ linters:
176176
linters:
177177
- lll
178178

179+
# Related to migration command.
180+
- path: pkg/commands/internal/migrate/
181+
linters:
182+
- gocritic
183+
text: "hugeParam:"
184+
179185
formatters:
180186
enable:
181187
- gofmt
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package migrate
2+
3+
import (
4+
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/one"
5+
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/ptr"
6+
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/two"
7+
)
8+
9+
func ToConfig(old *one.Config) *two.Config {
10+
return &two.Config{
11+
Version: ptr.Pointer("2"),
12+
Linters: toLinters(old),
13+
Formatters: toFormatters(old),
14+
Issues: toIssues(old),
15+
Output: toOutput(old),
16+
Severity: toSeverity(old),
17+
Run: toRun(old),
18+
}
19+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package migrate
2+
3+
import (
4+
"slices"
5+
"strings"
6+
7+
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/one"
8+
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/ptr"
9+
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/two"
10+
)
11+
12+
func toFormatters(old *one.Config) two.Formatters {
13+
enable, _ := ProcessEffectiveLinters(old.Linters)
14+
15+
formatterNames := onlyFormatterNames(enable)
16+
17+
var paths []string
18+
if len(formatterNames) != 0 {
19+
paths = slices.Concat(old.Issues.ExcludeFiles, old.Issues.ExcludeDirs)
20+
}
21+
22+
return two.Formatters{
23+
Enable: formatterNames,
24+
Settings: two.FormatterSettings{
25+
Gci: toGciSettings(old.LintersSettings.Gci),
26+
GoFmt: toGoFmtSettings(old.LintersSettings.GoFmt),
27+
GoFumpt: toGoFumptSettings(old.LintersSettings.GoFumpt),
28+
GoImports: toGoImportsSettings(old.LintersSettings.GoImports),
29+
},
30+
Exclusions: two.FormatterExclusions{
31+
Generated: toExclusionGenerated(old.Issues.ExcludeGenerated),
32+
Paths: paths,
33+
},
34+
}
35+
}
36+
37+
func toGciSettings(old one.GciSettings) two.GciSettings {
38+
return two.GciSettings{
39+
Sections: old.Sections,
40+
NoInlineComments: old.NoInlineComments,
41+
NoPrefixComments: old.NoPrefixComments,
42+
CustomOrder: old.CustomOrder,
43+
NoLexOrder: old.CustomOrder,
44+
}
45+
}
46+
47+
func toGoFmtSettings(old one.GoFmtSettings) two.GoFmtSettings {
48+
settings := two.GoFmtSettings{
49+
Simplify: old.Simplify,
50+
}
51+
52+
for _, rule := range old.RewriteRules {
53+
settings.RewriteRules = append(settings.RewriteRules, two.GoFmtRewriteRule{
54+
Pattern: rule.Pattern,
55+
Replacement: rule.Replacement,
56+
})
57+
}
58+
59+
return settings
60+
}
61+
62+
func toGoFumptSettings(old one.GoFumptSettings) two.GoFumptSettings {
63+
return two.GoFumptSettings{
64+
ModulePath: old.ModulePath,
65+
ExtraRules: old.ExtraRules,
66+
}
67+
}
68+
69+
func toGoImportsSettings(old one.GoImportsSettings) two.GoImportsSettings {
70+
var localPrefixes []string
71+
72+
if ptr.Deref(old.LocalPrefixes) != "" {
73+
localPrefixes = strings.Split(ptr.Deref(old.LocalPrefixes), ",")
74+
}
75+
76+
return two.GoImportsSettings{
77+
LocalPrefixes: localPrefixes,
78+
}
79+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package migrate
2+
3+
import (
4+
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/one"
5+
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/two"
6+
)
7+
8+
func toIssues(old *one.Config) two.Issues {
9+
return two.Issues{
10+
MaxIssuesPerLinter: old.Issues.MaxIssuesPerLinter,
11+
MaxSameIssues: old.Issues.MaxSameIssues,
12+
UniqByLine: old.Issues.UniqByLine,
13+
DiffFromRevision: old.Issues.DiffFromRevision,
14+
DiffFromMergeBase: old.Issues.DiffFromMergeBase,
15+
DiffPatchFilePath: old.Issues.DiffPatchFilePath,
16+
WholeFiles: old.Issues.WholeFiles,
17+
Diff: old.Issues.Diff,
18+
NeedFix: old.Issues.NeedFix,
19+
}
20+
}

0 commit comments

Comments
 (0)