/
create.ts
106 lines (92 loc) · 3.64 KB
/
create.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
106
import { CliUx, Flags } from '@oclif/core';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { Slugs } from '../../';
import AccountUtils from '../../architect/account/account.utils';
import ClusterUtils from '../../architect/cluster/cluster.utils';
import BaseCommand from '../../base-command';
import { booleanString } from '../../common/utils/oclif';
interface CreateEnvironmentDto {
name: string;
description?: string;
cluster_id: string;
ttl?: string;
}
export default class EnvironmentCreate extends BaseCommand {
static aliases = ['environment:create', 'envs:create', 'env:create'];
static description = 'Register a new environment with Architect Cloud';
static examples = [
'environment:create --account=myaccount myenvironment',
'environment:create --account=myaccount --ttl=5days --description="My new temporary Architect environment" myenvironment',
];
static flags = {
...BaseCommand.flags,
...AccountUtils.flags,
...ClusterUtils.flags,
description: Flags.string({
description: 'Environment Description',
sensitive: false,
}),
strict: booleanString({
description: 'If set to true, throws an error when attempting to create an environment that already exists',
hidden: true,
default: false,
sensitive: false,
}),
ttl: Flags.string({
description: 'The TTL of the environment in a duration of time, ex. 30d, 12h, or 30m',
sensitive: false,
}),
};
static args = [{
sensitive: false,
name: 'environment',
description: 'Name to give the environment',
parse: async (value: string): Promise<string> => value.toLowerCase(),
}];
async run(): Promise<void> {
const { args, flags } = await this.parse(EnvironmentCreate);
const answers = await inquirer.prompt([
{
type: 'input',
name: 'environment',
message: 'What would you like to name your new environment?',
when: !args.environment,
filter: value => value.toLowerCase(),
validate: value => {
if (Slugs.ArchitectSlugValidator.test(value)) return true;
return `environment ${Slugs.ArchitectSlugDescription}`;
},
},
]);
const environment_name = args.environment || answers.environment;
if (!Slugs.ArchitectSlugValidator.test(environment_name)) {
throw new Error(`environment ${Slugs.ArchitectSlugDescription}`);
}
const account = await AccountUtils.getAccount(this.app, flags.account, { account_message: 'Select an account to register the environment with' });
const cluster = await ClusterUtils.getCluster(this.app.api, account, flags.cluster || flags.platform);
CliUx.ux.action.start(chalk.blue('Registering environment with Architect'));
const dto: CreateEnvironmentDto = {
name: environment_name,
description: flags.description,
cluster_id: cluster.id,
};
if (flags.ttl) {
dto.ttl = flags.ttl;
}
let _environment_already_exists = false;
await this.app.api.post(`/accounts/${account.id}/environments`, dto, {
validateStatus: function (status): boolean {
_environment_already_exists = status === 409;
return status === 201 || (_environment_already_exists && flags.strict === false);
},
});
const environment_url = `${this.app.config.app_host}/${account.name}/environments/${environment_name}`;
CliUx.ux.action.stop();
if (_environment_already_exists) {
this.warn(`Unable to create new environment '${environment_name}'.\nEnvironment name already in use for account '${account.name}'`);
return;
}
this.log(chalk.green(`Environment created: ${environment_url}`));
}
}