Skip to content

Commit a46e11d

Browse files
feat: implement --show-diff for rust, php, and js languages (#100)
Summary The --show-diff flag was only implemented for Go. Rust, PHP, and JS all copied the flag into their internal config types but never read it — --show-diff was silently ignored. This adds the same before/after snapshot + cmp.Diff pattern to all three: Rust: diffs Cargo.lock before and after DoUpdate JS: diffs package.json before and after ApplyOverrides PHP: diffs all manifest files (from the detected build tool) before and after buildTool.Update Follows the existing Go implementation in pkg/languages/golang/updater.go. 🤖 Generated with Claude Code
1 parent 80250b9 commit a46e11d

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

pkg/languages/js/js.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/chainguard-dev/clog"
1616
"github.com/chainguard-dev/omnibump/pkg/languages"
17+
"github.com/google/go-cmp/cmp"
1718
)
1819

1920
// PackageJSON is the canonical manifest filename for JavaScript projects.
@@ -105,10 +106,22 @@ func (j *JS) Update(ctx context.Context, cfg *languages.UpdateConfig) error {
105106
return nil
106107
}
107108

109+
var originalContent []byte
110+
if cfg.ShowDiff {
111+
originalContent, _ = os.ReadFile(pkgPath) //nolint:gosec // pkgPath validated by os.Stat above
112+
}
113+
108114
if err := ApplyOverrides(pkgPath, managers, overrides); err != nil {
109115
return fmt.Errorf("apply overrides: %w", err)
110116
}
111117

118+
if cfg.ShowDiff && originalContent != nil {
119+
newContent, _ := os.ReadFile(pkgPath) //nolint:gosec // pkgPath validated by os.Stat above
120+
if diff := cmp.Diff(string(originalContent), string(newContent)); diff != "" {
121+
log.Infof("Diff for %s:\n%s", pkgPath, diff)
122+
}
123+
}
124+
112125
log.Infof("Successfully applied overrides to %s", pkgPath)
113126
return nil
114127
}

pkg/languages/php/php.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import (
99
"context"
1010
"errors"
1111
"fmt"
12+
"os"
13+
"path/filepath"
1214

1315
"github.com/chainguard-dev/clog"
1416
"github.com/chainguard-dev/omnibump/pkg/languages"
1517
"github.com/chainguard-dev/omnibump/pkg/languages/php/composer"
18+
"github.com/google/go-cmp/cmp"
1619
)
1720

1821
// ErrNoBuildToolFound indicates no supported PHP build tool was detected.
@@ -80,8 +83,33 @@ func (p *PHP) Update(ctx context.Context, cfg *languages.UpdateConfig) error {
8083

8184
log.Infof("Detected PHP build tool: %s", p.buildTool.Name())
8285

86+
// Snapshot manifest files for --show-diff.
87+
var snapshots map[string][]byte
88+
if cfg.ShowDiff {
89+
snapshots = make(map[string][]byte)
90+
for _, name := range p.buildTool.GetManifestFiles() {
91+
path := filepath.Join(cfg.RootDir, name)
92+
if data, err := os.ReadFile(path); err == nil { //nolint:gosec // path built from cfg.RootDir + known manifest filenames
93+
snapshots[path] = data
94+
}
95+
}
96+
}
97+
8398
// Delegate to the build tool
84-
return p.buildTool.Update(ctx, cfg)
99+
if err := p.buildTool.Update(ctx, cfg); err != nil {
100+
return err
101+
}
102+
103+
if cfg.ShowDiff && snapshots != nil {
104+
for path, original := range snapshots {
105+
newContent, _ := os.ReadFile(path) //nolint:gosec // path built from cfg.RootDir + known manifest filenames
106+
if diff := cmp.Diff(string(original), string(newContent)); diff != "" {
107+
log.Infof("Diff for %s:\n%s", path, diff)
108+
}
109+
}
110+
}
111+
112+
return nil
85113
}
86114

87115
// Validate checks if the updates were applied successfully.

pkg/languages/rust/rust.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/chainguard-dev/clog"
1818
"github.com/chainguard-dev/omnibump/pkg/languages"
19+
"github.com/google/go-cmp/cmp"
1920
)
2021

2122
var (
@@ -106,12 +107,24 @@ func (r *Rust) Update(ctx context.Context, cfg *languages.UpdateConfig) error {
106107
return nil
107108
}
108109

110+
var originalContent []byte
111+
if cfg.ShowDiff {
112+
originalContent, _ = os.ReadFile(cargoLockPath) //nolint:gosec // cargoLockPath built from cfg.RootDir + constant filename
113+
}
114+
109115
// Perform the update
110116
err = DoUpdate(ctx, packages, cargoPackages, updateCfg)
111117
if err != nil {
112118
return fmt.Errorf("failed to update Cargo packages: %w", err)
113119
}
114120

121+
if cfg.ShowDiff && originalContent != nil {
122+
newContent, _ := os.ReadFile(cargoLockPath) //nolint:gosec // cargoLockPath built from cfg.RootDir + constant filename
123+
if diff := cmp.Diff(string(originalContent), string(newContent)); diff != "" {
124+
log.Infof("Diff for %s:\n%s", cargoLockPath, diff)
125+
}
126+
}
127+
115128
log.Infof("Successfully updated Cargo packages")
116129
return nil
117130
}

0 commit comments

Comments
 (0)