diff --git a/README.md b/README.md index 0a2d819..ba82e52 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,27 @@ Flags: --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. ``` + +```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 + +Usage: + gci list path... [flags] + +Flags: + --custom-order Enable custom order of sections + -d, --debug Enables debug output from the formatter + -h, --help help for list + -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 +``` + ```shell $ gci diff -h Diff prints a patch in the style of the diff tool that contains the required changes to the file to make it adhere to the specified formatting. diff --git a/cmd/gci/list.go b/cmd/gci/list.go new file mode 100644 index 0000000..3a42988 --- /dev/null +++ b/cmd/gci/list.go @@ -0,0 +1,16 @@ +package gci + +import ( + "github.com/daixiang0/gci/pkg/gci" +) + +// listCmd represents the list command +func (e *Executor) initList() { + e.newGciCommand( + "list path...", + "Prints filenames that need to be formatted to STDOUT", + "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", + []string{}, + false, + gci.ListUnFormattedFiles) +} diff --git a/cmd/gci/root.go b/cmd/gci/root.go index 289e3d1..e231e4b 100644 --- a/cmd/gci/root.go +++ b/cmd/gci/root.go @@ -42,6 +42,7 @@ func NewExecutor(version string) *Executor { e.initDiff() e.initPrint() e.initWrite() + e.initList() return &e } diff --git a/pkg/gci/gci.go b/pkg/gci/gci.go index 0fd7a0e..81aa977 100644 --- a/pkg/gci/gci.go +++ b/pkg/gci/gci.go @@ -49,6 +49,16 @@ func WriteFormattedFiles(paths []string, cfg config.Config) error { }) } +func ListUnFormattedFiles(paths []string, cfg config.Config) error { + return processGoFilesInPaths(paths, cfg, func(filePath string, unmodifiedFile, formattedFile []byte) error { + if bytes.Equal(unmodifiedFile, formattedFile) { + return nil + } + fmt.Println(filePath) + return nil + }) +} + func DiffFormattedFiles(paths []string, cfg config.Config) error { return processStdInAndGoFilesInPaths(paths, cfg, func(filePath string, unmodifiedFile, formattedFile []byte) error { fileURI := span.URIFromPath(filePath)