Skip to content

Commit

Permalink
Replace cmp.Diff with github.com/pkg/diff for better file diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
nywilken committed Jul 13, 2023
1 parent 1420e61 commit cacfbef
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
12 changes: 5 additions & 7 deletions cmd/packer-sdc/internal/fix/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"path/filepath"
"strings"

"github.com/google/go-cmp/cmp"
"github.com/pkg/diff"
)

const cmdPrefix string = "fix"
Expand Down Expand Up @@ -106,7 +106,7 @@ func processFiles(rootDir string, showDiff bool) error {

var hasErrors error
for _, f := range availableFixes {
matches, err := filepath.Glob(filepath.Join(rootDir, f.fileGlob))
matches, err := f.scan(rootDir)
if err != nil {
return fmt.Errorf("failed to apply %s fix: %s", f.name, err)
}
Expand All @@ -124,12 +124,12 @@ func processFiles(rootDir string, showDiff bool) error {
fixedData = bytes.Clone(srcFiles[filename])
}

fixedData, err := f.Fix(filename, fixedData)
fixedData, err := f.fix(filename, fixedData)
if err != nil {
hasErrors = errors.Join(hasErrors, err)
continue
}
if cmp.Equal(fixedData, srcFiles[filename]) {
if bytes.Equal(fixedData, srcFiles[filename]) {
continue
}
fixedFiles[filename] = bytes.Clone(fixedData)
Expand All @@ -142,9 +142,7 @@ func processFiles(rootDir string, showDiff bool) error {

if showDiff {
for filename, fixedData := range fixedFiles {
fmt.Printf("%s:\n", filename)
// Using cmp Diff is not the greatest look for an alternative or implement custom reporter
fmt.Println(cmp.Diff(srcFiles[filename], fixedData))
diff.Text(filename, filename+"fixed", string(srcFiles[filename]), string(fixedData), os.Stdout)
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/packer-community/winrmcp v0.0.0-20180921211025-c76d91c1e7db
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.13.2
github.com/ryanuber/go-glob v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi
github.com/pierrec/lz4 v2.5.2+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pierrec/lz4 v2.6.1+incompatible h1:9UY3+iC23yxF0UfGaYrGplQ+79Rg+h/q9FV9ix19jjM=
github.com/pierrec/lz4 v2.6.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down

0 comments on commit cacfbef

Please sign in to comment.