diff --git a/README.md b/README.md index 9d9a04d..782ac6e 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ Aliases: Flags: --custom-order Enable custom order of sections -d, --debug Enables debug output from the formatter + --format-always Format the file even if no imports blocks are defined. The default value is false. -h, --help help for print -s, --section stringArray 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 > alias. The default value is [standard,default]. standard - standard section that Go provides officially, like "fmt" @@ -113,6 +114,7 @@ Aliases: Flags: --custom-order Enable custom order of sections -d, --debug Enables debug output from the formatter + --format-always Format the file even if no imports blocks are defined. The default value is false. -h, --help help for write -s, --section stringArray 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 > alias. The default value is [standard,default]. standard - standard section that Go provides officially, like "fmt" @@ -157,6 +159,7 @@ Usage: Flags: --custom-order Enable custom order of sections -d, --debug Enables debug output from the formatter + --format-always Format the file even if no imports blocks are defined. The default value is false. -h, --help help for diff -s, --section stringArray 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 > alias. The default value is [standard,default]. standard - standard section that Go provides officially, like "fmt" diff --git a/cmd/gci/gcicommand.go b/cmd/gci/gcicommand.go index a1a2236..5c84427 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, skipVendor, customOrder, debug *bool + var noInlineComments, noPrefixComments, skipGenerated, skipVendor, customOrder, debug, formatAlways *bool var sectionStrings, sectionSeparatorStrings *[]string cmd := cobra.Command{ Use: use, @@ -28,6 +28,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI SkipGenerated: *skipGenerated, SkipVendor: *skipVendor, CustomOrder: *customOrder, + FormatAlways: *formatAlways, } gciCfg, err := config.YamlConfig{Cfg: fmtCfg, SectionStrings: *sectionStrings, SectionSeparatorStrings: *sectionSeparatorStrings}.Parse() if err != nil { @@ -47,6 +48,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 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 > alias. 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 51f6ccf..951e1da 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -25,6 +25,7 @@ type BoolConfig struct { SkipGenerated bool `yaml:"skipGenerated"` SkipVendor bool `yaml:"skipVendor"` CustomOrder bool `yaml:"customOrder"` + FormatAlways bool `yaml:"formatAlways"` } type Config struct { diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index 163e95a..eef3f40 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -140,6 +140,9 @@ func LoadFormat(in []byte, path string, cfg config.Config) (src, dist []byte, er imports, headEnd, tailStart, cStart, cEnd, err := parse.ParseFile(src, path) if err != nil { if errors.Is(err, parse.NoImportError{}) { + if cfg.FormatAlways { + return GoFormat(src) + } return src, src, nil } return nil, nil, err @@ -220,6 +223,15 @@ func LoadFormat(in []byte, path string, cfg config.Config) (src, dist []byte, er 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 +}