import { Command } from 'commander'; import axios from 'axios'; import * as readline from 'readline'; import { ConfigManager } from './config'; import { DEFAULT_SERVER_URL } from '../constants'; import { Logger } from './logger'; let cachedConfigManager: ConfigManager | undefined; const configManager = (): ConfigManager => cachedConfigManager ??= new ConfigManager(); // Create readline interface for user input function createReadlineInterface() { return readline.createInterface({ input: process.stdin, output: process.stdout }); } function question(rl: readline.Interface, query: string): Promise { return new Promise((resolve) => { rl.question(query, resolve); }); } // Init command export function setupInitCommand(program: Command) { program .command('init') .description('Initialize DotShare CLI configuration') .action(async () => { Logger.info('šŸš€ Welcome to DotShare CLI Setup!\n'); const rl = createReadlineInterface(); try { // Get server URL const currentConfig = configManager().getConfig(); // Use the constant here instead of manually writing the URL const defaultServerUrl = currentConfig.serverUrl || DEFAULT_SERVER_URL; const serverUrl = await question(rl, `Server URL [${defaultServerUrl}]: `); const finalServerUrl = serverUrl.trim() || defaultServerUrl; configManager().setServerUrl(finalServerUrl); Logger.info(`āœ… Server URL set to: ${finalServerUrl}\n`); // Test server connection Logger.info('šŸ” Testing server connection...'); try { const response = await axios.get(`${finalServerUrl}/`, { timeout: 5000 }); if (response.status === 200) { Logger.info('āœ… Server connection successful!\n'); } } catch { Logger.info('āš ļø Server connection failed. Check the URL and your internet connection.\n'); } Logger.info('šŸŽ‰ DotShare CLI initialized successfully!'); Logger.info('Next steps:'); Logger.info(' • Run "dotshare login telegram" to authenticate Telegram'); Logger.info(' • Run "dotshare login linkedin" to authenticate LinkedIn'); Logger.info(' • Run "dotshare whoami" to check your setup'); Logger.info(' • Run "dotshare \'Hello World!\'" to post to all platforms'); } catch (error) { Logger.error('āŒ Error during initialization:', error); } finally { rl.close(); } }); } // Login commands export function setupLoginCommand(program: Command) { program.command('login ').description('Use named local accounts instead of legacy relay credentials') .action(() => { Logger.info('Use dotshare accounts add --platform . Its terminal prompts hide credentials and save them only to the required OS keyring.'); process.exitCode = 1; }); } // Whoami command export function setupWhoamiCommand(program: Command) { program .command('whoami') .description('Show current configuration and status') .action(() => { const config = configManager().getConfig(); const platforms = configManager().getConfiguredPlatforms(); Logger.info('šŸ” DotShare CLI Status\n'); Logger.info(`šŸ“” Server URL: ${config.serverUrl}`); Logger.info('\nšŸ” Authenticated Platforms:'); if (platforms.length === 0) { Logger.info(' None - Run "dotshare login " to authenticate'); } else { platforms.forEach(platform => { Logger.info(` āœ… ${platform.charAt(0).toUpperCase() + platform.slice(1)}`); }); } Logger.info('\nšŸ“Š Configuration:'); Logger.info(` Config file: ~/.dotshare/config.json`); if (platforms.length > 0) { Logger.info('\nšŸš€ Ready to post! Try:'); Logger.info(' dotshare accounts list'); Logger.info(' dotshare drafts list'); } else { Logger.info('\nāš ļø Setup incomplete. Run "dotshare init" to get started.'); } }); } // Default posting command export function setupDefaultCommand(program: Command) { program.arguments('[message]').option('--media ').option('--platforms ') .description('Legacy publishing is disabled; use saved drafts or publish with named local accounts') .action(() => { Logger.info('Legacy publishing forwarded platform credentials to a relay and is disabled. Use dotshare-agent accounts add, then drafts preview/publish or publish --input with named local accounts.'); process.exitCode = 1; }); } export { setupAgentCommands } from '../agent/commands';