All files / src/cli/setup/flows configure-existing.ts

96.96% Statements 96/99
81.53% Branches 53/65
88.88% Functions 16/18
96.77% Lines 90/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233          2x   2x 2x 2x           2x 15x     15x   15x 24x 24x 24x     15x   3x 3x     15x     15x   15x 12x     12x       15x 12x     12x       15x 3x 2x         1x             15x         15x         15x 2x     13x   5x     5x       6x 3x 3x 3x 3x 2x   1x 1x   3x                                 5x   5x             5x 1x     4x     4x   4x   2x         2x 1x   1x     3x 3x 3x 3x   3x 3x 2x     3x 3x 1x 1x       3x     2x               5x   5x             5x 1x     4x     4x   4x 2x         2x 1x   1x     3x 3x 3x 3x   3x 3x   3x 2x     3x 1x 1x       3x     2x        
/**
 * Configure existing setup flow.
 * Allows users to update, add, or remove configurations for detected clients.
 */
 
import * as p from "@clack/prompts";
import { DiscoveryResult, SetupResult } from "../types";
import { InstallableClient, CLIENT_METADATA } from "../../install/types";
import { installToClients } from "../../install/installers";
import { buildServerConfigFromEnv } from "../../install/install-command";
 
/**
 * Run the configure-existing flow.
 * Shows detected clients with their status and offers actions.
 */
export async function runConfigureExistingFlow(discovery: DiscoveryResult): Promise<SetupResult> {
  const { detected, configured, unconfigured } = discovery.clients;
 
  // Show current status
  p.log.step("Current configuration:");
 
  for (const client of detected) {
    const metadata = CLIENT_METADATA[client.client];
    const status = client.alreadyConfigured ? "✓ configured" : "○ not configured";
    console.log(`  ${status}  ${metadata.name}`);
  }
 
  if (discovery.docker.container) {
    const containerStatus =
      discovery.docker.container.status === "running" ? "✓ running" : "○ stopped";
    console.log(`  ${containerStatus}  Docker container`);
  }
 
  console.log("");
 
  // Determine available actions
  const actionOptions: { value: string; label: string; hint?: string }[] = [];
 
  if (unconfigured.length > 0) {
    actionOptions.push({
      value: "add-clients",
      label: `Add gitlab-mcp to ${unconfigured.length} unconfigured client(s)`,
      hint: unconfigured.map(c => CLIENT_METADATA[c.client].name).join(", "),
    });
  }
 
  if (configured.length > 0) {
    actionOptions.push({
      value: "update-clients",
      label: `Update ${configured.length} existing configuration(s)`,
      hint: configured.map(c => CLIENT_METADATA[c.client].name).join(", "),
    });
  }
 
  if (discovery.docker.container) {
    if (discovery.docker.container.status === "running") {
      actionOptions.push({
        value: "restart-docker",
        label: "Restart Docker container",
      });
    } else {
      actionOptions.push({
        value: "start-docker",
        label: "Start Docker container",
      });
    }
  }
 
  actionOptions.push({
    value: "cancel",
    label: "Cancel",
  });
 
  const action = await p.select({
    message: "What would you like to do?",
    options: actionOptions,
  });
 
  if (p.isCancel(action) || action === "cancel") {
    return { success: false, mode: "configure-existing", error: "Cancelled" };
  }
 
  switch (action) {
    case "add-clients":
      return addToClients(unconfigured.map(c => c.client));
 
    case "update-clients":
      return updateClients(configured.map(c => c.client));
 
    case "restart-docker":
    case "start-docker": {
      const { startContainer, restartContainer } = await import("../../docker/docker-utils");
      const spinner = p.spinner();
      spinner.start(action === "restart-docker" ? "Restarting..." : "Starting...");
      const result = action === "restart-docker" ? restartContainer() : startContainer();
      if (result.success) {
        spinner.stop("Done!");
      } else {
        spinner.stop("Failed");
        p.log.error(result.error ?? "Unknown error");
      }
      return {
        success: result.success,
        mode: "configure-existing",
        error: result.success ? undefined : (result.error ?? "Container operation failed"),
      };
    }
 
    default:
      return { success: false, mode: "configure-existing", error: "Unknown action" };
  }
}
 
