Skip to content

Commit

Permalink
added version contraint liniting
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancurrah committed May 13, 2020
1 parent 60984bd commit 335e7d8
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 344 deletions.
13 changes: 11 additions & 2 deletions .gomodguard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ allowed:
modules: # List of allowed modules
- gopkg.in/yaml.v2
- github.com/go-xmlfmt/xmlfmt
- github.com/phayes/checkstyle
- github.com/Masterminds/semver
domains: # List of allowed module domains
- golang.org

Expand All @@ -15,4 +15,13 @@ blocked:
- github.com/mitchellh/go-homedir:
recommendations:
- github.com/ryancurrah/gomodguard
reason: "testing if the linted module is not blocked when it is recommended"
reason: "testing if the current/linted module is not blocked when it is recommended"
- github.com/phayes/checkstyle:
recommendations:
- github.com/someother/module
reason: "testing if module is blocked with recommendation"

versions:
- github.com/mitchellh/go-homedir:
version: "<= 1.1.0"
reason: "testing if blocked version constraint works."
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Alternative modules can be optionally recommended in the blocked modules list.

If the linted module imports a blocked module but the linted module is in the recommended modules list the blocked module is ignored. Usually, this means the linted module wraps that blocked module for use by other modules, therefore the import of the blocked module should not be blocked.

Version constraints can be specified for modules as well which lets you block new or old versions of modules or specific versions.

Results are printed to `stdout`.

Logging statements are printed to `stderr`.
Expand All @@ -44,6 +46,10 @@ blocked:
recommendations: # Recommended modules that should be used instead (Optional)
- golang.org/x/mod
reason: "`mod` is the official go.mod parser library." # Reason why the recommended module should be used (Optional)
versions: # List of blocked module version constraints.
- github.com/mitchellh/go-homedir: # Blocked module with version constraint.
version: "<= 1.1.0" # Version constraint, see https://github.com/Masterminds/semver#basic-comparisons.
reason: "testing if blocked version constraint works." # Reason why the version constraint exists.
```

## Usage
Expand All @@ -54,17 +60,22 @@ Usage: gomodguard <file> [files...]
Also supports package syntax but will use it in relative path, i.e. ./pkg/...
Flags:
-f string
Report results to the specified file. A report type must also be specified
Report results to the specified file. A report type must also be specified
-file string
-h Show this help text
-h Show this help text
-help
-n Don't lint test files
-i int
Exit code when issues were found (default 2)
-issues-exit-code int
(default 2)
-n Don't lint test files
-no-test
-r string
Report results to one of the following formats: checkstyle. A report file destination must also be specified
Report results to one of the following formats: checkstyle. A report file destination must also be specified
-report string
```

Expand Down
28 changes: 16 additions & 12 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ const (
var (
configFile = ".gomodguard.yaml"
logger = log.New(os.Stderr, "", 0)
lintErrorRC = 2
errFindingConfigFile = fmt.Errorf("could not find config file")
)

// Run the gomodguard linter.
func Run() {
// Run the gomodguard linter. Returns the exit code to use.
func Run() int {
var (
args []string
help bool
noTest bool
report string
reportFile string
cwd, _ = os.Getwd()
args []string
help bool
noTest bool
report string
reportFile string
issuesExitCode int
cwd, _ = os.Getwd()
)

flag.BoolVar(&help, "h", false, "Show this help text")
Expand All @@ -47,13 +47,15 @@ func Run() {
flag.StringVar(&report, "report", "", "")
flag.StringVar(&reportFile, "f", "", "Report results to the specified file. A report type must also be specified")
flag.StringVar(&reportFile, "file", "", "")
flag.IntVar(&issuesExitCode, "i", 2, "Exit code when issues were found")
flag.IntVar(&issuesExitCode, "issues-exit-code", 2, "")
flag.Parse()

report = strings.TrimSpace(strings.ToLower(report))

if help {
showHelp()
return
return 0
}

if report != "" && report != "checkstyle" {
Expand Down Expand Up @@ -99,8 +101,10 @@ func Run() {
}

if len(results) > 0 {
os.Exit(lintErrorRC)
return issuesExitCode
}

return 0
}

// GetConfig from YAML file.
Expand Down Expand Up @@ -180,7 +184,7 @@ func GetFilteredFiles(cwd string, skipTests bool, args []string) []string {

// showHelp text for command line.
func showHelp() {
helpText := `Usage: gomodguard <file> [foundFiles...]
helpText := `Usage: gomodguard <file> [files...]
Also supports package syntax but will use it in relative path, i.e. ./pkg/...
Flags:`
fmt.Println(helpText)
Expand Down
4 changes: 3 additions & 1 deletion cmd/gomodguard/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"os"

"github.com/ryancurrah/gomodguard"
)

func main() {
gomodguard.Run()
os.Exit(gomodguard.Run())
}
7 changes: 6 additions & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ import (
)

func TestCmdRun(t *testing.T) {
gomodguard.Run()
wantExitCode := 2
exitCode := gomodguard.Run()

if exitCode != wantExitCode {
t.Errorf("got exit code '%d' want '%d'", exitCode, wantExitCode)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ryancurrah/gomodguard
go 1.14

require (
github.com/Masterminds/semver v1.5.0
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
github.com/mitchellh/go-homedir v1.1.0
github.com/phayes/checkstyle v0.0.0-20170904204023-bfd46e6a821d
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b h1:khEcpUM4yFcxg4/FHQWkvVRmgijNXRfzkIDHh23ggEo=
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b/go.mod h1:aUCEOzzezBEjDBbFBoSiya/gduyIiWYRP6CnSFIV8AM=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
Expand Down

0 comments on commit 335e7d8

Please sign in to comment.