UNPKG

docker-pilot

Version:

A powerful, scalable Docker CLI library for managing containerized applications of any size

145 lines 6.48 kB
"use strict"; /** * Down Command - Stop all services or specific service */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DownCommand = void 0; const BaseCommand_1 = require("./BaseCommand"); const child_process_1 = require("child_process"); const util_1 = require("util"); const execAsync = (0, util_1.promisify)(child_process_1.exec); class DownCommand extends BaseCommand_1.BaseCommand { constructor(context) { super('down', 'Stop all services or a specific service', 'docker-pilot down [service-name] [options]', context); } async execute(args, _options) { const { args: parsedArgs, options: parsedOptions } = this.parseOptions(args); const serviceName = parsedArgs[0]; try { if (!(await this.checkDockerAvailable())) { return this.createErrorResult(this.i18n.t('cmd.docker_not_available')); } // Confirm action if it's destructive if (parsedOptions['volumes'] || parsedOptions['remove-orphans']) { const volumeAction = parsedOptions['volumes'] ? this.i18n.t('cmd.confirm_volumes') : ''; const target = serviceName || this.i18n.t('cmd.all_services'); const action = volumeAction + this.i18n.t('cmd.confirm_stop', { target }); const confirmed = await this.confirmAction(this.i18n.t('cmd.confirm_action', { action })); if (!confirmed) { return this.createErrorResult(this.i18n.t('cmd.operation_cancelled')); } } const target = serviceName || this.i18n.t('cmd.all_services'); this.logger.loading(this.i18n.t('cmd.stopping', { target })); // Execute the real Docker command const { result: stopOutput, executionTime } = await this.measureExecutionTime(async () => { return await this.stopServices(serviceName, parsedOptions); }); // Display stop results this.showStopResults(stopOutput); this.logger.success(this.i18n.t('cmd.stopped_success', { target })); const resultMessage = serviceName ? `service "${serviceName}"` : 'all services'; return this.createSuccessResult(`Successfully stopped ${resultMessage}`, executionTime); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; const target = serviceName || this.i18n.t('cmd.all_services'); this.logger.error(this.i18n.t('cmd.stop_failed', { target }), error); return this.createErrorResult(errorMessage); } } showExamples() { this.logger.info('\nExamples:'); this.logger.info(' docker-pilot down # Stop all services'); this.logger.info(' docker-pilot down backend # Stop only backend service'); this.logger.info(' docker-pilot down --volumes # Stop and remove volumes'); this.logger.info(' docker-pilot down --remove-orphans # Stop and remove orphan containers'); } async stopServices(serviceName, options) { try { // Build Docker command const composeFile = this.context.composeFile; const downArgs = ['docker', 'compose']; // Add compose file if available (always should be from context) if (composeFile) { downArgs.push('-f', composeFile); } else { this.logger.warn('DownCommand: No compose file in context, command may fail'); } downArgs.push('down'); // Add options if (options?.['volumes'] || options?.['v']) { downArgs.push('--volumes'); } if (options?.['remove-orphans']) { downArgs.push('--remove-orphans'); } if (options?.['rmi']) { downArgs.push('--rmi', options['rmi']); } if (options?.['timeout'] || options?.['t']) { downArgs.push('--timeout', (options['timeout'] || options['t'])); } // Add specific service if provided if (serviceName) { downArgs.push(serviceName); } const command = downArgs.join(' '); this.logger.debug(`Executing: ${command}`); const result = await this.execDockerCommand(command); return result.stdout || 'Services stopped successfully'; } catch (error) { this.logger.warn(`Failed to stop services: ${error instanceof Error ? error.message : 'Unknown error'}`); throw error; } } async execDockerCommand(command) { try { const result = await execAsync(command, { cwd: this.context.workingDirectory, maxBuffer: 1024 * 1024 * 5 // 5MB buffer }); return result; } catch (error) { // Some docker commands return non-zero exit codes but still have useful output if (error.stdout) { return { stdout: error.stdout, stderr: error.stderr || '' }; } throw error; } } /** * Display stop results with formatting */ showStopResults(output) { if (!output || output.trim() === '' || output === 'Services stopped successfully') { // No specific output to show, stop was clean return; } this.logger.newLine(); this.logger.info('📋 Stop details:'); this.logger.separator('-', 30); // Display the output const lines = output.split('\n').filter(line => line.trim() !== ''); lines.forEach(line => { if (line.includes('ERROR') || line.includes('error')) { this.logger.error(` ${line}`); } else if (line.includes('WARN') || line.includes('warn')) { this.logger.warn(` ${line}`); } else if (line.includes('Stopped') || line.includes('Removed') || line.includes('Stopping')) { this.logger.success(` ${line}`); } else { this.logger.info(` ${line}`); } }); this.logger.newLine(); } } exports.DownCommand = DownCommand; //# sourceMappingURL=DownCommand.js.map