File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,10 @@ limitations under the License.
16
16
17
17
package plugins
18
18
19
- import "context"
19
+ import (
20
+ "context"
21
+ "fmt"
22
+ )
20
23
21
24
// Plugin defines the interface for a plugin.
22
25
// This interface should be embedded in all plugins across the code.
@@ -50,3 +53,18 @@ type HandlePlugins interface {
50
53
// GetAllPluginsWithNames returns all of the known plugins with their names
51
54
GetAllPluginsWithNames () map [string ]Plugin
52
55
}
56
+
57
+ // PluginByType retrieves the specified plugin by name and verifies its type
58
+ func PluginByType [P Plugin ](handlePlugins HandlePlugins , name string ) (P , error ) {
59
+ var zero P
60
+
61
+ rawPlugin := handlePlugins .Plugin (name )
62
+ if rawPlugin == nil {
63
+ return zero , fmt .Errorf ("there is no plugin with the name '%s' defined" , name )
64
+ }
65
+ thePlugin , ok := rawPlugin .(P )
66
+ if ! ok {
67
+ return zero , fmt .Errorf ("the plugin with the name '%s' is not an instance of %T" , name , zero )
68
+ }
69
+ return thePlugin , nil
70
+ }
You can’t perform that action at this time.
0 commit comments