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

Add support for tidy compat flag #21

Merged
merged 1 commit into from
Jan 28, 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
16 changes: 9 additions & 7 deletions cmd/gobump/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
)

type rootCLIFlags struct {
packages string
modroot string
replaces string
goVersion string
tidy bool
showDiff bool
packages string
modroot string
replaces string
goVersion string
tidy bool
showDiff bool
tidyCompat string
}

var rootFlags rootCLIFlags
Expand Down Expand Up @@ -68,7 +69,7 @@ var rootCmd = &cobra.Command{
}
}

if _, err := update.DoUpdate(pkgVersions, &types.Config{Modroot: rootFlags.modroot, Tidy: rootFlags.tidy, GoVersion: rootFlags.goVersion, ShowDiff: rootFlags.showDiff}); err != nil {
if _, err := update.DoUpdate(pkgVersions, &types.Config{Modroot: rootFlags.modroot, Tidy: rootFlags.tidy, GoVersion: rootFlags.goVersion, ShowDiff: rootFlags.showDiff, TidyCompat: rootFlags.tidyCompat}); err != nil {
return fmt.Errorf("Failed to running update. Error: %v", err)
}
return nil
Expand All @@ -91,4 +92,5 @@ func init() {
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")
flagSet.StringVar(&rootFlags.tidyCompat, "compat", "", "set the go version for which the tidied go.mod and go.sum files should be compatible")
}
10 changes: 8 additions & 2 deletions pkg/run/gorun.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
versionutil "k8s.io/apimachinery/pkg/util/version"
)

func GoModTidy(modroot, goVersion string) (string, error) {
func GoModTidy(modroot, goVersion, compat string) (string, error) {
if goVersion == "" {
cmd := exec.Command("go", "env", "GOVERSION")
cmd.Stderr = os.Stderr
Expand All @@ -25,7 +25,13 @@ func GoModTidy(modroot, goVersion string) (string, error) {
}

log.Printf("Running go mod tidy with go version '%s' ...\n", goVersion)
cmd := exec.Command("go", "mod", "tidy", "-go", goVersion)
args := []string{"mod", "tidy", "-go", goVersion}
if compat != "" {
log.Printf("Running go mod tidy with compat '%s' ...\n", compat)
args = append(args, "-compat", compat)
}

cmd := exec.Command("go", args...)
cmd.Dir = modroot
if bytes, err := cmd.CombinedOutput(); err != nil {
return strings.TrimSpace(string(bytes)), err
Expand Down
9 changes: 5 additions & 4 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ type Package struct {
}

type Config struct {
Modroot string
GoVersion string
ShowDiff bool
Tidy bool
Modroot string
GoVersion string
ShowDiff bool
Tidy bool
TidyCompat string
}
4 changes: 2 additions & 2 deletions pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfil
}
// Run go mod tidy before
if cfg.Tidy {
output, err := run.GoModTidy(cfg.Modroot, goVersion)
output, err := run.GoModTidy(cfg.Modroot, goVersion, cfg.TidyCompat)
if err != nil {
return nil, fmt.Errorf("failed to run 'go mod tidy': %v with output: %v", err, output)
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfil

// Run go mod tidy
if cfg.Tidy {
output, err := run.GoModTidy(cfg.Modroot, goVersion)
output, err := run.GoModTidy(cfg.Modroot, goVersion, cfg.TidyCompat)
if err != nil {
return nil, fmt.Errorf("failed to run 'go mod tidy': %v with output: %v", err, output)
}
Expand Down
Loading