Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ linters-settings:
goconst:
min-len: 3
min-occurrences: 3
depguard:
list-type: blacklist
include-go-root: false
packages:
- github.com/davecgh/go-spew/spew

linters:
enable:
Expand Down
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@
[[override]]
branch = "master"
name = "github.com/golangci/lint"

[[constraint]]
branch = "master"
name = "github.com/OpenPeeDeeP/depguard"
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ gofmt: Gofmt checks whether code was gofmt-ed. By default this tool runs with -s
goimports: Goimports does everything that gofmt does. Additionally it checks unused imports
maligned: Tool to detect Go structs that would take less memory if their fields were sorted
megacheck: 3 sub-linters in one: unused, gosimple and staticcheck
depguard: Go linter that checks if package imports are in a list of acceptable packages
```

Pass `-E/--enable` to enable linter and `-D/--disable` to disable:
Expand Down Expand Up @@ -196,6 +197,7 @@ golangci-lint linters
- [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports): Goimports does everything that gofmt does. Additionally it checks unused imports
- [maligned](https://github.com/mdempsky/maligned): Tool to detect Go structs that would take less memory if their fields were sorted
- [megacheck](https://github.com/dominikh/go-tools/tree/master/cmd/megacheck): 3 sub-linters in one: unused, gosimple and staticcheck
- [depguard](https://github.com/OpenPeeDeeP/depguard): Go linter that checks if package imports are in a list of acceptable packages
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add it to thanks section please


# Configuration
## Command-Line Options
Expand Down Expand Up @@ -231,7 +233,7 @@ Linters presets:
bugs: govet, errcheck, staticcheck, gas, megacheck
unused: unused, structcheck, varcheck, ineffassign, deadcode, megacheck
format: gofmt, goimports
style: golint, gosimple, interfacer, unconvert, dupl, goconst, megacheck
style: golint, gosimple, interfacer, unconvert, dupl, goconst, megacheck, depguard
complexity: gocyclo
performance: maligned
```
Expand Down Expand Up @@ -427,6 +429,7 @@ Thanks to developers and authors of used linters:
- [golang/x/tools/goimports](https://godoc.org/golang.org/x/tools/cmd/goimports)
- [mdempsky/maligned](https://github.com/mdempsky/maligned)
- [dominikh/go-tools/megacheck](https://github.com/dominikh/go-tools/tree/master/cmd/megacheck)
- [OpenPeeDeeP/depguard](https://github.com/OpenPeeDeeP/depguard)

# Future Plans
1. Upstream all changes of forked linters.
Expand Down
12 changes: 12 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ func (e *Executor) initRun() {
runCmd.Flags().IntVar(&lsc.Goconst.MinOccurrencesCount, "goconst.min-occurrences",
3, "Goconst: minimum occurrences of constant string count to trigger issue")

// (@dixonwille) These flag is only used for testing purposes.
runCmd.Flags().StringSliceVar(&lsc.Depguard.Packages, "depguard.packages", nil,
"Depguard: packages to add to the list")
if err := runCmd.Flags().MarkHidden("depguard.packages"); err != nil {
panic(err) //Considering The only time this is called is if name does not exist
}
runCmd.Flags().BoolVar(&lsc.Depguard.IncludeGoRoot, "depguard.include-go-root", false,
"Depguard: check list against standard lib")
if err := runCmd.Flags().MarkHidden("depguard.include-go-root"); err != nil {
panic(err) //Considering The only time this is called is if name does not exist
}

// Linters config
lc := &e.cfg.Linters
runCmd.Flags().StringSliceVarP(&lc.Enable, "enable", "E", []string{}, "Enable specific linter")
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ type LintersSettings struct {
MinStringLen int `mapstructure:"min-len"`
MinOccurrencesCount int `mapstructure:"min-occurrences"`
}
Depguard struct {
ListType string `mapstructure:"list-type"`
Packages []string
IncludeGoRoot bool `mapstructure:"inlude-go-root"`
}
}

type Linters struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/enabled_linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func GetAllSupportedLinterConfigs() []LinterConfig {
newLinterConfig(golinters.Maligned{}).WithFullImport().WithPresets(PresetPerformance).WithSpeed(10),
newLinterConfig(golinters.Megacheck{GosimpleEnabled: true, UnusedEnabled: true, StaticcheckEnabled: true}).
WithSSA().WithPresets(PresetStyle, PresetBugs, PresetUnused).WithSpeed(1),
newLinterConfig(golinters.Depguard{}).WithFullImport().WithPresets(PresetStyle).WithSpeed(6),
}

if os.Getenv("GOLANGCI_COM_RUN") == "1" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/enabled_linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func testOneSource(t *testing.T, sourcePath string) {
"--out-format=line-number",
"--print-welcome=false",
"--govet.check-shadowing=true",
"--depguard.include-go-root",
"--depguard.packages='log'",
sourcePath)
runGoErrchk(cmd, t)
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/golinters/depguard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package golinters
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test file to pkg/testdata please

Copy link
Author

@dixonwille dixonwille Jun 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't run test locally... https://github.com/golangci/golangci-lint/blob/master/pkg/enabled_linters_test.go#L93 GOROOT does not have a test directory for me. Am I missing something?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it. I use Fedora and the golang package that I installed strips out those files...


import (
"context"
"fmt"
"strings"

depguardAPI "github.com/OpenPeeDeeP/depguard"
"github.com/golangci/golangci-lint/pkg/result"
)

type Depguard struct{}

func (Depguard) Name() string {
return "depguard"
}

func (Depguard) Desc() string {
return "Go linter that checks if package imports are in a list of acceptable packages"
}

func (d Depguard) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
dg := &depguardAPI.Depguard{
Packages: lintCtx.Settings().Depguard.Packages,
IncludeGoRoot: lintCtx.Settings().Depguard.IncludeGoRoot,
}
listType := lintCtx.Settings().Depguard.ListType
var found bool
dg.ListType, found = depguardAPI.StringToListType[strings.ToLower(listType)]
if !found {
if listType != "" {
return nil, fmt.Errorf("unsure what list type %s is", listType)
}
dg.ListType = depguardAPI.LTBlacklist
}

issues, err := dg.Run(lintCtx.LoaderConfig, lintCtx.Program)
if err != nil {
return nil, err
}
if len(issues) == 0 {
return nil, nil
}
msgSuffix := "is in the blacklist"
if dg.ListType == depguardAPI.LTWhitelist {
msgSuffix = "is not in the whitelist"
}
res := make([]result.Issue, 0, len(issues))
for _, i := range issues {
res = append(res, result.Issue{
Pos: i.Position,
Text: fmt.Sprintf("%s %s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix),
FromLinter: d.Name(),
})
}
return res, nil
}
9 changes: 9 additions & 0 deletions pkg/testdata/with_issues/depguard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testdata

import (
"log" // ERROR "`log` is in the blacklist"
)

func SpewDebugInfo() {
log.Println("Debug info")
}
2 changes: 1 addition & 1 deletion pkg/testdata/with_issues/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package testdata

import (
"crypto/md5" // ERROR "G501: Blacklisted import crypto/md5: weak cryptographic primitive"
"log"
"log" // nolint:depguard
)

func Gas() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/testdata/with_issues/unconvert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package testdata

import "log"
import "log" // nolint:depguard

func Unconvert() {
a := 1
Expand Down
12 changes: 12 additions & 0 deletions vendor/github.com/OpenPeeDeeP/depguard/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions vendor/github.com/OpenPeeDeeP/depguard/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions vendor/github.com/OpenPeeDeeP/depguard/Gopkg.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading