WithPlugins creates a new CNPG-I client for plugins in a certain repository, loading the plugins with the specified name
(ctx context.Context, repository repository.Interface, names ...string)
| 77 | // WithPlugins creates a new CNPG-I client for plugins in a certain repository, |
| 78 | // loading the plugins with the specified name |
| 79 | func WithPlugins(ctx context.Context, repository repository.Interface, names ...string) (Client, error) { |
| 80 | result := &data{ |
| 81 | repository: repository, |
| 82 | } |
| 83 | |
| 84 | load := func(names ...string) error { |
| 85 | for _, name := range names { |
| 86 | pluginData, err := result.repository.GetConnection(ctx, name) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | result.plugins = append(result.plugins, pluginData) |
| 92 | } |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | // The following ensures that each plugin is loaded just one |
| 97 | // time, even when the same plugin has been requested multiple |
| 98 | // times. |
| 99 | loadingPlugins := stringset.From(names) |
| 100 | uniqueSortedPluginName := loadingPlugins.ToSortedList() |
| 101 | |
| 102 | if err := load(uniqueSortedPluginName...); err != nil { |
| 103 | result.Close(ctx) |
| 104 | return nil, err |
| 105 | } |
| 106 | |
| 107 | return result, nil |
| 108 | } |
no test coverage detected