UNPKG

sesterce-cli

Version:

A powerful command-line interface tool for managing Sesterce Cloud services. Sesterce CLI provides easy access to GPU cloud instances, AI inference services, container registries, and SSH key management directly from your terminal.

115 lines (99 loc) 3.43 kB
import { Registry } from "@/modules/registries/domain/registry"; import { listRegistries } from "@/modules/registries/use-cases/list-registries"; import { updateRegistry } from "@/modules/registries/use-cases/update-registry"; import { input, search } from "@inquirer/prompts"; import type { Command } from "commander"; const printRegistry = (registry: Registry) => { return `${registry.name} - ${registry.url} - ${registry.username}`; }; export function createRegistryUpdateCommand(registryCommand: Command) { registryCommand .command("update") .description("Update a registry") .option( "-i, --registry-id <registryId>", "The ID of the registry to update" ) .action(async (args) => { let registryId = args.registryId; let registry: Registry | null = null; console.log("Loading registries..."); const registriesResult = await listRegistries.execute(); if (registriesResult.isLeft()) { console.error(registriesResult.value.message); return; } const registries = registriesResult.value; if (registries.length === 0) { console.log("No registries found"); return; } if (registryId) { registry = registries.find((r) => r._id === registryId) ?? null; } else { const selectedRegistry = await search({ message: "Select a registry to update", source: async (input, { signal }) => { if (!input) { return registries.map((registry) => ({ name: printRegistry(registry), value: registry, })); } return registries .filter( (registry) => registry.name.toLowerCase().includes(input.toLowerCase()) || registry.url.toLowerCase().includes(input) || registry.username.toLowerCase().includes(input) ) .map((registry) => ({ name: printRegistry(registry), value: registry, })); }, }); registryId = selectedRegistry._id; registry = selectedRegistry; } if (!registry) { console.error("Registry not found"); return; } const url = await input({ message: "Registry URL", required: true, default: registry.url, validate: (value) => { // Regex to validate docker.io URLs const dockerIoRegex = /^docker\.io\/([a-zA-Z0-9_-]+(?:\/[a-zA-Z0-9_-]+)*)\/([a-zA-Z0-9_-]+)(?::([a-zA-Z0-9_.-]+))?$/; if (!dockerIoRegex.test(value)) { return "Please enter a valid docker.io URL format: docker.io/username/repo/image[:tag]"; } return true; }, }); const username = await input({ message: "Registry username", required: true, default: registry.username, }); const password = await input({ message: "Registry password", required: true, }); console.log("Updating registry..."); const result = await updateRegistry.execute({ id: registryId, url, username, password, }); if (result.isLeft()) { console.error(result.value.message); return; } console.log("Registry updated successfully!"); }); }