-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·100 lines (79 loc) · 2.9 KB
/
cli.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
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
#!/usr/bin/env node
const fs = require('fs');
const os = require('os');
const path = require('path');
const meow = require('meow');
const updateNotifier = require('update-notifier');
const chalk = require('chalk');
const { prompt } = require('inquirer');
const parseCredentials = require('./lib/parseCredentials');
const generateCredentials = require('./lib/generateCredentials');
const cli = meow(`
Usage
$ awsprofiles <profile>
Options
--credentials, -c Path to your credentials file (default: ~/.aws/credentials)
Examples
$ awsprofiles
$ awsprofiles prod
$ awsprofiles dev
`, {
alias: { c: 'credentials' },
default: { credentials: path.join(os.homedir(), '.aws', 'credentials') },
});
updateNotifier({ pkg: cli.pkg }).notify();
const credentialsFile = cli.flags.credentials;
Promise.resolve()
.then(() => {
const credentials = parseCredentials(fs.readFileSync(credentialsFile, 'utf8'));
if (!credentials.default) {
throw new Error('No default profile is set');
}
const currentDefaultProfile = Object.keys(credentials).find(profile => profile !== 'default' && credentials[profile].aws_access_key_id === credentials.default.aws_access_key_id);
if (!currentDefaultProfile) {
throw new Error('Current default profile is not named in profile list, please check README.md for valid configuration');
}
const profiles = Object.keys(credentials).filter(profile => profile !== 'default' && profile !== currentDefaultProfile);
if (cli.input.length > 0) {
const input = cli.input[0];
if (input !== currentDefaultProfile && !profiles.includes(input)) {
throw new Error(`Profile ${chalk.magenta(input)} does not exist in ${chalk.magenta(credentialsFile)}`);
}
return {
profile: input,
credentials,
};
}
console.log('');
console.log(`Current AWS profile ${chalk.magenta(currentDefaultProfile)}`);
console.log('');
const choices = [{
name: `${currentDefaultProfile} - (current default)`,
value: currentDefaultProfile,
}].concat(profiles);
const prompts = [
{
type: 'list',
name: 'profile',
message: 'Select AWS profile',
choices,
validate: input => profiles.includes(input) || `Profile ${input} does not exist in ${chalk.magenta(credentialsFile)}`,
},
];
return prompt(prompts).then(({ profile }) => ({ profile, credentials }));
})
.then(({ profile, credentials }) => {
const newCredentials = Object.assign({}, credentials, {
default: credentials[profile],
});
fs.writeFileSync(credentialsFile, generateCredentials(newCredentials), 'utf8');
return profile;
})
.then(profile => {
console.log('');
console.log(`🔑 Changed default profile to ${chalk.magenta(profile)} in ${chalk.magenta(credentialsFile)}`);
})
.catch(err => {
console.log('');
console.log(`❌ ${err}`);
});