-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
55 lines (42 loc) · 1.34 KB
/
cli.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
import { Select } from "./deps.ts";
const HOME = Deno.env.get("HOME");
const profileRegex = /\[profile .*]/g;
const bracketsRemovalRegx = /(\[profile )|(\])/g;
const defaultProfileChoice = "default";
const awsConfigFile = `${HOME}/.aws/config`;
const awsSwitchConfig = `${HOME}/.aws-switch.sh`;
// Check if the aws config file exists
try {
if (!Deno.statSync(awsConfigFile).isFile) {
console.error(`${awsConfigFile} does not exist`);
Deno.exit(1);
}
} catch {
console.error(`${awsConfigFile} does not exist`);
Deno.exit(1);
}
const awsConfig = Deno.readTextFileSync(awsConfigFile);
const matches = awsConfig.match(profileRegex);
if (!matches) {
console.log("No profiles found.");
console.log("Refer to this guide for help on setting up a new AWS profile:");
console.log(
"https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html"
);
Deno.exit(1);
}
const profiles = matches.map((match) => {
return match.replace(bracketsRemovalRegx, "");
});
if (!profiles.includes(defaultProfileChoice)) {
profiles.push(defaultProfileChoice);
}
const ret = await Select.prompt({
message: "Choose a profile",
options: profiles,
default: Deno.env.get("AWS_PROFILE") || defaultProfileChoice,
});
console.log(ret);
// Write to file
Deno.writeTextFileSync(awsSwitchConfig, `export AWS_PROFILE=${ret}`);
Deno.exit(0);