Skip to content

Commit dffdb06

Browse files
committed
chore: use config pointer like analyzers
1 parent ae0e109 commit dffdb06

File tree

5 files changed

+56
-31
lines changed

5 files changed

+56
-31
lines changed

pkg/goformatters/gci/gci.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package gci
22

33
import (
4+
"fmt"
5+
46
gcicfg "github.com/daixiang0/gci/pkg/config"
57
"github.com/daixiang0/gci/pkg/gci"
8+
"github.com/daixiang0/gci/pkg/log"
69
"github.com/ldez/grignotin/gomod"
710

811
"github.com/golangci/golangci-lint/pkg/config"
@@ -14,23 +17,36 @@ type Formatter struct {
1417
config *gcicfg.Config
1518
}
1619

17-
func New(cfg config.GciSettings) (*Formatter, error) {
20+
func New(settings *config.GciSettings) (*Formatter, error) {
21+
log.InitLogger()
22+
_ = log.L().Sync()
23+
1824
modPath, err := gomod.GetModulePath()
1925
if err != nil {
2026
return nil, err
2127
}
2228

23-
parsedCfg, err := gcicfg.YamlConfig{
29+
cfg := gcicfg.YamlConfig{
2430
Cfg: gcicfg.BoolConfig{
25-
NoInlineComments: cfg.NoInlineComments,
26-
NoPrefixComments: cfg.NoPrefixComments,
27-
SkipGenerated: cfg.SkipGenerated,
28-
CustomOrder: cfg.CustomOrder,
29-
NoLexOrder: cfg.NoLexOrder,
31+
NoInlineComments: settings.NoInlineComments,
32+
NoPrefixComments: settings.NoPrefixComments,
33+
SkipGenerated: settings.SkipGenerated,
34+
CustomOrder: settings.CustomOrder,
35+
NoLexOrder: settings.NoLexOrder,
3036
},
31-
SectionStrings: cfg.Sections,
37+
SectionStrings: settings.Sections,
3238
ModPath: modPath,
33-
}.Parse()
39+
}
40+
41+
if settings.LocalPrefixes != "" {
42+
cfg.SectionStrings = []string{
43+
"standard",
44+
"default",
45+
fmt.Sprintf("prefix(%s)", settings.LocalPrefixes),
46+
}
47+
}
48+
49+
parsedCfg, err := cfg.Parse()
3450
if err != nil {
3551
return nil, err
3652
}

pkg/goformatters/gofmt/gofmt.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ type Formatter struct {
1212
options gofmt.Options
1313
}
1414

15-
func New(cfg config.GoFmtSettings) *Formatter {
16-
var rewriteRules []gofmt.RewriteRule
17-
for _, rule := range cfg.RewriteRules {
18-
rewriteRules = append(rewriteRules, gofmt.RewriteRule(rule))
19-
}
15+
func New(settings *config.GoFmtSettings) *Formatter {
16+
options := gofmt.Options{}
17+
18+
if settings != nil {
19+
options.NeedSimplify = settings.Simplify
2020

21-
return &Formatter{
22-
options: gofmt.Options{
23-
NeedSimplify: cfg.Simplify,
24-
RewriteRules: rewriteRules,
25-
},
21+
for _, rule := range settings.RewriteRules {
22+
options.RewriteRules = append(options.RewriteRules, gofmt.RewriteRule(rule))
23+
}
2624
}
25+
26+
return &Formatter{options: options}
2727
}
2828

2929
func (*Formatter) Name() string {

pkg/goformatters/gofumpt/gofumpt.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ type Formatter struct {
1414
options gofumpt.Options
1515
}
1616

17-
func New(cfg config.GofumptSettings, goVersion string) *Formatter {
18-
return &Formatter{
19-
options: gofumpt.Options{
17+
func New(settings *config.GofumptSettings, goVersion string) *Formatter {
18+
var options gofumpt.Options
19+
20+
if settings != nil {
21+
options = gofumpt.Options{
2022
LangVersion: getLangVersion(goVersion),
21-
ModulePath: cfg.ModulePath,
22-
ExtraRules: cfg.ExtraRules,
23-
},
23+
ModulePath: settings.ModulePath,
24+
ExtraRules: settings.ExtraRules,
25+
}
2426
}
27+
28+
return &Formatter{options: options}
2529
}
2630

2731
func (*Formatter) Name() string {
@@ -32,7 +36,6 @@ func (f *Formatter) Format(_ string, src []byte) ([]byte, error) {
3236
return gofumpt.Source(src, f.options)
3337
}
3438

35-
// modified copy of pkg/golinters/gofumpt/gofumpt.go
3639
func getLangVersion(v string) string {
3740
if v == "" {
3841
// TODO: defaults to "1.15", in the future (v2) must be removed.

pkg/goformatters/goimports/goimports.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ package goimports
22

33
import (
44
"golang.org/x/tools/imports"
5+
6+
"github.com/golangci/golangci-lint/pkg/config"
57
)
68

79
const Name = "goimports"
810

911
type Formatter struct{}
1012

11-
func New() *Formatter {
13+
func New(settings *config.GoImportsSettings) *Formatter {
14+
if settings != nil {
15+
imports.LocalPrefix = settings.LocalPrefixes
16+
}
17+
1218
return &Formatter{}
1319
}
1420

pkg/goformatters/meta_formatter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ func NewMetaFormatter(log logutils.Log, cfg *config.Config, enabledLinters map[s
2323
m := &MetaFormatter{log: log}
2424

2525
if _, ok := enabledLinters[gofmt.Name]; ok {
26-
m.formatters = append(m.formatters, gofmt.New(cfg.LintersSettings.Gofmt))
26+
m.formatters = append(m.formatters, gofmt.New(&cfg.LintersSettings.Gofmt))
2727
}
2828

2929
if _, ok := enabledLinters[gofumpt.Name]; ok {
30-
m.formatters = append(m.formatters, gofumpt.New(cfg.LintersSettings.Gofumpt, cfg.Run.Go))
30+
m.formatters = append(m.formatters, gofumpt.New(&cfg.LintersSettings.Gofumpt, cfg.Run.Go))
3131
}
3232

3333
if _, ok := enabledLinters[goimports.Name]; ok {
34-
m.formatters = append(m.formatters, goimports.New())
34+
m.formatters = append(m.formatters, goimports.New(&cfg.LintersSettings.Goimports))
3535
}
3636

3737
// gci is a last because the only goal of gci is to handle imports.
3838
if _, ok := enabledLinters[gci.Name]; ok {
39-
formatter, err := gci.New(cfg.LintersSettings.Gci)
39+
formatter, err := gci.New(&cfg.LintersSettings.Gci)
4040
if err != nil {
4141
return nil, fmt.Errorf("gci: creating formatter: %w", err)
4242
}

0 commit comments

Comments
 (0)