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