UNPKG

@liara/cli

Version:

The command line interface for Liara

90 lines (89 loc) 3.18 kB
import inquirer from 'inquirer'; import Command from '../../base.js'; import { Flags } from '@oclif/core'; import { createDebugLogger } from '../../utils/output.js'; import ora from 'ora'; import { OBJECT_STORAGE_API_URL_DEV, OBJECT_STORAGE_API_URL, DEV_MODE, } from '../../constants.js'; class BucketDelete extends Command { async setGotConfig(config) { await super.setGotConfig(config); this.got = this.got.extend({ prefixUrl: DEV_MODE ? OBJECT_STORAGE_API_URL_DEV : OBJECT_STORAGE_API_URL, }); } async run() { const { flags } = await this.parse(BucketDelete); const debug = createDebugLogger(flags.debug); await this.setGotConfig(flags); const bucket = flags.bucket || (await this.promptBuckets()); try { if (flags.force) { await this.got.delete(`api/v1/buckets/${bucket}`); this.log(`Bucket ${bucket} deleted.`); } else if (await this.confirm(bucket)) { await this.got.delete(`api/v1/buckets/${bucket}`); this.log(`Bucket ${bucket} deleted.`); } } catch (error) { debug(error.message); if (error.response && error.response.data) { debug(JSON.stringify(error.response.data)); } if (error.response && error.response.status === 404) { this.error(`Could not find the bucket.`); } if (error.response && error.response.status === 409) { this.error(`Another operation is already running. Please wait.`); } this.error(`Could not delete the bucket. Please try again.`); } } async promptBuckets() { this.spinner = ora(); this.spinner.start('Loading...'); try { const { buckets } = await this.got('api/v1/buckets').json(); this.spinner.stop(); if (!buckets.length) { this.warn('Please go to https://console.liara.ir/buckets and create an bucket, first.'); this.exit(1); } const { bucket } = (await inquirer.prompt({ name: 'bucket', type: 'list', message: 'Please select a bucket:', choices: [...buckets.map((bucket) => bucket.name)], })); return bucket; } catch (error) { this.spinner.stop(); throw error; } } async confirm(bucket) { const { confirmation } = (await inquirer.prompt({ name: 'confirmation', type: 'confirm', message: `Are you sure you want to delete "${bucket}"?`, default: false, })); return confirmation; } } BucketDelete.description = 'delete a bucket'; BucketDelete.flags = { ...Command.flags, bucket: Flags.string({ char: 'b', description: 'bucket name', }), force: Flags.boolean({ char: 'f', description: 'force the deletion', }), }; BucketDelete.aliases = ['bucket:delete']; export default BucketDelete;