/
destroy.ts
90 lines (79 loc) · 3.05 KB
/
destroy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { CliUx, Flags, Interfaces } from '@oclif/core';
import chalk from 'chalk';
import inquirer from 'inquirer';
import AccountUtils from '../../architect/account/account.utils';
import { EnvironmentUtils } from '../../architect/environment/environment.utils';
import BaseCommand from '../../base-command';
export default class EnvironmentDestroy extends BaseCommand {
static aliases = ['environment:destroy', 'envs:destroy', 'env:destroy', 'env:deregister', 'environment:deregister'];
static description = 'Deregister an environment';
static flags = {
...BaseCommand.flags,
...AccountUtils.flags,
auto_approve: {
non_sensitive: true,
...Flags.boolean({
description: `${BaseCommand.DEPRECATED} Please use --auto-approve.`,
hidden: true,
}),
},
['auto-approve']: {
non_sensitive: true,
...Flags.boolean({
description: 'Automatically apply the changes',
default: false,
}),
},
force: {
non_sensitive: true,
...Flags.boolean({
description: 'Force the deletion even if the environment is not empty',
char: 'f',
default: false,
}),
},
};
static args = [{
non_sensitive: true,
name: 'environment',
description: 'Name of the environment to deregister',
parse: async (value: string): Promise<string> => value.toLowerCase(),
}];
async parse<F, A extends {
[name: string]: any;
}>(options?: Interfaces.Input<F>, argv = this.argv): Promise<Interfaces.ParserOutput<F, A>> {
const parsed = await super.parse(options, argv) as Interfaces.ParserOutput<F, A>;
const flags: any = parsed.flags;
// Merge any values set via deprecated flags into their supported counterparts
flags['auto-approve'] = flags.auto_approve ? flags.auto_approve : flags['auto-approve'];
parsed.flags = flags;
return parsed;
}
async run(): Promise<void> {
const { args, flags } = await this.parse(EnvironmentDestroy);
const account = await AccountUtils.getAccount(this.app, flags.account);
const environment = await EnvironmentUtils.getEnvironment(this.app.api, account, args.environment);
let answers = await inquirer.prompt([{
type: 'input',
name: 'destroy',
message: 'Are you absolutely sure? This will deregister the environment.\nPlease type in the name of the environment to confirm.\n',
validate: (value: any, answers: any) => {
if (value === environment.name) {
return true;
}
return `Name must match: ${chalk.blue(environment.name)}`;
},
when: !flags['auto-approve'],
}]);
CliUx.ux.action.start(chalk.blue('Deregistering environment'));
answers = { ...args, ...flags, ...answers };
const { data: account_environment } = await this.app.api.get(`/accounts/${account.id}/environments/${environment.name}`);
await this.app.api.delete(`/environments/${account_environment.id}`, {
params: {
force: answers.force ? 1 : 0,
},
});
CliUx.ux.action.stop();
this.log(chalk.green('Environment deregistered'));
}
}