From 7fc1fbbfa67de4e04c4cfaf3294d55faa92211dc Mon Sep 17 00:00:00 2001 From: hectorj2f Date: Tue, 16 Jan 2024 20:15:52 +0100 Subject: [PATCH] go-version: use the version from the go.mod file Signed-off-by: hectorj2f --- pkg/run/gorun.go | 3 +-- pkg/update/update.go | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/run/gorun.go b/pkg/run/gorun.go index 7ba21b0..ea52eec 100644 --- a/pkg/run/gorun.go +++ b/pkg/run/gorun.go @@ -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 { diff --git a/pkg/update/update.go b/pkg/update/update.go index 08b3167..d85c342 100644 --- a/pkg/update/update.go +++ b/pkg/update/update.go @@ -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) @@ -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) }