|
| 1 | +import {flags} from '@oclif/command' |
| 2 | +import cli from 'cli-ux' |
| 3 | +import {APIClientService} from '../share/api/api.service' |
| 4 | +import {AbstractCommand} from '../share/abstract.command' |
| 5 | + |
| 6 | +export default class Delete extends AbstractCommand { |
| 7 | + static description = 'Domain delete command' |
| 8 | + |
| 9 | + static examples = [ |
| 10 | + 'Simply usage', |
| 11 | + '$ shifter-domain delete --username USERNAME --password PASSWORD --site-id xxx-YOUR-SITE-ID-xxxx --domain test.example.com', |
| 12 | + ] |
| 13 | + |
| 14 | + static flags = { |
| 15 | + version: flags.version({char: 'v'}), |
| 16 | + help: flags.help({char: 'h'}), |
| 17 | + development: flags.boolean({ |
| 18 | + description: 'Work as development mode (Only for Shifter developer team)', |
| 19 | + default: false, |
| 20 | + }), |
| 21 | + verbose: flags.boolean({ |
| 22 | + description: 'Show verbose', |
| 23 | + default: false, |
| 24 | + }), |
| 25 | + username: flags.string({ |
| 26 | + char: 'U', |
| 27 | + description: 'Shifter username', |
| 28 | + }), |
| 29 | + password: flags.string({ |
| 30 | + hidden: true, |
| 31 | + char: 'P', |
| 32 | + description: 'Shifter password', |
| 33 | + }), |
| 34 | + domain: flags.string({ |
| 35 | + char: 'D', |
| 36 | + description: 'Target domain name (eg. www.example.com)', |
| 37 | + }), |
| 38 | + 'site-id': flags.string({ |
| 39 | + char: 'S', |
| 40 | + description: 'Shifter site id', |
| 41 | + }), |
| 42 | + } |
| 43 | + |
| 44 | + async run() { |
| 45 | + const {flags} = this.parse(Delete) |
| 46 | + const siteId = flags['site-id'] || await cli.prompt('Site id') |
| 47 | + const domain = flags.domain || await cli.prompt('Target domain') |
| 48 | + const development = flags.development === true |
| 49 | + if (development) this.log('Work as development mode') |
| 50 | + try { |
| 51 | + const clientWithAuth = await this.setupApiClient(flags.username, flags.password, flags.verbose, development) |
| 52 | + const site = await clientWithAuth.get(`/latest/sites/${siteId}`) |
| 53 | + if (!site || site.project_id !== siteId) throw new Error(`No such site ${siteId}`) |
| 54 | + const domainObj = await clientWithAuth.get(`/latest/sites/${siteId}/domains/${domain}`) |
| 55 | + if (!domainObj) throw new Error(`No such domain ${domain}`) |
| 56 | + await clientWithAuth.delete(`/latest/sites/${siteId}/domains/${domain}`) |
| 57 | + this.log('Domain has been deleted') |
| 58 | + } catch (error) { |
| 59 | + if (APIClientService.isAxiosError(error) && error.response) { |
| 60 | + const response = error.response |
| 61 | + // eslint-disable-next-line no-console |
| 62 | + if (development) console.log(response) |
| 63 | + this.error(`${response.status} - ${response.statusText}\n${response.data.message}`) |
| 64 | + } |
| 65 | + // eslint-disable-next-line no-console |
| 66 | + if (development) console.log(error) |
| 67 | + this.error(error) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments