/
destroy.ts
105 lines (94 loc) · 3.82 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { CliUx, Interfaces } from '@oclif/core';
import chalk from 'chalk';
import inquirer from 'inquirer';
import AccountUtils from '../../architect/account/account.utils';
import { EnvironmentUtils, GetEnvironmentOptions } from '../../architect/environment/environment.utils';
import BaseCommand from '../../base-command';
import { booleanString } from '../../common/utils/oclif';
export default class EnvironmentDestroy extends BaseCommand {
static aliases = ['environment:destroy', 'envs:destroy', 'env:destroy', 'env:deregister', 'environment:deregister'];
static description = 'Deregister an environment';
static examples = [
'architect environment:destroy --account=myaccount myenvironment',
'architect environment:deregister --account=myaccount --auto-approve --force myenvironment',
];
static flags = {
...BaseCommand.flags,
...AccountUtils.flags,
auto_approve: booleanString({
description: `Please use --auto-approve.`,
hidden: true,
sensitive: false,
default: undefined,
deprecated: {
to: 'auto-approve',
},
}),
'auto-approve': booleanString({
description: 'Automatically apply the changes',
default: false,
sensitive: false,
}),
strict: booleanString({
description: 'If set to true, throws an error when an environment cannot be found to deregister',
hidden: true,
default: false,
sensitive: false,
}),
force: booleanString({
description: 'Force the deletion even if the environment is not empty',
char: 'f',
default: false,
sensitive: false,
}),
};
static args = [{
sensitive: false,
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, A>, 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 get_environment_options: GetEnvironmentOptions = { environment_name: args.environment, strict: flags.strict };
const environment = await EnvironmentUtils.getEnvironment(this.app.api, account, get_environment_options);
if (!environment.id && !flags.strict) {
this.warn(`No configured environments found matching ${args?.environment}.`);
return;
}
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, answers) => {
if (value === environment.name) {
return true;
}
return `Name must match: ${chalk.blue(environment.name)}`;
},
when: !flags['auto-approve'],
ciMessage: '--auto-approve flag is required in CI pipelines',
}]);
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'));
}
}