|
| 1 | +package workingset |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/docker/mcp-gateway/pkg/db" |
| 11 | + "github.com/docker/mcp-gateway/pkg/oci" |
| 12 | + "github.com/docker/mcp-gateway/pkg/registryapi" |
| 13 | +) |
| 14 | + |
| 15 | +func AddServers(ctx context.Context, dao db.DAO, registryClient registryapi.Client, ociService oci.Service, id string, servers []string) error { |
| 16 | + if len(servers) == 0 { |
| 17 | + return fmt.Errorf("at least one server must be specified") |
| 18 | + } |
| 19 | + |
| 20 | + dbWorkingSet, err := dao.GetWorkingSet(ctx, id) |
| 21 | + if err != nil { |
| 22 | + if errors.Is(err, sql.ErrNoRows) { |
| 23 | + return fmt.Errorf("working set %s not found", id) |
| 24 | + } |
| 25 | + return fmt.Errorf("failed to get working set: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + workingSet := NewFromDb(dbWorkingSet) |
| 29 | + |
| 30 | + if err := workingSet.EnsureSnapshotsResolved(ctx, ociService); err != nil { |
| 31 | + return fmt.Errorf("failed to resolve snapshots: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + newServers := make([]Server, len(servers)) |
| 35 | + for i, server := range servers { |
| 36 | + s, err := resolveServerFromString(ctx, registryClient, ociService, server) |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("invalid server value: %w", err) |
| 39 | + } |
| 40 | + newServers[i] = s |
| 41 | + } |
| 42 | + |
| 43 | + workingSet.Servers = append(workingSet.Servers, newServers...) |
| 44 | + |
| 45 | + if err := workingSet.Validate(); err != nil { |
| 46 | + return fmt.Errorf("invalid working set: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + err = dao.UpdateWorkingSet(ctx, workingSet.ToDb()) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("failed to update working set: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Printf("Added %d server(s) to working set %s\n", len(newServers), id) |
| 55 | + |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func RemoveServers(ctx context.Context, dao db.DAO, id string, serverRefs []string) error { |
| 60 | + if len(serverRefs) == 0 { |
| 61 | + return fmt.Errorf("at least one server must be specified") |
| 62 | + } |
| 63 | + |
| 64 | + dbWorkingSet, err := dao.GetWorkingSet(ctx, id) |
| 65 | + if err != nil { |
| 66 | + if errors.Is(err, sql.ErrNoRows) { |
| 67 | + return fmt.Errorf("working set %s not found", id) |
| 68 | + } |
| 69 | + return fmt.Errorf("failed to get working set: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + workingSet := NewFromDb(dbWorkingSet) |
| 73 | + |
| 74 | + // Build a set of servers to remove (strip protocol scheme for comparison) |
| 75 | + refsToRemove := make(map[string]bool) |
| 76 | + for _, ref := range serverRefs { |
| 77 | + normalized := stripProtocol(ref) |
| 78 | + refsToRemove[normalized] = true |
| 79 | + } |
| 80 | + |
| 81 | + // Filter out the servers to remove |
| 82 | + originalCount := len(workingSet.Servers) |
| 83 | + filtered := make([]Server, 0, len(workingSet.Servers)) |
| 84 | + for _, server := range workingSet.Servers { |
| 85 | + shouldKeep := true |
| 86 | + |
| 87 | + switch server.Type { |
| 88 | + case ServerTypeImage: |
| 89 | + if refsToRemove[stripProtocol(server.Image)] { |
| 90 | + shouldKeep = false |
| 91 | + } |
| 92 | + case ServerTypeRegistry: |
| 93 | + if refsToRemove[stripProtocol(server.Source)] { |
| 94 | + shouldKeep = false |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if shouldKeep { |
| 99 | + filtered = append(filtered, server) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + removedCount := originalCount - len(filtered) |
| 104 | + if removedCount == 0 { |
| 105 | + return fmt.Errorf("no matching servers found to remove") |
| 106 | + } |
| 107 | + |
| 108 | + workingSet.Servers = filtered |
| 109 | + |
| 110 | + if err := workingSet.Validate(); err != nil { |
| 111 | + return fmt.Errorf("invalid working set: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + err = dao.UpdateWorkingSet(ctx, workingSet.ToDb()) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to update working set: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + fmt.Printf("Removed %d server(s) from working set %s\n", removedCount, id) |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +// stripProtocol removes the protocol scheme (everything before and including "://") from a URI |
| 125 | +func stripProtocol(uri string) string { |
| 126 | + if idx := strings.Index(uri, "://"); idx != -1 { |
| 127 | + return uri[idx+3:] |
| 128 | + } |
| 129 | + return uri |
| 130 | +} |
0 commit comments