Skip to content

Commit a537661

Browse files
authored
feat: cli print names of unformatted files (#9)
1 parent c607865 commit a537661

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

.golangci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ linters:
3838
- fmt.Fprintf
3939
- fmt.Fprintln
4040

41+
gomoddirectives:
42+
replace-local: true
43+
4144
goheader:
4245
template: |-
4346
Copyright Josh Komoroske. All rights reserved.

cmd/command.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
package cmd
88

99
import (
10+
"bytes"
1011
"fmt"
1112
"os"
1213
"path/filepath"
1314
"strings"
1415

1516
"github.com/joshdk/buildversion"
1617
"github.com/spf13/cobra"
18+
19+
"github.com/joshdk/modfmt/pkg/modfmt"
1720
)
1821

1922
// Command returns a complete command line handler for modfmt.
@@ -47,7 +50,22 @@ func Command() *cobra.Command {
4750
}
4851

4952
for _, filename := range filenames {
50-
fmt.Println(filename)
53+
// Read the original file.
54+
original, err := os.ReadFile(filename)
55+
if err != nil {
56+
return err
57+
}
58+
59+
// Format the file.
60+
formatted, err := modfmt.Format(filename, original)
61+
if err != nil {
62+
return err
63+
}
64+
65+
// Did formatting change the file or was it already formatted?
66+
if !bytes.Equal(original, formatted) {
67+
fmt.Println(filename)
68+
}
5169
}
5270

5371
return nil

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ go 1.23.0
44

55
require (
66
github.com/joshdk/buildversion v0.1.0
7+
github.com/joshdk/modfmt/pkg/modfmt v0.0.0-00010101000000-000000000000
78
github.com/spf13/cobra v1.10.1
89
)
910

1011
require (
1112
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1213
github.com/spf13/pflag v1.0.9 // indirect
14+
golang.org/x/mod v0.27.0 // indirect
15+
)
16+
17+
replace (
18+
github.com/joshdk/modfmt/pkg/modfmt => ./pkg/modfmt
1319
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
88
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
99
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
1010
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
11+
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
12+
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
1113
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1214
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)