-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
104 lines (87 loc) · 3.44 KB
/
cli.ts
File metadata and controls
104 lines (87 loc) · 3.44 KB
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
import * as readline from "readline";
import * as fs from "fs";
import { Cluster } from "./shared/cluster";
import { Logger } from "./shared/log";
import { Configuration } from "./shared/configuration";
export class CLI {
static async getArgument(names: (string | number)[], prompt?: string | [string, string, string, string]): Promise<string> {
return new Promise(done => {
for (let name of names) {
if (typeof name == "number") {
const args = [];
for (let i = 0; i < process.argv.length; i++) {
if (process.argv[i][0] == "-") {
if (process.argv[i + 1] && process.argv[i + 1][0] != "-") {
i++;
}
} else {
args.push(process.argv[i]);
}
}
if (args[name + 2]) {
return done(args[name + 2]);
}
} else if (typeof name == "string") {
const index = process.argv.indexOf(name);
if (index != -1) {
if (process.argv[index + 1]) {
// return null if the parameter has no value set
if (process.argv[index + 1][0] == "-") {
return done(null);
} else {
return done(process.argv[index + 1]);
}
} else {
return done(null);
}
}
}
}
if (prompt) {
const input = readline.createInterface(process.stdin, process.stdout);
if (Array.isArray(prompt)) {
input.question(`${prompt[0]} (${prompt[1]} = ${prompt[2]}}): `, res => {
input.close();
if (res == prompt[1]) {
done(prompt[3]);
} else {
done(res);
}
});
} else {
input.question(`${prompt}: `, res => {
input.close();
done(res)
});
}
} else {
done(null);
}
});
}
static async getClusterName(): Promise<string> {
if (process.argv.includes("-c") || process.argv.includes("--cluster")) {
const cluster = await this.getArgument(["-c", "--cluster"]);
this.setActiveCluster(cluster);
return cluster;
}
try {
const activeCluster = Configuration.activeCluster;
if (Configuration.clients.length > 1) {
const logger = new Logger("*");
logger.log("using cluster ", logger.c(activeCluster));
}
return activeCluster;
} catch {
const cluster = await this.getArgument(["-c", "--cluster"], "Cluster name");
this.setActiveCluster(cluster);
return cluster;
}
}
static setActiveCluster(cluster: string) {
if (cluster) {
Configuration.activeCluster = cluster;
Configuration.save();
}
}
}