Skip to content

Commit 65b147e

Browse files
authored
Support multiple prefixes to group them together #107 (#142)
This changes allows to use this to group multiple prefixes Signed-off-by: ccoVeille <[email protected]>
1 parent 5a4fe84 commit 65b147e

File tree

7 files changed

+57
-16
lines changed

7 files changed

+57
-16
lines changed

README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ GCI, a tool that controls golang package import order and makes it always determ
55
The desired output format is highly configurable and allows for more custom formatting than `goimport` does.
66

77
GCI considers a import block based on AST as below:
8+
89
```
910
Doc
1011
Name Path Comment
1112
```
13+
1214
All comments will keep as they were, except the isolated comment blocks.
1315

1416
The isolated comment blocks like below:
17+
1518
```
1619
import (
1720
"fmt"
@@ -78,9 +81,9 @@ Flags:
7881
-h, --help help for write
7982
-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].
8083
standard - standard section that Golang provides officially, like "fmt"
81-
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
84+
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)
8285
default - default section, contains all rest imports
83-
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled. (default [standard,default])
86+
blank - blank section, contains all blank imports.
8487
--skip-generated Skip generated files
8588
--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.
8689
```
@@ -99,11 +102,11 @@ Flags:
99102
-d, --debug Enables debug output from the formatter
100103
-h, --help help for write
101104
-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].
102-
standard - standard section thatolang provides officially, like "fmt"
103-
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
105+
standard - standard section that Golang provides officially, like "fmt"
106+
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)
104107
default - default section, contains all rest imports
105-
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
106-
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled. (default [standard,default])
108+
blank - blank section, contains all blank imports.
109+
dot - dot section, contains all dot imports.
107110
--skip-generated Skip generated files
108111
--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.
109112
```
@@ -119,11 +122,11 @@ Flags:
119122
-d, --debug Enables debug output from the formatter
120123
-h, --help help for write
121124
-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].
122-
standard - standard section thatolang provides officially, like "fmt"
123-
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
125+
standard - standard section that Golang provides officially, like "fmt"
126+
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)
124127
default - default section, contains all rest imports
125-
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
126-
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled. (default [standard,default])
128+
blank - blank section, contains all blank imports.
129+
dot - dot section, contains all dot imports.
127130
--skip-generated Skip generated files
128131
--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.
129132
```

cmd/gci/gcicommand.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
4949

5050
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].
5151
standard - standard section that Golang provides officially, like "fmt"
52-
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix.
52+
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)
5353
default - default section, contains all rest imports
54-
blank - blank section, contains all blank imports. This section is not presed unless explicitly enabled.
55-
dot - dot section, contains all dot imports. This section is not presed unless explicitly enabled.`
54+
blank - blank section, contains all blank imports.
55+
dot - dot section, contains all dot imports.`
5656

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

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

6262
// deprecated
6363
noInlineComments = cmd.Flags().Bool("NoInlineComments", false, "Drops inline comments while formatting")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sections:
2+
- Standard
3+
- Default
4+
- Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import (
4+
"daixiang0/lib1"
5+
"fmt"
6+
"github.com/daixiang0/gci"
7+
"gitlab.com/daixiang0/gci"
8+
g "github.com/golang"
9+
"github.com/daixiang0/gci/subtest"
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
g "github.com/golang"
7+
8+
"daixiang0/lib1"
9+
"github.com/daixiang0/gci"
10+
"github.com/daixiang0/gci/subtest"
11+
"gitlab.com/daixiang0/gci"
12+
)

pkg/section/parser_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func TestParse(t *testing.T) {
4040
expectedSection: nil,
4141
expectedError: errors.New("invalid params: prefix("),
4242
},
43+
{
44+
input: []string{"prefix(domainA,domainB)"},
45+
expectedSection: SectionList{Custom{"domainA,domainB"}},
46+
expectedError: nil,
47+
},
4348
}
4449
for _, test := range testCases {
4550
parsedSection, err := Parse(test.input)

pkg/section/prefix.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ type Custom struct {
1212
Prefix string
1313
}
1414

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

1721
func (c Custom) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
18-
if strings.HasPrefix(spec.Path, c.Prefix) {
19-
return specificity.Match{Length: len(c.Prefix)}
22+
for _, prefix := range strings.Split(c.Prefix, CustomSeparator) {
23+
if strings.HasPrefix(spec.Path, prefix) {
24+
return specificity.Match{Length: len(prefix)}
25+
}
2026
}
27+
2128
return specificity.MisMatch{}
2229
}
2330

0 commit comments

Comments
 (0)