|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "plugin" |
| 20 | + |
| 21 | + "github.com/googleapis/api-linter/lint" |
| 22 | +) |
| 23 | + |
| 24 | +// addRulesFuncName is the expected name of the function exported by plugins. |
| 25 | +const addRulesFuncName = "AddCustomRules" |
| 26 | + |
| 27 | +// loadCustomRulePlugin loads a plugin from the given path and registers |
| 28 | +// its rules with the provided registry. |
| 29 | +func loadCustomRulePlugin(pluginPath string, registry lint.RuleRegistry) error { |
| 30 | + // Open the plugin |
| 31 | + p, err := plugin.Open(pluginPath) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("failed to open plugin %q: %v", pluginPath, err) |
| 34 | + } |
| 35 | + |
| 36 | + // Look up the AddCustomRules function |
| 37 | + sym, err := p.Lookup(addRulesFuncName) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("plugin %q does not export %q function: %v", |
| 40 | + pluginPath, addRulesFuncName, err) |
| 41 | + } |
| 42 | + |
| 43 | + // Cast to the expected function type |
| 44 | + addRulesFunc, ok := sym.(func(lint.RuleRegistry) error) |
| 45 | + if !ok { |
| 46 | + return fmt.Errorf("plugin %q exports %q with wrong signature", |
| 47 | + pluginPath, addRulesFuncName) |
| 48 | + } |
| 49 | + |
| 50 | + // Call the function to add custom rules |
| 51 | + if err := addRulesFunc(registry); err != nil { |
| 52 | + return fmt.Errorf("error registering rules from plugin %q: %v", |
| 53 | + pluginPath, err) |
| 54 | + } |
| 55 | + |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +// loadCustomRulePlugins loads all plugins from the given paths and registers |
| 60 | +// their rules with the provided registry. |
| 61 | +func loadCustomRulePlugins(pluginPaths []string, registry lint.RuleRegistry) error { |
| 62 | + for _, path := range pluginPaths { |
| 63 | + if err := loadCustomRulePlugin(path, registry); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
0 commit comments