|
| 1 | +import { Args, Command, Flags } from '@oclif/core'; |
| 2 | +import { ConfigService } from '../services/config.service'; |
| 3 | +import { CLIUtils } from '../utils/cli.utils'; |
| 4 | +import { ErrorUtils } from '../utils/errors.utils'; |
| 5 | +import { NotValidPortError } from '../types/command.types'; |
| 6 | +import { ValidationService } from '../services/validation.service'; |
| 7 | + |
| 8 | +export default class WebDAVConfig extends Command { |
| 9 | + static readonly description = 'Edit the configuration of the Internxt CLI WebDav server as the port or the protocol.'; |
| 10 | + static readonly args = { |
| 11 | + action: Args.string({ |
| 12 | + required: true, |
| 13 | + options: ['set-http', 'set-https', 'change-port'], |
| 14 | + }), |
| 15 | + }; |
| 16 | + static readonly examples = [ |
| 17 | + '<%= config.bin %> <%= command.id %> set-http', |
| 18 | + '<%= config.bin %> <%= command.id %> set-https', |
| 19 | + '<%= config.bin %> <%= command.id %> change-port', |
| 20 | + ]; |
| 21 | + static readonly flags = { |
| 22 | + ...CLIUtils.CommonFlags, |
| 23 | + port: Flags.string({ |
| 24 | + char: 'p', |
| 25 | + description: 'The new port that the WebDAV server is going to be have.', |
| 26 | + required: false, |
| 27 | + }), |
| 28 | + }; |
| 29 | + |
| 30 | + public async run(): Promise<void> { |
| 31 | + const { args, flags } = await this.parse(WebDAVConfig); |
| 32 | + const nonInteractive = flags['non-interactive']; |
| 33 | + const webdavConfig = await ConfigService.instance.readWebdavConfig(); |
| 34 | + |
| 35 | + if (args.action !== 'change-port' && flags['port']) { |
| 36 | + CLIUtils.warning('The port flag will be ignored; it can only be used with the "change-port" action.'); |
| 37 | + } |
| 38 | + |
| 39 | + switch (args.action) { |
| 40 | + case 'set-http': { |
| 41 | + await ConfigService.instance.saveWebdavConfig({ |
| 42 | + ...webdavConfig, |
| 43 | + protocol: 'http', |
| 44 | + }); |
| 45 | + CLIUtils.success('On the next start, the WebDAV server will use HTTP.'); |
| 46 | + break; |
| 47 | + } |
| 48 | + |
| 49 | + case 'set-https': { |
| 50 | + await ConfigService.instance.saveWebdavConfig({ |
| 51 | + ...webdavConfig, |
| 52 | + protocol: 'https', |
| 53 | + }); |
| 54 | + CLIUtils.success('On the next start, the WebDAV server will use HTTPS.'); |
| 55 | + break; |
| 56 | + } |
| 57 | + |
| 58 | + case 'change-port': { |
| 59 | + const newPort = await this.getWebDAVPort(flags['port'], nonInteractive); |
| 60 | + await ConfigService.instance.saveWebdavConfig({ |
| 61 | + ...webdavConfig, |
| 62 | + port: newPort, |
| 63 | + }); |
| 64 | + CLIUtils.success('For the next start, the Webdav server will be served at the new port: ' + newPort); |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + async catch(error: Error) { |
| 71 | + ErrorUtils.report(error, { command: this.id }); |
| 72 | + CLIUtils.error(error.message); |
| 73 | + this.exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + private static readonly MAX_ATTEMPTS = 3; |
| 77 | + |
| 78 | + public getWebDAVPort = async (webdavPortFlag: string | undefined, nonInteractive: boolean): Promise<string> => { |
| 79 | + let port = CLIUtils.getValueFromFlag( |
| 80 | + { |
| 81 | + value: webdavPortFlag, |
| 82 | + name: WebDAVConfig.flags['port'].name, |
| 83 | + error: new NotValidPortError(), |
| 84 | + canBeEmpty: true, |
| 85 | + }, |
| 86 | + nonInteractive, |
| 87 | + (port: string) => ValidationService.instance.validateTCPIntegerPort(port), |
| 88 | + ); |
| 89 | + if (!port) { |
| 90 | + port = (await this.getNewWebDAVPortInteractively()).trim(); |
| 91 | + } |
| 92 | + return port; |
| 93 | + }; |
| 94 | + |
| 95 | + public getNewWebDAVPortInteractively = (): Promise<string> => { |
| 96 | + return CLIUtils.promptWithAttempts( |
| 97 | + { |
| 98 | + message: 'What is the new WebDAV server port?', |
| 99 | + options: { required: false }, |
| 100 | + error: new NotValidPortError(), |
| 101 | + }, |
| 102 | + WebDAVConfig.MAX_ATTEMPTS, |
| 103 | + (port: string) => ValidationService.instance.validateTCPIntegerPort(port), |
| 104 | + ); |
| 105 | + }; |
| 106 | +} |
0 commit comments