Skip to content

Commit

Permalink
Support multiple prefixes to group them together #107
Browse files Browse the repository at this point in the history
This changes allows to use this to group multiple prefixes

gci write -s standard -s default -s 'Prefix(alt.code.company.com,code.company.com)' --custom-order --skip-generated

In order to provide this, a change was needed in gci --section parameter parsing
The code was using pflag.StringSliceP for parsing sections.

Please consider the pflag documentation:

> Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
> For example: --ss="v1,v2" --ss="v3"

While the usage of StringSliceP was legitimate with -local legacy parameter, if we consider gci documentation:

> -l, --local strings   put imports beginning with this string after 3rd-party packages, separate imports by comma

We can assume that when gci was rewritten in its "new style", it appears it was unintended to keep using pflag.StringSliceP.

Switching back to StringArrayP allows us to use comma in CLI arguments,
without this change the following command would have failed:

gci write -s standard -s default -s 'Prefix(alt.code.company.com,code.company.com)' --custom-order --skip-generated

because pflag.StringSliceP would have considered it as:

gci write -s standard -s default -s 'Prefix(alt.code.company.com' -s 'code.company.com)' --custom-order --skip-generated

We can assume this change will be OK. But it will break the following
command:

gci write -s standard,default

It was working because of StringSliceP

The syntax is now gci write -s standard -s default as documented

Signed-off-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
  • Loading branch information
ccoVeille committed Feb 28, 2023
1 parent 5a4fe84 commit b0fe436
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 17 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ GCI, a tool that controls golang package import order and makes it always determ
The desired output format is highly configurable and allows for more custom formatting than `goimport` does.

GCI considers a import block based on AST as below:

```
Doc
Name Path Comment
```

All comments will keep as they were, except the isolated comment blocks.

The isolated comment blocks like below:

```
import (
"fmt"
Expand Down Expand Up @@ -78,9 +81,9 @@ Flags:
-h, --help help for write
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
standard - standard section that Golang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0;daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled. (default [standard,default])
blank - blank section, contains all blank imports.
--skip-generated Skip generated files
--custom-order Enable custom order of sections. If specified, make the section order the same as your configuration order. The default order is standard > default > custom > blank > dot.
```
Expand All @@ -99,11 +102,11 @@ Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
standard - standard section thatolang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
standard - standard section that Golang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0;daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled. (default [standard,default])
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports.
--skip-generated Skip generated files
--custom-order Enable custom order of sections. If specified, make the section order the same as your configuration order. The default order is standard > default > custom > blank > dot.
```
Expand All @@ -119,11 +122,11 @@ Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section strings Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
standard - standard section thatolang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
standard - standard section that Golang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0;daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled. (default [standard,default])
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports.
--skip-generated Skip generated files
--custom-order Enable custom order of sections. If specified, make the section order the same as your configuration order. The default order is standard > default > custom > blank > dot.
```
Expand Down
10 changes: 5 additions & 5 deletions cmd/gci/gcicommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI

debug = cmd.Flags().BoolP("debug", "d", false, "Enables debug output from the formatter")

sectionHelp := `Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
sectionHelp := `Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom... > blank > dot. The default value is [standard,default].
standard - standard section that Golang provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled.`
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports.`

skipGenerated = cmd.Flags().Bool("skip-generated", false, "Skip generated files")

customOrder = cmd.Flags().Bool("custom-order", false, "Enable custom order of sections")
sectionStrings = cmd.Flags().StringSliceP("section", "s", section.DefaultSections().String(), sectionHelp)
sectionStrings = cmd.Flags().StringArrayP("section", "s", section.DefaultSections().String(), sectionHelp)

// deprecated
noInlineComments = cmd.Flags().Bool("NoInlineComments", false, "Drops inline comments while formatting")
Expand Down
4 changes: 4 additions & 0 deletions pkg/gci/internal/testdata/grouped-multiple-custom.cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sections:
- Standard
- Default
- Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
11 changes: 11 additions & 0 deletions pkg/gci/internal/testdata/grouped-multiple-custom.in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
// let's assume the code depends on code on on github, gitlab but also a local package
"daixiang0/lib1"
"fmt"
"github.com/daixiang0/gci"
"gitlab.com/daixiang0/gci"
g "github.com/golang"
"github.com/daixiang0/gci/subtest"
)
13 changes: 13 additions & 0 deletions pkg/gci/internal/testdata/grouped-multiple-custom.out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"

g "github.com/golang"

// let's assume the code depends on code on on github, gitlab but also a local package
"daixiang0/lib1"
"github.com/daixiang0/gci"
"github.com/daixiang0/gci/subtest"
"gitlab.com/daixiang0/gci"
)
5 changes: 5 additions & 0 deletions pkg/section/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func TestParse(t *testing.T) {
expectedSection: nil,
expectedError: errors.New("invalid params: prefix("),
},
{
input: []string{"prefix(domainA,domainB)"},
expectedSection: SectionList{Custom{"domainA,domainB"}},
expectedError: nil,
},
}
for _, test := range testCases {
parsedSection, err := Parse(test.input)
Expand Down
11 changes: 9 additions & 2 deletions pkg/section/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ type Custom struct {
Prefix string
}

// CustomSeparator allows you to group multiple custom prefix together in the same section
// gci diff -s standard -s default -s prefix(github.com/company,gitlab.com/company,companysuffix)
const CustomSeparator = ","

const CustomType = "custom"

func (c Custom) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
if strings.HasPrefix(spec.Path, c.Prefix) {
return specificity.Match{Length: len(c.Prefix)}
for _, prefix := range strings.Split(c.Prefix, CustomSeparator) {
if strings.HasPrefix(spec.Path, prefix) {
return specificity.Match{Length: len(prefix)}
}
}

return specificity.MisMatch{}
}

Expand Down

0 comments on commit b0fe436

Please sign in to comment.