Skip to content

Commit

Permalink
show diffs between the original and new go.mod file
Browse files Browse the repository at this point in the history
Signed-off-by: hectorj2f <[email protected]>
  • Loading branch information
hectorj2f committed Jan 16, 2024
1 parent 7c0cecf commit e6000e9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 deletions.
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")
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/chainguard-dev/gobump
go 1.21

require (
github.com/google/go-cmp v0.6.0
github.com/sergi/go-diff v1.3.1
github.com/spf13/cobra v1.8.0
golang.org/x/mod v0.14.0
k8s.io/apimachinery v0.29.0
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/apimachinery v0.29.0 h1:+ACVktwyicPz0oc6MTMLwa2Pw3ouLAfAon1wPLtG48o=
Expand Down
Binary file added gobump
Binary file not shown.
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

0 comments on commit e6000e9

Please sign in to comment.