|
| 1 | +import { flags, configHandler, FlagInput, log, handleAndLogError, cliux } from '@contentstack/cli-utilities'; |
| 2 | +import { askProxyPassword } from '../../../utils/interactive'; |
| 3 | +import { BaseCommand } from '../../../base-command'; |
| 4 | + |
| 5 | +export default class ProxySetCommand extends BaseCommand<typeof ProxySetCommand> { |
| 6 | + static description = 'Set proxy configuration for CLI'; |
| 7 | + |
| 8 | + static flags: FlagInput = { |
| 9 | + host: flags.string({ |
| 10 | + description: 'Proxy host address', |
| 11 | + required: true, |
| 12 | + }), |
| 13 | + port: flags.string({ |
| 14 | + description: 'Proxy port number', |
| 15 | + required: true, |
| 16 | + }), |
| 17 | + protocol: flags.string({ |
| 18 | + description: 'Proxy protocol (http or https)', |
| 19 | + options: ['http', 'https'], |
| 20 | + default: 'http', |
| 21 | + required: true, |
| 22 | + }), |
| 23 | + username: flags.string({ |
| 24 | + description: 'Proxy username (optional)', |
| 25 | + }), |
| 26 | + }; |
| 27 | + |
| 28 | + static examples = [ |
| 29 | + 'csdx config:set:proxy --host 127.0.0.1 --port 3128', |
| 30 | + 'csdx config:set:proxy --host proxy.example.com --port 8080 --protocol https', |
| 31 | + 'csdx config:set:proxy --host proxy.example.com --port 8080 --username user', |
| 32 | + ]; |
| 33 | + |
| 34 | + async run() { |
| 35 | + try { |
| 36 | + log.debug('Starting proxy configuration setup', this.contextDetails); |
| 37 | + const { flags } = await this.parse(ProxySetCommand); |
| 38 | + |
| 39 | + log.debug('Parsed proxy configuration flags', this.contextDetails); |
| 40 | + |
| 41 | + // Validate host - must not be empty or whitespace-only |
| 42 | + if (!flags.host || flags.host.trim() === '') { |
| 43 | + log.error('Invalid host provided - host cannot be empty or whitespace-only', this.contextDetails); |
| 44 | + cliux.error('Invalid host address. Host cannot be empty or contain only whitespace.'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const port = Number.parseInt(flags.port, 10); |
| 49 | + if (Number.isNaN(port) || port < 1 || port > 65535) { |
| 50 | + log.error('Invalid port number provided', this.contextDetails); |
| 51 | + cliux.error('Invalid port number. Port must be between 1 and 65535.'); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const proxyConfig: any = { |
| 56 | + protocol: flags.protocol || 'http', |
| 57 | + host: flags.host.trim(), |
| 58 | + port: port, |
| 59 | + }; |
| 60 | + |
| 61 | + if (flags.username) { |
| 62 | + log.debug('Username provided, prompting for password', this.contextDetails); |
| 63 | + // Prompt for password when username is provided |
| 64 | + const password = await askProxyPassword(); |
| 65 | + proxyConfig.auth = { |
| 66 | + username: flags.username, |
| 67 | + password: password || '', |
| 68 | + }; |
| 69 | + log.debug('Proxy authentication configured', this.contextDetails); |
| 70 | + } |
| 71 | + |
| 72 | + log.debug('Saving proxy configuration to global config', this.contextDetails); |
| 73 | + configHandler.set('proxy', proxyConfig); |
| 74 | + |
| 75 | + log.success('Proxy configuration set successfully', this.contextDetails); |
| 76 | + } catch (error) { |
| 77 | + handleAndLogError(error, { ...this.contextDetails, module: 'config-set-proxy' }); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
0 commit comments