Skip to content

Commit

Permalink
go-version: use the version from the 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 1cac130 commit 7fc1fbb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 1 addition & 2 deletions pkg/run/gorun.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ func GoModTidy(modroot, goVersion string) (string, error) {

v := versionutil.MustParseGeneric(goVersion)
goVersion = fmt.Sprintf("%d.%d", v.Major(), v.Minor())

log.Printf("Running go mod tidy with go version '%s' ...\n", goVersion)
}

log.Printf("Running go mod tidy with go version '%s' ...\n", goVersion)
cmd := exec.Command("go", "mod", "tidy", "-go", goVersion)
cmd.Dir = modroot
if bytes, err := cmd.CombinedOutput(); err != nil {
Expand Down
16 changes: 13 additions & 3 deletions pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,26 @@ func checkPackageValues(pkgVersions map[string]*types.Package, modFile *modfile.
}

func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfile.File, error) {
modpath := path.Join(cfg.Modroot, "go.mod")

goVersion := cfg.GoVersion
if goVersion == "" {
// Read the go version from go.mod file and use that one
modFile, _, err := ParseGoModfile(modpath)
if err != nil {
return nil, fmt.Errorf("unable to parse the go mod file with error: %v", err)
}
goVersion = modFile.Go.Version
}
// Run go mod tidy before
if cfg.Tidy {
output, err := run.GoModTidy(cfg.Modroot, cfg.GoVersion)
output, err := run.GoModTidy(cfg.Modroot, 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(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 Down Expand Up @@ -117,7 +127,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, cfg.GoVersion)
output, err := run.GoModTidy(cfg.Modroot, goVersion)
if err != nil {
return nil, fmt.Errorf("failed to run 'go mod tidy': %v with output: %v", err, output)
}
Expand Down

0 comments on commit 7fc1fbb

Please sign in to comment.