www.github.com/golangci/golangci-lint.git@v1.10.1/pkg/golinters/depguard.go (about) 1 package golinters 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 depguardAPI "github.com/OpenPeeDeeP/depguard" 9 "github.com/golangci/golangci-lint/pkg/lint/linter" 10 "github.com/golangci/golangci-lint/pkg/result" 11 ) 12 13 type Depguard struct{} 14 15 func (Depguard) Name() string { 16 return "depguard" 17 } 18 19 func (Depguard) Desc() string { 20 return "Go linter that checks if package imports are in a list of acceptable packages" 21 } 22 23 func (d Depguard) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) { 24 dg := &depguardAPI.Depguard{ 25 Packages: lintCtx.Settings().Depguard.Packages, 26 IncludeGoRoot: lintCtx.Settings().Depguard.IncludeGoRoot, 27 } 28 listType := lintCtx.Settings().Depguard.ListType 29 var found bool 30 dg.ListType, found = depguardAPI.StringToListType[strings.ToLower(listType)] 31 if !found { 32 if listType != "" { 33 return nil, fmt.Errorf("unsure what list type %s is", listType) 34 } 35 dg.ListType = depguardAPI.LTBlacklist 36 } 37 38 issues, err := dg.Run(lintCtx.LoaderConfig, lintCtx.Program) 39 if err != nil { 40 return nil, err 41 } 42 if len(issues) == 0 { 43 return nil, nil 44 } 45 msgSuffix := "is in the blacklist" 46 if dg.ListType == depguardAPI.LTWhitelist { 47 msgSuffix = "is not in the whitelist" 48 } 49 res := make([]result.Issue, 0, len(issues)) 50 for _, i := range issues { 51 res = append(res, result.Issue{ 52 Pos: i.Position, 53 Text: fmt.Sprintf("%s %s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix), 54 FromLinter: d.Name(), 55 }) 56 } 57 return res, nil 58 }