loadPlugin loads a single plugin.
(ctx context.Context, path string)
| 76 | |
| 77 | // loadPlugin loads a single plugin. |
| 78 | func (m *Manager) loadPlugin(ctx context.Context, path string) error { |
| 79 | client := plugin.NewClient(&plugin.ClientConfig{ |
| 80 | HandshakeConfig: Handshake, |
| 81 | Plugins: PluginMap, |
| 82 | Cmd: exec.Command(path), |
| 83 | AllowedProtocols: []plugin.Protocol{ |
| 84 | plugin.ProtocolNetRPC, |
| 85 | }, |
| 86 | // By default the go-plugin logger is set to TRACE level, which is very verbose. |
| 87 | // We can't set it to the level set by the command at this point, because we need to |
| 88 | // load the commands first before running the cobra command where the debug flag is set |
| 89 | // We set it to WARN level, so we don't get too much noise from the plugins. |
| 90 | Logger: hclog.New(&hclog.LoggerOptions{ |
| 91 | Output: hclog.DefaultOutput, |
| 92 | Level: hclog.Warn, |
| 93 | Name: "plugin", |
| 94 | }), |
| 95 | }) |
| 96 | |
| 97 | // Connect via RPC |
| 98 | rpcClient, err := client.Client() |
| 99 | if err != nil { |
| 100 | client.Kill() |
| 101 | return fmt.Errorf("failed to create RPC client: %w", err) |
| 102 | } |
| 103 | |
| 104 | // Request the plugin |
| 105 | raw, err := rpcClient.Dispense("chainloop") |
| 106 | if err != nil { |
| 107 | client.Kill() |
| 108 | return fmt.Errorf("failed to dispense plugin: %w", err) |
| 109 | } |
| 110 | |
| 111 | // Cast to our interface |
| 112 | chainloopPlugin, ok := raw.(Plugin) |
| 113 | if !ok { |
| 114 | client.Kill() |
| 115 | return fmt.Errorf("plugin does not implement Plugin interface") |
| 116 | } |
| 117 | |
| 118 | // Get plugin metadata |
| 119 | metadata, err := chainloopPlugin.GetMetadata(ctx) |
| 120 | if err != nil { |
| 121 | client.Kill() |
| 122 | return fmt.Errorf("failed to get plugin metadata: %w", err) |
| 123 | } |
| 124 | |
| 125 | // Store the plugin |
| 126 | m.plugins[metadata.Name] = &LoadedPlugin{ |
| 127 | Path: path, |
| 128 | Plugin: chainloopPlugin, |
| 129 | Metadata: metadata, |
| 130 | } |
| 131 | m.pluginClients[metadata.Name] = client |
| 132 | |
| 133 | return nil |
| 134 | } |
| 135 |
no test coverage detected