/**
 * Add gitlab-mcp configuration to unconfigured clients
 */
async function addToClients(clients: InstallableClient[]): Promise<SetupResult> {
  // Select which clients to configure
  const selectedClients = await p.multiselect({
    message: "Select clients to add gitlab-mcp to:",
    options: clients.map(client => ({
      value: client,
      label: CLIENT_METADATA[client].name,
    })),
    required: true,
  });
 
  if (p.isCancel(selectedClients)) {
    return { success: false, mode: "configure-existing", error: "Cancelled" };
  }
 
  const targetClients = selectedClients;
 
  // Build server config from current environment
  const serverConfig = buildServerConfigFromEnv();
 
  if (!serverConfig.env.GITLAB_TOKEN) {
    // Need to get a token
    const token = await p.password({
      message: "Enter GitLab Personal Access Token:",
      validate: v => (!v || v.length < 10 ? "Token is too short" : undefined),
    });
 
    if (p.isCancel(token)) {
      return { success: false, mode: "configure-existing", error: "Cancelled" };
    }
    serverConfig.env.GITLAB_TOKEN = token;
  }
 
  const spinner = p.spinner();
  spinner.start("Installing configuration...");
  const results = installToClients(targetClients, serverConfig, false);
  spinner.stop("Done!");
 
  const successful = results.filter(r => r.success);
  if (successful.length > 0) {
    p.log.success(`Added to ${successful.length} client(s)`);
  }
 
  const failed = results.filter(r => !r.success);
  if (failed.length > 0) {
    for (const r of failed) {
      p.log.error(`  ${CLIENT_METADATA[r.client].name}: ${r.error}`);
    }
  }
 
  return {
    success: successful.length > 0,
    mode: "configure-existing",
    configuredClients: successful.map(r => r.client),
  };
}
 
/**
 * Update existing client configurations
 */
async function updateClients(clients: InstallableClient[]): Promise<SetupResult> {
  const selectedClients = await p.multiselect({
    message: "Select clients to update:",
    options: clients.map(client => ({
      value: client,
      label: CLIENT_METADATA[client].name,
    })),
    required: true,
  });
 
  if (p.isCancel(selectedClients)) {
    return { success: false, mode: "configure-existing", error: "Cancelled" };
  }
 
  const targetClients = selectedClients;
 
  // Build server config from current environment
  const serverConfig = buildServerConfigFromEnv();
 
  if (!serverConfig.env.GITLAB_TOKEN) {
    const token = await p.password({
      message: "Enter GitLab Personal Access Token:",
      validate: v => (!v || v.length < 10 ? "Token is too short" : undefined),
    });
 
    if (p.isCancel(token)) {
      return { success: false, mode: "configure-existing", error: "Cancelled" };
    }
    serverConfig.env.GITLAB_TOKEN = token;
  }
 
  const spinner = p.spinner();
  spinner.start("Updating configuration...");
  const results = installToClients(targetClients, serverConfig, true);
  spinner.stop("Done!");
 
  const successful = results.filter(r => r.success);
  const failed = results.filter(r => !r.success);
 
  if (successful.length > 0) {
    p.log.success(`Updated ${successful.length} client(s)`);
  }
 
  if (failed.length > 0) {
    for (const r of failed) {
      p.log.error(`  ${CLIENT_METADATA[r.client].name}: ${r.error}`);
    }
  }
 
  return {
    success: successful.length > 0,
    mode: "configure-existing",
    configuredClients: successful.map(r => r.client),
    error: failed.length > 0 ? `Failed to update ${failed.length} client(s)` : undefined,
  };
}