Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(format): add --format-always flag #162

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion cmd/gci/gcicommand.go
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions pkg/gci/gci.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/gci/internal/testdata/format-always.cfg.yaml
@@ -0,0 +1 @@
formatAlways: true
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/format-always.in.go
@@ -0,0 +1,5 @@
package main

func SomeFunc() error {
return nil
}
5 changes: 5 additions & 0 deletions pkg/gci/internal/testdata/format-always.out.go
@@ -0,0 +1,5 @@
package main

func SomeFunc() error {
return nil
}