|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "gopkg.in/yaml.v3" |
| 8 | + |
| 9 | + "github.com/daixiang0/gci/pkg/config" |
| 10 | + "github.com/daixiang0/gci/pkg/section" |
| 11 | +) |
| 12 | + |
| 13 | +var defaultOrder = map[string]int{ |
| 14 | + section.StandardType: 0, |
| 15 | + section.DefaultType: 1, |
| 16 | + section.CustomType: 2, |
| 17 | + section.BlankType: 3, |
| 18 | + section.DotType: 4, |
| 19 | + section.AliasType: 5, |
| 20 | + section.LocalModuleType: 6, |
| 21 | +} |
| 22 | + |
| 23 | +type Config struct { |
| 24 | + config.BoolConfig |
| 25 | + Sections section.SectionList |
| 26 | + SectionSeparators section.SectionList |
| 27 | +} |
| 28 | + |
| 29 | +type YamlConfig struct { |
| 30 | + Cfg config.BoolConfig `yaml:",inline"` |
| 31 | + SectionStrings []string `yaml:"sections"` |
| 32 | + SectionSeparatorStrings []string `yaml:"sectionseparators"` |
| 33 | + |
| 34 | + // Since history issue, Golangci-lint needs Analyzer to run and GCI add an Analyzer layer to integrate. |
| 35 | + // The ModPath param is only from analyzer.go, no need to set it in all other places. |
| 36 | + ModPath string `yaml:"-"` |
| 37 | +} |
| 38 | + |
| 39 | +func (g YamlConfig) Parse() (*Config, error) { |
| 40 | + var err error |
| 41 | + |
| 42 | + sections, err := section.Parse(g.SectionStrings) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + if sections == nil { |
| 47 | + sections = section.DefaultSections() |
| 48 | + } |
| 49 | + if err := configureSections(sections, g.ModPath); err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + // if default order sorted sections |
| 54 | + if !g.Cfg.CustomOrder { |
| 55 | + sort.Slice(sections, func(i, j int) bool { |
| 56 | + sectionI, sectionJ := sections[i].Type(), sections[j].Type() |
| 57 | + |
| 58 | + if g.Cfg.NoLexOrder || strings.Compare(sectionI, sectionJ) != 0 { |
| 59 | + return defaultOrder[sectionI] < defaultOrder[sectionJ] |
| 60 | + } |
| 61 | + |
| 62 | + return strings.Compare(sections[i].String(), sections[j].String()) < 0 |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + sectionSeparators, err := section.Parse(g.SectionSeparatorStrings) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + if sectionSeparators == nil { |
| 71 | + sectionSeparators = section.DefaultSectionSeparators() |
| 72 | + } |
| 73 | + |
| 74 | + return &Config{g.Cfg, sections, sectionSeparators}, nil |
| 75 | +} |
| 76 | + |
| 77 | +func ParseConfig(in string) (*Config, error) { |
| 78 | + config := YamlConfig{} |
| 79 | + |
| 80 | + err := yaml.Unmarshal([]byte(in), &config) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + gciCfg, err := config.Parse() |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + return gciCfg, nil |
| 91 | +} |
| 92 | + |
| 93 | +// configureSections now only do golang module path finding. |
| 94 | +// Since history issue, Golangci-lint needs Analyzer to run and GCI add an Analyzer layer to integrate. |
| 95 | +// The path param is from analyzer.go, in all other places should pass empty string. |
| 96 | +func configureSections(sections section.SectionList, path string) error { |
| 97 | + for _, sec := range sections { |
| 98 | + switch s := sec.(type) { |
| 99 | + case *section.LocalModule: |
| 100 | + if err := s.Configure(path); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return nil |
| 106 | +} |
0 commit comments