/** * Service subcommands for CLI — all go through SDK. * * @module */ import { runAction, runList, runGet, chalk, formatStatus, getCliSdk, } from "../actions.js"; import type { ICoolifyService } from "../../coolify/types.js"; import type { ICoolifyEnvVar } from "../../coolify/index.js"; /** List all services. */ export const svcListCommand = () => runList("service(s)", (s) => s.services.list(), [ { header: "UUID", value: (s) => s.uuid }, { header: "Name", value: (s) => s.name || "-" }, { header: "Type", value: (s) => s.type || "-" }, { header: "Status", value: (s) => formatStatus(s.status) }, ]); /** Get service details. */ export const svcGetCommand = (uuid: string) => runGet( uuid, "service", (s, u) => s.services.get(u), (svc) => { console.log(chalk.cyan("Service Details:")); console.log(chalk.gray("UUID: ") + svc.uuid); console.log(chalk.gray("Name: ") + svc.name); console.log(chalk.gray("Type: ") + (svc.type || "-")); console.log(chalk.gray("Status: ") + formatStatus(svc.status)); }, ); /** Start a service. */ export const svcStartCommand = (uuid: string) => runAction( uuid, "Starting service", (s, u) => s.services.start(u), (u) => `Service started: ${u}`, ); /** Stop a service. */ export const svcStopCommand = (uuid: string) => runAction( uuid, "Stopping service", (s, u) => s.services.stop(u), (u) => `Service stopped: ${u}`, ); /** Restart a service. */ export const svcRestartCommand = (uuid: string) => runAction( uuid, "Restarting service", (s, u) => s.services.restart(u), (u) => `Service restarted: ${u}`, ); /** Delete a service. */ export const svcDeleteCommand = (uuid: string) => runAction( uuid, "Deleting service", (s, u) => s.services.delete(u), (u) => `Service deleted: ${u}`, ); /** List env vars for a service. */ export const svcEnvCommand = (uuid: string) => runList("env var(s)", (s) => s.services.envVars(uuid), [ { header: "Key", value: (e) => e.key }, { header: "Value", value: (e) => e.value }, { header: "Runtime", value: (e) => (e.is_runtime ? "Yes" : "No") }, ]); /** * Set env var for a service. * * Note: `services.setEnv` delegates to the bulk endpoint internally, so * calling this for a key that already exists as an override UPDATES the * value instead of failing with 409. Repeatable across deploys. */ export async function svcSetEnvCommand( uuid: string, keyValue: string, ): Promise { const [key, ...rest] = keyValue.split("="); const value = rest.join("="); if (!key || value === undefined) { console.error(chalk.red("Error: Use format KEY=VALUE")); return; } try { await getCliSdk().services.setEnv(uuid, { key, value }); console.log(chalk.green(`✓ Set ${chalk.bold(key)} for service ${uuid}`)); } catch (error) { console.error( chalk.red( `Error: ${error instanceof Error ? error.message : String(error)}`, ), ); } }