-
Notifications
You must be signed in to change notification settings - Fork 12
/
create.ts
123 lines (102 loc) · 3.05 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import ora, { Ora } from 'ora';
import inquirer from 'inquirer';
import Command, { IGetProjectsResponse } from '../../base.js';
import { Flags } from '@oclif/core';
import { createDebugLogger } from '../../utils/output.js';
export default class DiskCreate extends Command {
static description = 'create a disk';
static flags = {
...Command.flags,
app: Flags.string({
char: 'a',
description: 'app id',
}),
name: Flags.string({
char: 'n',
description: 'disk name',
}),
size: Flags.string({
char: 's',
description: 'disk size',
}),
};
spinner!: Ora;
async run() {
this.spinner = ora();
const { flags } = await this.parse(DiskCreate);
const debug = createDebugLogger(flags.debug);
await this.setGotConfig(flags);
const app = flags.app || (await this.promptProject());
const name = flags.name || (await this.promptDiskName());
const size = flags.size || (await this.promptDiskSize());
try {
await this.got.post(`v1/projects/${app}/disks`, { json: { name, size } });
this.log(`Disk ${name} created.`);
} catch (error) {
debug(error.message);
if (error.response && error.response.data) {
debug(JSON.stringify(error.response.data));
}
if (
error.response &&
error.response.status === 400 &&
error.response.data.message === 'not_enough_storage_space'
) {
this.error(`Not enough storage space.`);
}
if (
error.response &&
error.response.status === 400 &&
error.response.data.message.includes('["size" must be a number]')
) {
this.error('Invalid disk size.');
}
if (error.response && error.response.status === 400) {
this.error(`Invalid disk name.`);
}
this.error(`Could not create the disk. Please try again.`);
}
}
async promptProject(): Promise<string> {
this.spinner.start('Loading...');
try {
const { projects } = await this.got(
'v1/projects'
).json<IGetProjectsResponse>();
this.spinner.stop();
if (projects.length === 0) {
this.warn(
"Please create an app via 'liara app:create' command, first."
);
this.exit(1);
}
const { project } = (await inquirer.prompt({
name: 'project',
type: 'list',
message: 'Please select an app:',
choices: [...projects.map((project) => project.project_id)],
})) as { project: string };
return project;
} catch (error) {
this.spinner.stop();
throw error;
}
}
async promptDiskName(): Promise<string> {
const { name } = (await inquirer.prompt({
name: 'name',
type: 'input',
message: 'Enter a disk name:',
validate: (input) => input.length > 2,
})) as { name: string };
return name;
}
async promptDiskSize(): Promise<number> {
const { size } = (await inquirer.prompt({
name: 'size',
type: 'input',
message: 'Enter a disk size in GB:',
})) as { size: number };
return size;
}
}