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

add list command #153

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions cmd/gci/list.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions cmd/gci/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewExecutor(version string) *Executor {
e.initDiff()
e.initPrint()
e.initWrite()
e.initList()
return &e
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down