Skip to content

Commit

Permalink
Add flag to allow multiple prefix() sections to not be automatically …
Browse files Browse the repository at this point in the history
…sorted (#210)

* Add no lex order for custom sections

Signed-off-by: DanWlker <[email protected]>

* Add tests

Signed-off-by: DanWlker <[email protected]>

* Invert check to decrease steps

Signed-off-by: DanWlker <[email protected]>

---------

Signed-off-by: DanWlker <[email protected]>
  • Loading branch information
DanWlker authored Aug 26, 2024
1 parent 5969d4e commit 5e7f3e5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/gci/gcicommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type processingFunc = func(args []string, gciCfg config.Config) error

func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdInSupport bool, processingFunc processingFunc) *cobra.Command {
var noInlineComments, noPrefixComments, skipGenerated, skipVendor, customOrder, debug *bool
var noInlineComments, noPrefixComments, skipGenerated, skipVendor, customOrder, noLexOrder, debug *bool
var sectionStrings, sectionSeparatorStrings *[]string
cmd := cobra.Command{
Use: use,
Expand All @@ -28,6 +28,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
SkipGenerated: *skipGenerated,
SkipVendor: *skipVendor,
CustomOrder: *customOrder,
NoLexOrder: *noLexOrder,
}
gciCfg, err := config.YamlConfig{Cfg: fmtCfg, SectionStrings: *sectionStrings, SectionSeparatorStrings: *sectionSeparatorStrings}.Parse()
if err != nil {
Expand Down Expand Up @@ -61,6 +62,7 @@ localmodule: localmodule section, contains all imports from local packages`
skipVendor = cmd.Flags().Bool("skip-vendor", false, "Skip files inside vendor directory")

customOrder = cmd.Flags().Bool("custom-order", false, "Enable custom order of sections")
noLexOrder = cmd.Flags().Bool("no-lex-order", false, "Drops lexical ordering for custom sections")
sectionStrings = cmd.Flags().StringArrayP("section", "s", section.DefaultSections().String(), sectionHelp)

// deprecated
Expand Down
4 changes: 4 additions & 0 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func generateCmdLine(cfg config.Config) string {
result += " --custom-order "
}

if cfg.BoolConfig.NoLexOrder {
result += " --no-lex-order"
}

for _, s := range cfg.Sections.String() {
result += fmt.Sprintf(" --Section \"%s\" ", s)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type BoolConfig struct {
SkipGenerated bool `yaml:"skipGenerated"`
SkipVendor bool `yaml:"skipVendor"`
CustomOrder bool `yaml:"customOrder"`
NoLexOrder bool `yaml:"noLexOrder"`
}

type Config struct {
Expand Down Expand Up @@ -63,10 +64,11 @@ func (g YamlConfig) Parse() (*Config, error) {
sort.Slice(sections, func(i, j int) bool {
sectionI, sectionJ := sections[i].Type(), sections[j].Type()

if strings.Compare(sectionI, sectionJ) == 0 {
return strings.Compare(sections[i].String(), sections[j].String()) < 0
if g.Cfg.NoLexOrder || strings.Compare(sectionI, sectionJ) != 0 {
return defaultOrder[sectionI] < defaultOrder[sectionJ]
}
return defaultOrder[sectionI] < defaultOrder[sectionJ]

return strings.Compare(sections[i].String(), sections[j].String()) < 0
})
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ func TestParseCustomOrder(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, section.SectionList{section.Default{}, section.Custom{Prefix: "github/daixiang0/gci"}, section.Custom{Prefix: "github/daixiang0/gai"}}, gciCfg.Sections)
}

func TestParseNoLexOrder(t *testing.T) {
cfg := YamlConfig{
SectionStrings: []string{"prefix(github/daixiang0/gci)", "prefix(github/daixiang0/gai)", "default"},
Cfg: BoolConfig{
NoLexOrder: true,
},
}

gciCfg, err := cfg.Parse()
assert.NoError(t, err)
assert.Equal(t, section.SectionList{section.Default{}, section.Custom{Prefix: "github/daixiang0/gci"}, section.Custom{Prefix: "github/daixiang0/gai"}}, gciCfg.Sections)
}

0 comments on commit 5e7f3e5

Please sign in to comment.