From 72599abef347f836f48f83c1fc637e30602a55f0 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Sat, 25 Feb 2023 02:06:54 +0100 Subject: [PATCH] Support multiple prefixes to group them together #107 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> --- README.md | 23 +++++++++++-------- cmd/gci/gcicommand.go | 8 +++---- .../testdata/grouped-multiple-custom.cfg.yaml | 4 ++++ .../testdata/grouped-multiple-custom.in.go | 10 ++++++++ .../testdata/grouped-multiple-custom.out.go | 12 ++++++++++ pkg/section/parser_test.go | 5 ++++ pkg/section/prefix.go | 11 +++++++-- 7 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 pkg/gci/internal/testdata/grouped-multiple-custom.cfg.yaml create mode 100644 pkg/gci/internal/testdata/grouped-multiple-custom.in.go create mode 100644 pkg/gci/internal/testdata/grouped-multiple-custom.out.go diff --git a/README.md b/README.md index 6221351..62b4b64 100644 --- a/README.md +++ b/README.md @@ -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" @@ -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. ``` @@ -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. ``` @@ -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. ``` diff --git a/cmd/gci/gcicommand.go b/cmd/gci/gcicommand.go index 4e6185b..8fde5b5 100644 --- a/cmd/gci/gcicommand.go +++ b/cmd/gci/gcicommand.go @@ -49,15 +49,15 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI 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") diff --git a/pkg/gci/internal/testdata/grouped-multiple-custom.cfg.yaml b/pkg/gci/internal/testdata/grouped-multiple-custom.cfg.yaml new file mode 100644 index 0000000..614bfa3 --- /dev/null +++ b/pkg/gci/internal/testdata/grouped-multiple-custom.cfg.yaml @@ -0,0 +1,4 @@ +sections: + - Standard + - Default + - Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0) diff --git a/pkg/gci/internal/testdata/grouped-multiple-custom.in.go b/pkg/gci/internal/testdata/grouped-multiple-custom.in.go new file mode 100644 index 0000000..cc64488 --- /dev/null +++ b/pkg/gci/internal/testdata/grouped-multiple-custom.in.go @@ -0,0 +1,10 @@ +package main + +import ( + "daixiang0/lib1" + "fmt" + "github.com/daixiang0/gci" + "gitlab.com/daixiang0/gci" + g "github.com/golang" + "github.com/daixiang0/gci/subtest" +) diff --git a/pkg/gci/internal/testdata/grouped-multiple-custom.out.go b/pkg/gci/internal/testdata/grouped-multiple-custom.out.go new file mode 100644 index 0000000..3f21ff8 --- /dev/null +++ b/pkg/gci/internal/testdata/grouped-multiple-custom.out.go @@ -0,0 +1,12 @@ +package main + +import ( + "fmt" + + g "github.com/golang" + + "daixiang0/lib1" + "github.com/daixiang0/gci" + "github.com/daixiang0/gci/subtest" + "gitlab.com/daixiang0/gci" +) diff --git a/pkg/section/parser_test.go b/pkg/section/parser_test.go index b8c64b7..0b21e78 100644 --- a/pkg/section/parser_test.go +++ b/pkg/section/parser_test.go @@ -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) diff --git a/pkg/section/prefix.go b/pkg/section/prefix.go index 92ef963..a274347 100644 --- a/pkg/section/prefix.go +++ b/pkg/section/prefix.go @@ -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{} }