Skip to content

Commit

Permalink
feat(format): add --format-always flag
Browse files Browse the repository at this point in the history
Signed-off-by: Jamy Timmermans <jamy@uber.com>
  • Loading branch information
JamyDev committed Jul 13, 2023
1 parent b9a2597 commit 516a01f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
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, customOrder, debug *bool
var noInlineComments, noPrefixComments, skipGenerated, customOrder, debug, formatAlways *bool
var sectionStrings, sectionSeparatorStrings *[]string
cmd := cobra.Command{
Use: use,
Expand All @@ -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 {
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions pkg/gci/gci.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
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
}

0 comments on commit 516a01f

Please sign in to comment.