219 lines
6.5 KiB
TypeScript
219 lines
6.5 KiB
TypeScript
import * as plugins from './mod.plugins.js';
|
|
import * as helpers from './helpers.js';
|
|
import { ServiceManager } from './classes.servicemanager.js';
|
|
import { logger } from '../gitzone.logging.js';
|
|
|
|
export const run = async (argvArg: any) => {
|
|
const serviceManager = new ServiceManager();
|
|
await serviceManager.init();
|
|
|
|
const command = argvArg._[1] || 'help';
|
|
const service = argvArg._[2] || 'all';
|
|
|
|
switch (command) {
|
|
case 'start':
|
|
await handleStart(serviceManager, service);
|
|
break;
|
|
|
|
case 'stop':
|
|
await handleStop(serviceManager, service);
|
|
break;
|
|
|
|
case 'restart':
|
|
await handleRestart(serviceManager, service);
|
|
break;
|
|
|
|
case 'status':
|
|
await serviceManager.showStatus();
|
|
break;
|
|
|
|
case 'config':
|
|
await serviceManager.showConfig();
|
|
break;
|
|
|
|
case 'compass':
|
|
await serviceManager.showCompassConnection();
|
|
break;
|
|
|
|
case 'logs':
|
|
const lines = parseInt(argvArg._[3]) || 20;
|
|
await serviceManager.showLogs(service, lines);
|
|
break;
|
|
|
|
case 'remove':
|
|
await handleRemove(serviceManager);
|
|
break;
|
|
|
|
case 'clean':
|
|
await handleClean(serviceManager);
|
|
break;
|
|
|
|
case 'help':
|
|
default:
|
|
showHelp();
|
|
break;
|
|
}
|
|
};
|
|
|
|
async function handleStart(serviceManager: ServiceManager, service: string) {
|
|
helpers.printHeader('Starting Services');
|
|
|
|
switch (service) {
|
|
case 'mongo':
|
|
case 'mongodb':
|
|
await serviceManager.startMongoDB();
|
|
break;
|
|
|
|
case 'minio':
|
|
case 's3':
|
|
await serviceManager.startMinIO();
|
|
break;
|
|
|
|
case 'all':
|
|
case '':
|
|
await serviceManager.startMongoDB();
|
|
console.log();
|
|
await serviceManager.startMinIO();
|
|
break;
|
|
|
|
default:
|
|
logger.log('error', `Unknown service: ${service}`);
|
|
logger.log('note', 'Use: mongo, s3, or all');
|
|
break;
|
|
}
|
|
}
|
|
|
|
async function handleStop(serviceManager: ServiceManager, service: string) {
|
|
helpers.printHeader('Stopping Services');
|
|
|
|
switch (service) {
|
|
case 'mongo':
|
|
case 'mongodb':
|
|
await serviceManager.stopMongoDB();
|
|
break;
|
|
|
|
case 'minio':
|
|
case 's3':
|
|
await serviceManager.stopMinIO();
|
|
break;
|
|
|
|
case 'all':
|
|
case '':
|
|
await serviceManager.stopMongoDB();
|
|
console.log();
|
|
await serviceManager.stopMinIO();
|
|
break;
|
|
|
|
default:
|
|
logger.log('error', `Unknown service: ${service}`);
|
|
logger.log('note', 'Use: mongo, s3, or all');
|
|
break;
|
|
}
|
|
}
|
|
|
|
async function handleRestart(serviceManager: ServiceManager, service: string) {
|
|
helpers.printHeader('Restarting Services');
|
|
|
|
switch (service) {
|
|
case 'mongo':
|
|
case 'mongodb':
|
|
await serviceManager.stopMongoDB();
|
|
await plugins.smartdelay.delayFor(2000);
|
|
await serviceManager.startMongoDB();
|
|
break;
|
|
|
|
case 'minio':
|
|
case 's3':
|
|
await serviceManager.stopMinIO();
|
|
await plugins.smartdelay.delayFor(2000);
|
|
await serviceManager.startMinIO();
|
|
break;
|
|
|
|
case 'all':
|
|
case '':
|
|
await serviceManager.stopMongoDB();
|
|
await serviceManager.stopMinIO();
|
|
await plugins.smartdelay.delayFor(2000);
|
|
await serviceManager.startMongoDB();
|
|
console.log();
|
|
await serviceManager.startMinIO();
|
|
break;
|
|
|
|
default:
|
|
logger.log('error', `Unknown service: ${service}`);
|
|
break;
|
|
}
|
|
}
|
|
|
|
async function handleRemove(serviceManager: ServiceManager) {
|
|
helpers.printHeader('Removing Containers');
|
|
logger.log('note', '⚠️ This will remove containers but preserve data');
|
|
|
|
const shouldContinue = await plugins.smartinteract.SmartInteract.getCliConfirmation('Continue?', false);
|
|
|
|
if (shouldContinue) {
|
|
await serviceManager.removeContainers();
|
|
} else {
|
|
logger.log('note', 'Cancelled');
|
|
}
|
|
}
|
|
|
|
async function handleClean(serviceManager: ServiceManager) {
|
|
helpers.printHeader('Clean All');
|
|
logger.log('error', '⚠️ WARNING: This will remove all containers and data!');
|
|
logger.log('error', 'This action cannot be undone!');
|
|
|
|
const smartinteraction = new plugins.smartinteract.SmartInteract();
|
|
const confirmAnswer = await smartinteraction.askQuestion({
|
|
name: 'confirm',
|
|
type: 'input',
|
|
message: 'Type "yes" to confirm:',
|
|
default: 'no'
|
|
});
|
|
|
|
if (confirmAnswer.value === 'yes') {
|
|
await serviceManager.removeContainers();
|
|
console.log();
|
|
await serviceManager.cleanData();
|
|
logger.log('ok', 'All cleaned ✓');
|
|
} else {
|
|
logger.log('note', 'Cancelled');
|
|
}
|
|
}
|
|
|
|
function showHelp() {
|
|
helpers.printHeader('GitZone Services Manager');
|
|
|
|
logger.log('ok', 'Usage: gitzone services [command] [options]');
|
|
console.log();
|
|
|
|
logger.log('note', 'Commands:');
|
|
logger.log('info', ' start [service] Start services (mongo|s3|all)');
|
|
logger.log('info', ' stop [service] Stop services (mongo|s3|all)');
|
|
logger.log('info', ' restart [service] Restart services (mongo|s3|all)');
|
|
logger.log('info', ' status Show service status');
|
|
logger.log('info', ' config Show current configuration');
|
|
logger.log('info', ' compass Show MongoDB Compass connection string');
|
|
logger.log('info', ' logs [service] Show logs (mongo|s3|all) [lines]');
|
|
logger.log('info', ' remove Remove all containers');
|
|
logger.log('info', ' clean Remove all containers and data ⚠️');
|
|
logger.log('info', ' help Show this help message');
|
|
console.log();
|
|
|
|
logger.log('note', 'Features:');
|
|
logger.log('info', ' • Auto-creates .nogit/env.json with smart defaults');
|
|
logger.log('info', ' • Random ports (20000-30000) to avoid conflicts');
|
|
logger.log('info', ' • Project-specific containers for multi-project support');
|
|
logger.log('info', ' • Preserves custom configuration values');
|
|
logger.log('info', ' • MongoDB Compass connection support');
|
|
console.log();
|
|
|
|
logger.log('note', 'Examples:');
|
|
logger.log('info', ' gitzone services start # Start all services');
|
|
logger.log('info', ' gitzone services start mongo # Start only MongoDB');
|
|
logger.log('info', ' gitzone services stop # Stop all services');
|
|
logger.log('info', ' gitzone services status # Check service status');
|
|
logger.log('info', ' gitzone services config # Show configuration');
|
|
logger.log('info', ' gitzone services compass # Get MongoDB Compass connection');
|
|
logger.log('info', ' gitzone services logs mongo 50 # Show last 50 lines of MongoDB logs');
|
|
} |