-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplugin.go
70 lines (56 loc) · 2.17 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubeapilinter
import (
"fmt"
"github.com/golangci/plugin-module-register/register"
"golang.org/x/tools/go/analysis"
"k8s.io/apimachinery/pkg/util/validation/field"
kalanalysis "sigs.k8s.io/kube-api-linter/pkg/analysis"
"sigs.k8s.io/kube-api-linter/pkg/config"
"sigs.k8s.io/kube-api-linter/pkg/validation"
)
func init() {
register.Plugin("kubeapilinter", New)
}
// New creates a new golangci-lint plugin based on the KAL analyzers.
func New(settings any) (register.LinterPlugin, error) {
s, err := register.DecodeSettings[config.GolangCIConfig](settings)
if err != nil {
return nil, fmt.Errorf("error decoding settings: %w", err)
}
return &GolangCIPlugin{config: s}, nil
}
// GolangCIPlugin constructs a new plugin for the golangci-lint
// plugin pattern.
// This allows golangci-lint to build a version of itself, containing
// all of the anaylzers included in KAL.
type GolangCIPlugin struct {
config config.GolangCIConfig
}
// BuildAnalyzers returns all of the analyzers to run, based on the configuration.
func (f *GolangCIPlugin) BuildAnalyzers() ([]*analysis.Analyzer, error) {
if err := validation.ValidateGolangCIConfig(f.config, field.NewPath("")); err != nil {
return nil, fmt.Errorf("error in KAL configuration: %w", err)
}
registry := kalanalysis.NewRegistry()
analyzers, err := registry.InitializeLinters(f.config.Linters, f.config.LintersConfig)
if err != nil {
return nil, fmt.Errorf("error initializing analyzers: %w", err)
}
return analyzers, nil
}
// GetLoadMode implements the golangci-lint plugin interface.
func (f *GolangCIPlugin) GetLoadMode() string {
return register.LoadModeTypesInfo
}