/
configure.ts
60 lines (51 loc) · 1.7 KB
/
configure.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
import cli from 'cli-ux';
import { chmod, mkdirp, readJSON, writeFile } from 'fs-extra';
import { join } from 'path';
import BaseCommand, { FileConfig } from '../lib/base';
const UserRw = 0o600; // rw- --- ---
export default class Configure extends BaseCommand {
public static description = `Configure Vertex credentials.`;
public static examples = [
`$ vertex configure
Saved 'https://platform.vertexvis.com' configuration to '~/.config/@vertexvis/vertex-cli/config.json'.
`,
];
public static flags = { ...BaseCommand.flags };
public async run(): Promise<void> {
const basePath = this.parsedFlags?.basePath;
const client = this.userConfig?.client;
const id = await cli.prompt(`Vertex client ID`, {
default: client?.id,
required: false,
});
const secret = await cli.prompt(`Vertex client secret`, {
default: client?.secret,
required: false,
type: 'hide',
});
const configPath = join(this.config.configDir, 'config.json');
await mkdirp(this.config.configDir);
await writeFile(
configPath,
JSON.stringify(await this.buildConfig(basePath, configPath, id, secret)),
{ mode: UserRw }
);
// Update permissions for already existing config files
await chmod(configPath, UserRw);
this.log(`Saved '${basePath}' configuration to '${configPath}'.`);
}
private async buildConfig(
basePath: string,
configPath: string,
id: string,
secret: string
): Promise<FileConfig> {
try {
const fileConfig: FileConfig = await readJSON(configPath);
fileConfig[basePath] = { client: { id, secret } };
return fileConfig;
} catch {
return { [basePath]: { client: { id, secret } } };
}
}
}