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 3 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
16 changes: 9 additions & 7 deletions README.md
Expand Up @@ -17,7 +17,7 @@ The isolated comment blocks like below:

```
import (
"fmt"
"fmt"
// this line is isolated comment

// those lines belong to one
Expand Down Expand Up @@ -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
Expand All @@ -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 blocks are defined. The default value is false.
JamyDev marked this conversation as resolved.
Show resolved Hide resolved
```

```shell
Expand Down Expand Up @@ -157,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
Expand Down Expand Up @@ -188,9 +190,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"
)
```
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, 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("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"
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
}