|
| 1 | +package golinters |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + |
| 7 | + "github.com/yeya24/promlinter" |
| 8 | + "golang.org/x/tools/go/analysis" |
| 9 | + |
| 10 | + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" |
| 11 | + "github.com/golangci/golangci-lint/pkg/lint/linter" |
| 12 | + "github.com/golangci/golangci-lint/pkg/result" |
| 13 | +) |
| 14 | + |
| 15 | +func NewPromlinter() *goanalysis.Linter { |
| 16 | + var mu sync.Mutex |
| 17 | + var resIssues []goanalysis.Issue |
| 18 | + |
| 19 | + const linterName = "promlinter" |
| 20 | + analyzer := &analysis.Analyzer{ |
| 21 | + Name: linterName, |
| 22 | + Doc: goanalysis.TheOnlyanalyzerDoc, |
| 23 | + } |
| 24 | + return goanalysis.NewLinter( |
| 25 | + linterName, |
| 26 | + "Check Prometheus metrics naming via promlint", |
| 27 | + []*analysis.Analyzer{analyzer}, |
| 28 | + nil, |
| 29 | + ).WithContextSetter(func(lintCtx *linter.Context) { |
| 30 | + strict := lintCtx.Cfg.LintersSettings.Promlinter.Strict |
| 31 | + disabledLinters := lintCtx.Cfg.LintersSettings.Promlinter.DisabledLinters |
| 32 | + |
| 33 | + analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { |
| 34 | + issues := promlinter.RunLint(pass.Fset, pass.Files, promlinter.Setting{ |
| 35 | + Strict: strict, |
| 36 | + DisabledLintFuncs: disabledLinters, |
| 37 | + }) |
| 38 | + |
| 39 | + if len(issues) == 0 { |
| 40 | + return nil, nil |
| 41 | + } |
| 42 | + |
| 43 | + res := make([]goanalysis.Issue, len(issues)) |
| 44 | + for k, i := range issues { |
| 45 | + issue := result.Issue{ |
| 46 | + Pos: i.Pos, |
| 47 | + Text: fmt.Sprintf("Metric: %s Error: %s", i.Metric, i.Text), |
| 48 | + FromLinter: linterName, |
| 49 | + } |
| 50 | + |
| 51 | + res[k] = goanalysis.NewIssue(&issue, pass) |
| 52 | + } |
| 53 | + |
| 54 | + mu.Lock() |
| 55 | + resIssues = append(resIssues, res...) |
| 56 | + mu.Unlock() |
| 57 | + |
| 58 | + return nil, nil |
| 59 | + } |
| 60 | + }).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
| 61 | + return resIssues |
| 62 | + }).WithLoadMode(goanalysis.LoadModeSyntax) |
| 63 | +} |
0 commit comments