Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM]: show diffs between the original and new go.mod file #13

Merged
merged 1 commit into from
Jan 16, 2024
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
4 changes: 3 additions & 1 deletion cmd/gobump/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type rootCLIFlags struct {
replaces string
goVersion string
tidy bool
showDiff bool
}

var rootFlags rootCLIFlags
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 7 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ type Package struct {
Replace bool
Require bool
}

type Config struct {
Modroot string
GoVersion string
ShowDiff bool
Tidy bool
}
42 changes: 24 additions & 18 deletions pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down