From a2a3d5cebe1fbe0e4f6691e571711f38f29af68d Mon Sep 17 00:00:00 2001 From: hectorj2f Date: Tue, 16 Jan 2024 13:30:06 +0100 Subject: [PATCH] show diffs between the original and new go.mod file Signed-off-by: hectorj2f --- cmd/gobump/root.go | 4 +++- go.mod | 1 + go.sum | 2 ++ pkg/types/types.go | 7 +++++++ pkg/update/update.go | 42 ++++++++++++++++++++++----------------- pkg/update/update_test.go | 10 +++++----- 6 files changed, 42 insertions(+), 24 deletions(-) diff --git a/cmd/gobump/root.go b/cmd/gobump/root.go index 0005b29..d841670 100644 --- a/cmd/gobump/root.go +++ b/cmd/gobump/root.go @@ -18,6 +18,7 @@ type rootCLIFlags struct { replaces string goVersion string tidy bool + showDiff bool } var rootFlags rootCLIFlags @@ -73,7 +74,7 @@ var rootCmd = &cobra.Command{ } } - if _, err := update.DoUpdate(pkgVersions, rootFlags.modroot, rootFlags.tidy, rootFlags.goVersion); err != nil { + if _, err := update.DoUpdate(pkgVersions, &types.Config{Modroot: rootFlags.modroot, Tidy: rootFlags.tidy, GoVersion: rootFlags.goVersion, ShowDiff: rootFlags.showDiff}); err != nil { fmt.Println("failed running update: ", err) os.Exit(1) } @@ -94,5 +95,6 @@ func init() { flagSet.StringVar(&rootFlags.modroot, "modroot", "", "path to the go.mod root") flagSet.StringVar(&rootFlags.replaces, "replaces", "", "A space-separated list of packages to replace") flagSet.BoolVar(&rootFlags.tidy, "tidy", false, "Run 'go mod tidy' command") + flagSet.BoolVar(&rootFlags.showDiff, "show-diff", false, "Show the difference between the original and 'go.mod' files") flagSet.StringVar(&rootFlags.goVersion, "go-version", "", "set the go-version for go-mod-tidy") } diff --git a/go.mod b/go.mod index b9ea252..ca37e22 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/chainguard-dev/gobump go 1.21 require ( + github.com/google/go-cmp v0.6.0 github.com/spf13/cobra v1.8.0 golang.org/x/mod v0.14.0 k8s.io/apimachinery v0.29.0 diff --git a/go.sum b/go.sum index d6a3d20..a38f3e8 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/pkg/types/types.go b/pkg/types/types.go index d3d2e9d..924041d 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -7,3 +7,10 @@ type Package struct { Replace bool Require bool } + +type Config struct { + Modroot string + GoVersion string + ShowDiff bool + Tidy bool +} diff --git a/pkg/update/update.go b/pkg/update/update.go index fcbb16d..08b3167 100644 --- a/pkg/update/update.go +++ b/pkg/update/update.go @@ -6,24 +6,24 @@ import ( "os" "path" - "golang.org/x/mod/modfile" - "golang.org/x/mod/semver" - "github.com/chainguard-dev/gobump/pkg/run" "github.com/chainguard-dev/gobump/pkg/types" + "github.com/google/go-cmp/cmp" + "golang.org/x/mod/modfile" + "golang.org/x/mod/semver" ) -func ParseGoModfile(path string) (*modfile.File, error) { +func ParseGoModfile(path string) (*modfile.File, []byte, error) { content, err := os.ReadFile(path) if err != nil { - return nil, err + return nil, content, err } mod, err := modfile.Parse("go.mod", content, nil) if err != nil { - return nil, err + return nil, content, err } - return mod, nil + return mod, content, nil } func checkPackageValues(pkgVersions map[string]*types.Package, modFile *modfile.File) error { @@ -65,18 +65,18 @@ func checkPackageValues(pkgVersions map[string]*types.Package, modFile *modfile. return nil } -func DoUpdate(pkgVersions map[string]*types.Package, modroot string, tidy bool, goVersion string) (*modfile.File, error) { +func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfile.File, error) { // Run go mod tidy before - if tidy { - output, err := run.GoModTidy(modroot, goVersion) + if cfg.Tidy { + output, err := run.GoModTidy(cfg.Modroot, cfg.GoVersion) if err != nil { return nil, fmt.Errorf("failed to run 'go mod tidy': %v with output: %v", err, output) } } // Read the entire go.mod one more time into memory and check that all the version constraints are met. - modpath := path.Join(modroot, "go.mod") - modFile, err := ParseGoModfile(modpath) + modpath := path.Join(cfg.Modroot, "go.mod") + modFile, content, err := ParseGoModfile(modpath) if err != nil { return nil, fmt.Errorf("unable to parse the go mod file with error: %v", err) } @@ -92,7 +92,7 @@ func DoUpdate(pkgVersions map[string]*types.Package, modroot string, tidy bool, if pkg.Replace { log.Printf("Update package: %s\n", k) log.Println("Running go mod edit replace ...") - if output, err := run.GoModEditReplaceModule(pkg.OldName, pkg.Name, pkg.Version, modroot); err != nil { + if output, err := run.GoModEditReplaceModule(pkg.OldName, pkg.Name, pkg.Version, cfg.Modroot); err != nil { return nil, fmt.Errorf("failed to run 'go mod edit -replace': %v with output: %v", err, output) } } @@ -104,27 +104,27 @@ func DoUpdate(pkgVersions map[string]*types.Package, modroot string, tidy bool, log.Printf("Update package: %s\n", k) if pkg.Require { log.Println("Running go mod edit -droprequire ...") - if output, err := run.GoModEditDropRequireModule(pkg.Name, modroot); err != nil { + if output, err := run.GoModEditDropRequireModule(pkg.Name, cfg.Modroot); err != nil { return nil, fmt.Errorf("failed to run 'go mod edit -droprequire': %v with output: %v", err, output) } } log.Println("Running go get ...") - if output, err := run.GoGetModule(pkg.Name, pkg.Version, modroot); err != nil { + if output, err := run.GoGetModule(pkg.Name, pkg.Version, cfg.Modroot); err != nil { return nil, fmt.Errorf("failed to run 'go get': %v with output: %v", err, output) } } } // Run go mod tidy - if tidy { - output, err := run.GoModTidy(modroot, goVersion) + if cfg.Tidy { + output, err := run.GoModTidy(cfg.Modroot, cfg.GoVersion) if err != nil { return nil, fmt.Errorf("failed to run 'go mod tidy': %v with output: %v", err, output) } } // Read the entire go.mod one more time into memory and check that all the version constraints are met. - newModFile, err := ParseGoModfile(modpath) + newModFile, newContent, err := ParseGoModfile(modpath) if err != nil { return nil, fmt.Errorf("unable to parse the go mod file with error: %v", err) } @@ -135,6 +135,12 @@ func DoUpdate(pkgVersions map[string]*types.Package, modroot string, tidy bool, } } + if cfg.ShowDiff { + if diff := cmp.Diff(string(content), string(newContent)); diff != "" { + fmt.Println(diff) + } + } + return newModFile, nil } diff --git a/pkg/update/update_test.go b/pkg/update/update_test.go index 050549b..e8614ab 100644 --- a/pkg/update/update_test.go +++ b/pkg/update/update_test.go @@ -45,7 +45,7 @@ func TestUpdate(t *testing.T) { tmpdir := t.TempDir() copyFile(t, "testdata/aws-efs-csi-driver/go.mod", tmpdir) - modFile, err := DoUpdate(tc.pkgVersions, tmpdir, false, "") + modFile, err := DoUpdate(tc.pkgVersions, &types.Config{Modroot: tmpdir, Tidy: false, GoVersion: ""}) if err != nil { t.Fatal(err) } @@ -85,7 +85,7 @@ func TestGoModTidy(t *testing.T) { copyFile(t, "testdata/hello/go.sum", tmpdir) copyFile(t, "testdata/hello/main.go", tmpdir) - modFile, err := DoUpdate(tc.pkgVersions, tmpdir, true, "") + modFile, err := DoUpdate(tc.pkgVersions, &types.Config{Modroot: tmpdir, Tidy: false, GoVersion: ""}) if err != nil { t.Fatal(err) } @@ -119,7 +119,7 @@ func TestUpdateError(t *testing.T) { tmpdir := t.TempDir() copyFile(t, "testdata/aws-efs-csi-driver/go.mod", tmpdir) - _, err := DoUpdate(tc.pkgVersions, tmpdir, false, "") + _, err := DoUpdate(tc.pkgVersions, &types.Config{Modroot: tmpdir, Tidy: false, GoVersion: ""}) if err == nil { t.Fatal("expected error, got nil") } @@ -139,7 +139,7 @@ func TestReplaces(t *testing.T) { Replace: true, }} - modFile, err := DoUpdate(replaces, tmpdir, false, "") + modFile, err := DoUpdate(replaces, &types.Config{Modroot: tmpdir, Tidy: false, GoVersion: ""}) if err != nil { t.Fatal(err) } @@ -203,7 +203,7 @@ func TestCommit(t *testing.T) { Version: tc.version, }, } - modFile, err := DoUpdate(pkgVersions, tmpdir, false, "1.21") + modFile, err := DoUpdate(pkgVersions, &types.Config{Modroot: tmpdir, Tidy: false, GoVersion: "1.21"}) if err != nil { t.Fatal(err) }