/
create.ts
123 lines (103 loc) · 2.96 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 from "ora";
import axios from "axios";
import inquirer from "inquirer";
import Command, { IGetProjectsResponse } from "../../base";
import { flags } from "@oclif/command";
import { createDebugLogger } from "../../utils/output";
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.Ora;
async run() {
this.spinner = ora();
const { flags } = this.parse(DiskCreate);
const debug = createDebugLogger(flags.debug);
this.setAxiosConfig({
...this.readGlobalConfig(),
...flags,
});
const app = flags.app || (await this.promptProject());
const name = flags.name || (await this.promptDiskName());
const size = flags.size || (await this.promptDiskSize());
try {
await axios.post(
`/v1/projects/${app}/disks`,
{
name,
size,
},
this.axiosConfig
);
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) {
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 {
data: { projects },
} = await axios.get<IGetProjectsResponse>(
"/v1/projects",
this.axiosConfig
);
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;
}
}