Skip to content

Commit

Permalink
-mod=mod parameter added to version check
Browse files Browse the repository at this point in the history
Signed-off-by: Serdar Ormanlı <s.ormanli@thebeat.co>
  • Loading branch information
Serdar Ormanlı committed Feb 12, 2021
1 parent acb8872 commit bd6b8bd
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 107 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@master

- name: Set up Go (1.13)
- name: Set up Go (1.15)
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.15

- name: Linter
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
- name: Unshallow
run: git fetch --prune --unshallow

- name: Set up Go (1.13)
- name: Set up Go (1.15)
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.15

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@master
Expand Down
3 changes: 3 additions & 0 deletions cmd/gomodctl/gomodctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/signal"
"runtime/debug"
"syscall"

"github.com/beatlabs/gomodctl/internal/cmd/check"
Expand Down Expand Up @@ -47,6 +48,8 @@ type RootOptions struct {

// Execute is exported.
func Execute() {
debug.SetGCPercent(-1)

ctx, cancel := context.WithCancel(context.Background())

signals := make(chan os.Signal, 1)
Expand Down
23 changes: 8 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
module github.com/beatlabs/gomodctl

go 1.13
go 1.15

require (
github.com/Masterminds/semver v1.5.0
github.com/frankban/quicktest v1.7.2 // indirect
github.com/go-resty/resty/v2 v2.3.0
github.com/google/go-cmp v0.4.0 // indirect
github.com/google/licenseclassifier v0.0.0-20200708223521-3d09a0ea2f39
github.com/klauspost/compress v1.9.8 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/mholt/archiver/v3 v3.3.0
github.com/go-resty/resty/v2 v2.5.0
github.com/google/licenseclassifier v0.0.0-20201113175434-78a70215ca36
github.com/mholt/archiver/v3 v3.5.0
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.4
github.com/pierrec/lz4 v2.4.1+incompatible // indirect
github.com/securego/gosec v0.0.0-20200129084146-17df5b370244
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/securego/gosec v0.0.0-20200401082031-e946c8c39989
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
golang.org/x/mod v0.3.0
gopkg.in/yaml.v2 v2.2.8 // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/mod v0.4.1
)
122 changes: 49 additions & 73 deletions go.sum

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion internal/module/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

var regex = regexp.MustCompile(`({([^}]*)})`)

var go115 = semver.MustParse("1.15.0")

type item struct {
Path string `json:"Path"`
Version string `json:"Version"`
Expand Down Expand Up @@ -45,7 +47,17 @@ type PackageResult struct {

// Parse is exported
func (v *ModParser) Parse(path string) ([]PackageResult, error) {
cmd := exec.CommandContext(v.ctx, "go", "list", "-m", "-versions", "-json", "all")
goVersion, err := v.goRuntimeVersion()
if err != nil {
return nil, err
}

args := []string{"list", "-m", "-versions", "-json", "-mod=mod", "all"}
if goVersion.LessThan(go115) {
args = []string{"list", "-m", "-versions", "-json", "all"}
}

cmd := exec.CommandContext(v.ctx, "go", args...)

if path != "" {
home := viper.GetString("home")
Expand Down Expand Up @@ -98,3 +110,22 @@ func (v *ModParser) Parse(path string) ([]PackageResult, error) {

return result, nil
}

func (v *ModParser) goRuntimeVersion() (*semver.Version, error) {
cmd := exec.CommandContext(v.ctx, "go", "version")

out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}

r := regexp.MustCompile(`(go version go)(.*)( .+)`)
find := r.FindSubmatch(out)

version, err := semver.NewVersion(string(find[2]))
if err != nil {
return nil, err
}

return version, nil
}
14 changes: 0 additions & 14 deletions internal/module/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"path/filepath"

"github.com/Masterminds/semver"
"github.com/beatlabs/gomodctl/internal"
"golang.org/x/mod/modfile"
)
Expand Down Expand Up @@ -87,16 +86,3 @@ func (u *Updater) Update(path string) (map[string]internal.CheckResult, error) {

return latestMinors, nil
}

func getLatestMinorVersion(current *semver.Version, versions []*semver.Version) (*semver.Version, error) {
n := 0
for _, version := range versions {
if version.Major() == current.Major() {
versions[n] = version
n++
}
}
versions = versions[:n]

return getLatestVersion(nil, versions)
}

0 comments on commit bd6b8bd

Please sign in to comment.