| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "reasonix/internal/config" |
| 7 | "reasonix/internal/plugin" |
| 8 | ) |
| 9 | |
| 10 | // capabilityServerInventory adds explicitly supplied host-session servers to |
| 11 | // the capability runtime and marks only those additions enabled. |
| 12 | func capabilityServerInventory(configEntries []config.PluginEntry, root string, opts PluginSpecOptions, hostSession []plugin.Spec, enabled map[string]bool) ([]config.PluginEntry, []plugin.Spec) { |
| 13 | for _, spec := range hostSession { |
| 14 | if name := strings.TrimSpace(spec.Name); name != "" { |
| 15 | enabled[name] = true |
| 16 | } |
| 17 | } |
| 18 | configSpecs := PluginSpecsForRootWithOptions(configEntries, root, opts) |
| 19 | return mergeHostSessionCapabilitySpecs(configEntries, configSpecs, hostSession) |
| 20 | } |
| 21 | |
| 22 | // mergeHostSessionCapabilitySpecs makes the host-session endpoint authoritative |
| 23 | // on a name collision and drops the shadowed config entry with its stale state. |
| 24 | func mergeHostSessionCapabilitySpecs(configEntries []config.PluginEntry, configSpecs, hostSession []plugin.Spec) ([]config.PluginEntry, []plugin.Spec) { |
| 25 | if len(hostSession) == 0 { |
| 26 | return configEntries, configSpecs |
| 27 | } |
| 28 | shadowed := make(map[string]bool, len(hostSession)) |
| 29 | for _, spec := range hostSession { |
| 30 | if name := strings.TrimSpace(spec.Name); name != "" { |
| 31 | shadowed[name] = true |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | specs := make([]plugin.Spec, 0, len(configSpecs)+len(hostSession)) |
| 36 | for _, spec := range configSpecs { |
| 37 | if !shadowed[strings.TrimSpace(spec.Name)] { |
| 38 | specs = append(specs, spec) |
| 39 | } |
| 40 | } |
| 41 | specs = append(specs, hostSession...) |
| 42 | |
| 43 | entries := make([]config.PluginEntry, 0, len(configEntries)) |
| 44 | for _, entry := range configEntries { |
| 45 | if !shadowed[strings.TrimSpace(entry.Name)] { |
| 46 | entries = append(entries, entry) |
| 47 | } |
| 48 | } |
| 49 | return entries, specs |
| 50 | } |
| 51 |