Skip to content
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
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ linters:
- forbidigo
- gochecknoglobals
- gosec
- mnd
- noinlineerr
- varnamelen
- wrapcheck
Expand Down
36 changes: 33 additions & 3 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// Command returns a complete command line handler for modfmt.
func Command() *cobra.Command {
func Command() *cobra.Command { //nolint:cyclop,funlen
cmd := &cobra.Command{
Use: "modfmt [directory|file]",
Long: "modfmt - formatter for go.mod and go.work files",
Expand All @@ -43,6 +43,18 @@ func Command() *cobra.Command {
false,
"exit with code 1 if any files were unformatted")

// Define --list/-l flag.
list := cmd.Flags().BoolP(
"list", "l",
false,
"list any files that were unformatted")

// Define --write/-w flag.
write := cmd.Flags().BoolP(
"write", "w",
false,
"write result to (source) file instead of stdout")

cmd.RunE = func(_ *cobra.Command, args []string) error {
// If no arguments are given, default to recursively searching through
// the current working directory.
Expand Down Expand Up @@ -72,10 +84,28 @@ func Command() *cobra.Command {
}

// Did formatting change the file or was it already formatted?
if !bytes.Equal(original, formatted) {
unformatted = true
if bytes.Equal(original, formatted) {
continue
}

unformatted = true

if *write {
// If write mode was requested, then silently update the
// original file.
if err := os.WriteFile(filename, formatted, 0o644); err != nil {
return err
}
}

if *list {
// If list mode was requested, then list files.
fmt.Println(filename)
} else if !*write {
// Otherwise if write mode was not requested, then print
// filename header and formatted body.
fmt.Printf("--- %s ---\n", filename)
fmt.Print(string(formatted))
}
}

Expand Down
Loading