Skip to content

Commit 086b8e1

Browse files
authored
Merge pull request #28 from beatlabs/check-refactoring
module version check refactored for the path and error
2 parents e870284 + e1f1dae commit 086b8e1

File tree

5 files changed

+86
-20
lines changed

5 files changed

+86
-20
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ Result:
164164
Apache-2.0
165165
```
166166

167+
## How to configure for private modules
168+
169+
Since check and update rely on go toolchain, if you have any private module that isn't publicly accessible, don't forget to set up your environment variables. For more information and how to configure, please check [Module configuration for non-public modules](https://golang.org/cmd/go/#hdr-Module_configuration_for_non_public_modules).
170+
167171
## Code of conduct
168172

169173
Please note that this project is released with a [Contributor Code of Conduct](https://github.com/beatlabs/gomodctl/blob/master/CODE_OF_CONDUCT.md). By participating in this project and its community you agree to abide by those terms.

cmd/gomodctl/gomodctl.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"os/signal"
8+
"syscall"
89

910
"github.com/beatlabs/gomodctl/internal/cmd/check"
1011
"github.com/beatlabs/gomodctl/internal/cmd/info"
@@ -44,18 +45,13 @@ type RootOptions struct {
4445
func Execute() {
4546
ctx, cancel := context.WithCancel(context.Background())
4647

47-
c := make(chan os.Signal, 1)
48+
signals := make(chan os.Signal, 1)
4849

49-
signal.Notify(c, os.Interrupt)
50-
51-
defer func() {
52-
signal.Stop(c)
53-
cancel()
54-
}()
50+
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
5551

5652
go func() {
5753
select {
58-
case <-c:
54+
case <-signals:
5955
cancel()
6056
case <-ctx.Done():
6157
}

internal/cmd/check/check.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,18 @@ func (o *Options) Execute(checker Checker) {
5656
var data [][]string
5757

5858
for name, result := range checkResults {
59-
data = append(data, []string{
59+
r := []string{
6060
name,
6161
result.LocalVersion.Original(),
62-
result.LatestVersion.Original(),
63-
})
62+
}
63+
64+
if result.Error != nil {
65+
r = append(r, fmt.Sprintf("failed because of: %s", result.Error.Error()))
66+
} else {
67+
r = append(r, result.LatestVersion.Original())
68+
}
69+
70+
data = append(data, r)
6471
}
6572

6673
table := tablewriter.NewWriter(os.Stdout)

internal/module/check_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package module
22

33
import (
44
"context"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
58
"testing"
69

710
"github.com/stretchr/testify/suite"
@@ -41,3 +44,60 @@ func (s *CheckTestSuite) Test_SelfCheck_CancelContextBefore() {
4144
s.EqualError(err, "context canceled")
4245
s.Empty(result)
4346
}
47+
func (s *CheckTestSuite) Test_CustomModFile() {
48+
gomodContent := []byte(`module test-project
49+
50+
go 1.13
51+
52+
require (
53+
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
54+
github.com/cespare/xxhash/v2 v2.1.1 // indirect
55+
github.com/fsnotify/fsnotify v1.4.7
56+
github.com/gin-gonic/gin v1.5.0 // indirect
57+
github.com/go-openapi/jsonreference v0.19.3 // indirect
58+
github.com/go-openapi/spec v0.19.4 // indirect
59+
github.com/go-playground/universal-translator v0.17.0 // indirect
60+
github.com/json-iterator/go v1.1.8 // indirect
61+
github.com/klauspost/compress v1.9.3 // indirect
62+
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
63+
github.com/labstack/echo/v4 v4.1.11
64+
github.com/leodido/go-urn v1.2.0 // indirect
65+
github.com/mailru/easyjson v0.7.0 // indirect
66+
github.com/mattn/go-colorable v0.1.4 // indirect
67+
github.com/mattn/go-isatty v0.0.10 // indirect
68+
github.com/newrelic/go-agent v2.16.0+incompatible // indirect
69+
github.com/prometheus/client_golang v1.2.1 // indirect
70+
github.com/prometheus/procfs v0.0.8 // indirect
71+
github.com/sirupsen/logrus v1.4.2
72+
github.com/spf13/viper v1.4.0
73+
github.com/stretchr/testify v1.4.0
74+
github.com/swaggo/echo-swagger v0.0.0-20190329130007-1219b460a043
75+
github.com/swaggo/swag v1.6.3 // indirect
76+
github.com/valyala/fasthttp v1.6.0 // indirect
77+
github.com/valyala/fasttemplate v1.1.0 // indirect
78+
golang.org/x/crypto v0.0.0-20191128160524-b544559bb6d1 // indirect
79+
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933
80+
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 // indirect
81+
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d // indirect
82+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
83+
gopkg.in/go-playground/validator.v9 v9.30.2 // indirect
84+
gopkg.in/yaml.v2 v2.2.7 // indirect
85+
)`)
86+
87+
tempDir, err := ioutil.TempDir("", "test")
88+
s.NoError(err)
89+
90+
tempFile := filepath.Join(tempDir, "go.mod")
91+
92+
err = ioutil.WriteFile(tempFile, gomodContent, 0666)
93+
s.NoError(err)
94+
95+
checker := Checker{Ctx: s.ctx}
96+
97+
result, err := checker.Check(tempDir)
98+
s.NoError(err)
99+
s.NotEmpty(result)
100+
101+
s.NoError(os.Remove(tempFile))
102+
s.NoError(os.RemoveAll(tempDir))
103+
}

internal/module/parser.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package module
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"os/exec"
8+
"path/filepath"
79
"regexp"
8-
"strings"
910

1011
"github.com/Masterminds/semver"
1112
"github.com/spf13/viper"
@@ -38,19 +39,17 @@ type packageResult struct {
3839
func (v *versionParser) Parse(path string) ([]packageResult, error) {
3940
cmd := exec.CommandContext(v.ctx, "go", "list", "-m", "-versions", "-json", "all")
4041

41-
home := viper.GetString("home")
4242
if path != "" {
43-
if home != "" && strings.Contains(path, "~") {
44-
path = home + strings.Replace(path, "~", "", 1) + "/"
45-
} else {
46-
path = path + "/"
47-
}
48-
49-
cmd.Dir = path
43+
home := viper.GetString("home")
44+
cmd.Dir = filepath.Join(home, path)
5045
}
5146

5247
out, err := cmd.CombinedOutput()
5348
if err != nil {
49+
if len(out) > 0 {
50+
return nil, fmt.Errorf("with output [%s] %w", out, err)
51+
}
52+
5453
return nil, err
5554
}
5655

0 commit comments

Comments
 (0)