From 516a01f5d505ea37083defb50a33d37bae039d72 Mon Sep 17 00:00:00 2001 From: Jamy Timmermans Date: Thu, 13 Jul 2023 17:33:31 +0000 Subject: [PATCH 1/4] feat(format): add `--format-always` flag Signed-off-by: Jamy Timmermans --- cmd/gci/gcicommand.go | 4 +++- pkg/config/config.go | 1 + pkg/gci/gci.go | 12 ++++++++++++ pkg/gci/internal/testdata/format-always.cfg.yaml | 1 + pkg/gci/internal/testdata/format-always.in.go | 5 +++++ pkg/gci/internal/testdata/format-always.out.go | 5 +++++ 6 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 pkg/gci/internal/testdata/format-always.cfg.yaml create mode 100644 pkg/gci/internal/testdata/format-always.in.go create mode 100644 pkg/gci/internal/testdata/format-always.out.go diff --git a/cmd/gci/gcicommand.go b/cmd/gci/gcicommand.go index 4cae03d..1795b12 100644 --- a/cmd/gci/gcicommand.go +++ b/cmd/gci/gcicommand.go @@ -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, customOrder, debug *bool + var noInlineComments, noPrefixComments, skipGenerated, customOrder, debug, formatAlways *bool var sectionStrings, sectionSeparatorStrings *[]string cmd := cobra.Command{ Use: use, @@ -27,6 +27,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI Debug: *debug, SkipGenerated: *skipGenerated, CustomOrder: *customOrder, + FormatAlways: *formatAlways, } gciCfg, err := config.YamlConfig{Cfg: fmtCfg, SectionStrings: *sectionStrings, SectionSeparatorStrings: *sectionSeparatorStrings}.Parse() if err != nil { @@ -46,6 +47,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI e.rootCmd.AddCommand(&cmd) debug = cmd.Flags().BoolP("debug", "d", false, "Enables debug output from the formatter") + formatAlways = cmd.Flags().Bool("always-format", false, "Format the file even if no imports are defined.") 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 Go provides officially, like "fmt" diff --git a/pkg/config/config.go b/pkg/config/config.go index b32148b..19f746d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -24,6 +24,7 @@ type BoolConfig struct { Debug bool `yaml:"-"` SkipGenerated bool `yaml:"skipGenerated"` CustomOrder bool `yaml:"customOrder"` + FormatAlways bool `yaml:"formatAlways"` } type Config struct { diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index 81aa977..0ba2614 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -134,6 +134,9 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err imports, headEnd, tailStart, cStart, cEnd, err := parse.ParseFile(src, file.Path()) if err != nil { if errors.Is(err, parse.NoImportError{}) { + if cfg.FormatAlways { + return GoFormat(src) + } return src, src, nil } return nil, nil, err @@ -210,6 +213,15 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err return src, dist, nil } +func GoFormat(src []byte) ([]byte, []byte, error) { + dist, err := goFormat.Source(src) + if err != nil { + return nil, nil, err + } + + return src, dist, nil +} + func AddIndent(in *[]byte, first *bool) { if *first { *first = false diff --git a/pkg/gci/internal/testdata/format-always.cfg.yaml b/pkg/gci/internal/testdata/format-always.cfg.yaml new file mode 100644 index 0000000..66c9c5b --- /dev/null +++ b/pkg/gci/internal/testdata/format-always.cfg.yaml @@ -0,0 +1 @@ +formatAlways: true diff --git a/pkg/gci/internal/testdata/format-always.in.go b/pkg/gci/internal/testdata/format-always.in.go new file mode 100644 index 0000000..664be31 --- /dev/null +++ b/pkg/gci/internal/testdata/format-always.in.go @@ -0,0 +1,5 @@ +package main + +func SomeFunc() error { +return nil +} diff --git a/pkg/gci/internal/testdata/format-always.out.go b/pkg/gci/internal/testdata/format-always.out.go new file mode 100644 index 0000000..357e01f --- /dev/null +++ b/pkg/gci/internal/testdata/format-always.out.go @@ -0,0 +1,5 @@ +package main + +func SomeFunc() error { + return nil +} From 52cc28f949d32ebe2e34dce6eb7cd11232c6f235 Mon Sep 17 00:00:00 2001 From: Jamy Timmermans Date: Mon, 17 Jul 2023 08:58:42 -0700 Subject: [PATCH 2/4] fixup! add readme + flag key update Signed-off-by: Jamy Timmermans --- README.md | 15 ++++++++------- cmd/gci/gcicommand.go | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8181194..ea41455 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ The isolated comment blocks like below: ``` import ( - "fmt" + "fmt" // this line is isolated comment // those lines belong to one @@ -67,10 +67,10 @@ Now GCI provides two command line methods, mainly for backward compatibility. GCI supports three modes of operation > **Note** -> -> Since v0.10.0, the `-s` and `--section` flag can only be used multiple times to specify multiple sections. -> For example, you could use `-s standard,default` before, but now you must use `-s standard -s default`. -> This breaking change makes it possible for the project to support specifying multiple custom prefixes. (Please see below.) +> +> Since v0.10.0, the `-s` and `--section` flag can only be used multiple times to specify multiple sections. +> For example, you could use `-s standard,default` before, but now you must use `-s standard -s default`. +> This breaking change makes it possible for the project to support specifying multiple custom prefixes. (Please see below.) ```shell @@ -93,6 +93,7 @@ Flags: 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. + --format-always Format the file even if no imports are defined. By default, formatting doesn't run when there are <= 1 imports. ``` ```shell @@ -188,9 +189,9 @@ Run `gci write -s standard -s default -s "prefix(github.com/daixiang0/gci)" main package main import ( "golang.org/x/tools" - + "fmt" - + "github.com/daixiang0/gci" ) ``` diff --git a/cmd/gci/gcicommand.go b/cmd/gci/gcicommand.go index 1795b12..deb9917 100644 --- a/cmd/gci/gcicommand.go +++ b/cmd/gci/gcicommand.go @@ -47,7 +47,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI e.rootCmd.AddCommand(&cmd) debug = cmd.Flags().BoolP("debug", "d", false, "Enables debug output from the formatter") - formatAlways = cmd.Flags().Bool("always-format", false, "Format the file even if no imports are defined.") + formatAlways = cmd.Flags().Bool("format-always", false, "Format the file even if no imports are defined.") 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 Go provides officially, like "fmt" From 7e1a2caa9a6eeebd76ca5d6e4623c8fc41c03c74 Mon Sep 17 00:00:00 2001 From: Jamy Timmermans Date: Mon, 17 Jul 2023 22:14:00 -0700 Subject: [PATCH 3/4] fix! update docs Signed-off-by: Jamy Timmermans --- README.md | 3 ++- cmd/gci/gcicommand.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ea41455..dde6a77 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ Flags: 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. - --format-always Format the file even if no imports are defined. By default, formatting doesn't run when there are <= 1 imports. + --format-always Format the file even if no imports blocks are defined. The default value is false. ``` ```shell @@ -158,6 +158,7 @@ Flags: 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. + --format-always Format the file even if no imports blocks are defined. The default value is false. ``` ### Old style diff --git a/cmd/gci/gcicommand.go b/cmd/gci/gcicommand.go index deb9917..87819a7 100644 --- a/cmd/gci/gcicommand.go +++ b/cmd/gci/gcicommand.go @@ -47,7 +47,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI e.rootCmd.AddCommand(&cmd) debug = cmd.Flags().BoolP("debug", "d", false, "Enables debug output from the formatter") - formatAlways = cmd.Flags().Bool("format-always", false, "Format the file even if no imports are defined.") + formatAlways = cmd.Flags().Bool("format-always", false, "Format the file even if no imports blocks are defined. The default value is false.") 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 Go provides officially, like "fmt" From a45d2945949c0e3b45b751ed1f054a308cab656f Mon Sep 17 00:00:00 2001 From: Jamy Timmermans Date: Tue, 18 Jul 2023 21:18:06 -0700 Subject: [PATCH 4/4] fixup! comment Signed-off-by: Jamy Timmermans --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dde6a77..496c5fc 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ Flags: 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. + --format-always Format the file even if no imports blocks are defined. The default value is false. ```