-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathswitch-profile.js
63 lines (53 loc) · 1.71 KB
/
switch-profile.js
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
const _ = require("lodash");
const { Command } = require("@oclif/command");
const { getProfiles, replaceProfiles } = require("../lib/aws-profile-utils");
const inquirer = require("inquirer");
const { track } = require("../lib/analytics");
class SwitchProfileCommand extends Command {
async run() {
track("switch-profile", {});
const { sharedCred, config } = getProfiles();
const sharedCredProfileNames = Object.keys(sharedCred).filter(
x => x !== "default"
);
const configProfileNames = Object.keys(config);
if (_.isEmpty(sharedCredProfileNames) && _.isEmpty(configProfileNames)) {
this.log("You don't have any named profiles set up");
this.log(
"Run 'aws configure --profile profile-name' to set up named profiles"
);
this.exit();
}
const profileChoices = _.uniq([
...sharedCredProfileNames,
...configProfileNames
]).map(name =>
_.isEqual(sharedCred[name] || config[name], sharedCred.default)
? `${name} (current default profile)`
: name
);
const { accountToSwitchTo } = await inquirer.prompt([
{
type: "list",
name: "accountToSwitchTo",
message: "Which profile do you want to switch to?",
choices: profileChoices
}
]);
if (accountToSwitchTo) {
const profileName = accountToSwitchTo.replace(
" (current default profile)",
""
);
if (accountToSwitchTo.endsWith("(current default profile)")) {
this.log(`Stay logged in as [${profileName}]`);
} else {
sharedCred.default = sharedCred[profileName] || config[profileName];
replaceProfiles({ sharedCred, config });
this.log(`You are now logged in as [${profileName}]`);
}
}
}
}
SwitchProfileCommand.description = "Switch AWS profiles";
module.exports = SwitchProfileCommand;