Skip to content

Commit

Permalink
add flag for skipping files inside vendor directory (#139)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Rychlewicz <[email protected]>
  • Loading branch information
artuross authored Aug 8, 2023
1 parent b9a2597 commit ef37fdd
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 48 deletions.
94 changes: 56 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
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 All @@ -28,6 +28,7 @@ import (
```

GCI splits all import blocks into different sections, now support five section type:

- standard: Go official imports, like "fmt"
- custom: Custom section, use full and the longest match (match full string first, if multiple matches, use the longest one)
- default: All rest import blocks
Expand Down Expand Up @@ -64,14 +65,14 @@ go install github.com/daixiang0/[email protected]
Now GCI provides two command line methods, mainly for backward compatibility.

### New style

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
$ gci print -h
Expand All @@ -84,15 +85,17 @@ Aliases:
print, output

Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section strings 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"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
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.
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-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. The default value is [standard,default].
standard - standard section that Go provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
```

```shell
Expand All @@ -106,19 +109,19 @@ Aliases:
write, overwrite

Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section strings 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"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
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.
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-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. The default value is [standard,default].
standard - standard section that Go provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
```


```shell
$ gci list -h
Prints the filenames that need to be formatted. If you want to show the diff use diff instead, and if you want to apply the changes use write instead
Expand All @@ -137,6 +140,7 @@ Flags:
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
```
```shell
Expand All @@ -147,23 +151,36 @@ Usage:
gci diff path... [flags]

Flags:
-d, --debug Enables debug output from the formatter
-h, --help help for write
-s, --section strings 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"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
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.
--custom-order Enable custom order of sections
-d, --debug Enables debug output from the formatter
-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. The default value is [standard,default].
standard - standard section that Go provides officially, like "fmt"
Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
default - default section, contains all rest imports
blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports. (default [standard,default])
--skip-generated Skip generated files
--skip-vendor Skip files inside vendor directory
```
### Old style
```shell
Gci enables automatic formatting of imports in a deterministic manner
If you want to integrate this as part of your CI take a look at golangci-lint.

Usage:
gci [-diff | -write] [--local localPackageURLs] path... [flags]
gci [command]

Available Commands:
completion Generate the autocompletion script for the specified shell
diff Prints a git style diff to STDOUT
help Help about any command
list Prints filenames that need to be formatted to STDOUT
print Outputs the formatted file to STDOUT
write Formats the specified files in-place

Flags:
-d, --diff display diffs instead of rewriting files
Expand All @@ -172,6 +189,7 @@ Flags:
-v, --version version for gci
-w, --write write result to (source) file instead of stdout

Use "gci [command] --help" for more information about a command.
```
**Note**::
Expand All @@ -188,9 +206,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
Original file line number Diff line number Diff line change
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, skipVendor, customOrder, debug *bool
var sectionStrings, sectionSeparatorStrings *[]string
cmd := cobra.Command{
Use: use,
Expand All @@ -26,6 +26,7 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
NoPrefixComments: *noPrefixComments,
Debug: *debug,
SkipGenerated: *skipGenerated,
SkipVendor: *skipVendor,
CustomOrder: *customOrder,
}
gciCfg, err := config.YamlConfig{Cfg: fmtCfg, SectionStrings: *sectionStrings, SectionSeparatorStrings: *sectionSeparatorStrings}.Parse()
Expand Down Expand Up @@ -55,6 +56,7 @@ blank - blank section, contains all blank imports.
dot - dot section, contains all dot imports.`

skipGenerated = cmd.Flags().Bool("skip-generated", false, "Skip generated files")
skipVendor = cmd.Flags().Bool("skip-vendor", false, "Skip files inside vendor directory")

customOrder = cmd.Flags().Bool("custom-order", false, "Enable custom order of sections")
sectionStrings = cmd.Flags().StringArrayP("section", "s", section.DefaultSections().String(), sectionHelp)
Expand Down
1 change: 1 addition & 0 deletions cmd/gci/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (e *Executor) runInCompatibilityMode(cmd *cobra.Command, args []string) err
NoPrefixComments: false,
Debug: false,
SkipGenerated: false,
SkipVendor: false,
},
Sections: sections,
SectionSeparators: sectionSeparators,
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type BoolConfig struct {
NoPrefixComments bool `yaml:"no-prefixComments"`
Debug bool `yaml:"-"`
SkipGenerated bool `yaml:"skipGenerated"`
SkipVendor bool `yaml:"skipVendor"`
CustomOrder bool `yaml:"customOrder"`
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func DiffFormattedFilesToArray(paths []string, cfg config.Config, diffs *[]strin
type fileFormattingFunc func(filePath string, unmodifiedFile, formattedFile []byte) error

func processStdInAndGoFilesInPaths(paths []string, cfg config.Config, fileFunc fileFormattingFunc) error {
return ProcessFiles(io.StdInGenerator.Combine(io.GoFilesInPathsGenerator(paths)), cfg, fileFunc)
return ProcessFiles(io.StdInGenerator.Combine(io.GoFilesInPathsGenerator(paths, cfg.SkipVendor)), cfg, fileFunc)
}

func processGoFilesInPaths(paths []string, cfg config.Config, fileFunc fileFormattingFunc) error {
return ProcessFiles(io.GoFilesInPathsGenerator(paths), cfg, fileFunc)
return ProcessFiles(io.GoFilesInPathsGenerator(paths, cfg.SkipVendor), cfg, fileFunc)
}

func ProcessFiles(fileGenerator io.FileGeneratorFunc, cfg config.Config, fileFunc fileFormattingFunc) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gci/gci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func init() {

var testFilesPath = "internal/testdata"

func isTestInputFile(file os.FileInfo) bool {
func isTestInputFile(_ string, file os.FileInfo) bool {
return !file.IsDir() && strings.HasSuffix(file.Name(), ".in.go")
}

Expand Down
9 changes: 7 additions & 2 deletions pkg/io/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ func (a FileGeneratorFunc) Combine(b FileGeneratorFunc) FileGeneratorFunc {
}
}

func GoFilesInPathsGenerator(paths []string) FileGeneratorFunc {
return FilesInPathsGenerator(paths, isGoFile)
func GoFilesInPathsGenerator(paths []string, skipVendor bool) FileGeneratorFunc {
checkFunc := isGoFile
if skipVendor {
checkFunc = checkChains(isGoFile, isOutsideVendorDir)
}

return FilesInPathsGenerator(paths, checkFunc)
}

func FilesInPathsGenerator(paths []string, fileCheckFun fileCheckFunction) FileGeneratorFunc {
Expand Down
38 changes: 34 additions & 4 deletions pkg/io/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"path/filepath"
)

type fileCheckFunction func(file os.FileInfo) bool
type fileCheckFunction func(path string, file os.FileInfo) bool

func FindFilesForPath(path string, fileCheckFun fileCheckFunction) ([]string, error) {
switch entry, err := os.Stat(path); {
case err != nil:
return nil, err
case entry.IsDir():
return findFilesForDirectory(path, fileCheckFun)
case fileCheckFun(entry):
case fileCheckFun(path, entry):
return []string{filepath.Clean(path)}, nil
default:
return []string{}, nil
Expand All @@ -31,7 +31,7 @@ func findFilesForDirectory(dirPath string, fileCheckFun fileCheckFunction) ([]st
if err != nil {
return err
}
if !entry.IsDir() && fileCheckFun(file) {
if !entry.IsDir() && fileCheckFun(path, file) {
filePaths = append(filePaths, filepath.Clean(path))
}
return nil
Expand All @@ -42,6 +42,36 @@ func findFilesForDirectory(dirPath string, fileCheckFun fileCheckFunction) ([]st
return filePaths, nil
}

func isGoFile(file os.FileInfo) bool {
func isGoFile(_ string, file os.FileInfo) bool {
return !file.IsDir() && filepath.Ext(file.Name()) == ".go"
}

func isOutsideVendorDir(path string, _ os.FileInfo) bool {
for {
base := filepath.Base(path)
if base == "vendor" {
return false
}

prevPath := path
path = filepath.Dir(path)

if prevPath == path {
break
}
}

return true
}

func checkChains(funcs ...fileCheckFunction) fileCheckFunction {
return func(path string, file os.FileInfo) bool {
for _, checkFunc := range funcs {
if !checkFunc(path, file) {
return false
}
}

return true
}
}

0 comments on commit ef37fdd

Please sign in to comment.