-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·76 lines (69 loc) · 2.33 KB
/
Copy pathcli.js
File metadata and controls
executable file
·76 lines (69 loc) · 2.33 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
#!/usr/bin/env node --harmony
const fs = require('fs');
const path = require('path');
const commander = require('commander');
const prompt = require('prompt');
const chalk = require('chalk');
const Switcher = require('./switcher.js')();
const data = JSON.parse(fs.readFileSync(path.resolve(__dirname, './package.json'), 'utf8'));
commander
.usage('<command> [options]')
.version(data.version);
commander.command('list')
.alias('ls')
.description('Lists available AWS profiles from [~/.aws/credentials]')
.action(() => {
console.log(chalk.green('Available profiles:'));
Switcher.listProfiles()
.catch(err => console.error(err));
});
commander.command('current')
.alias('c')
.description('List the current default profile')
.action(() => {
Switcher.getCurrentProfile().then(name => {
return console.log(chalk.green(`Current profile: ${name}`));
})
.catch(err => console.error(err));
});
commander.command('switch')
.alias('sw')
.description('Switches the default profile to a different, user-specified profile')
.option('-p, --profile <optional>', 'The name of the profile to make the default profile')
.option('-i, --index <optional>', 'The index of the profile to make the default profile (from list command)')
.action(cmd => {
if (cmd.profile) {
console.log(chalk.green(`Switching default aws profile to ${cmd.profile}`));
Switcher.switchProfileByName(cmd.profile);
} else if (cmd.index) {
Switcher.getProfileNameByIndex(cmd.index).then(name => {
console.log(chalk.green(`Switching default aws profile to ${name}`));
return Switcher.switchProfileByName(name);
})
.catch(err => console.error(err));
} else {
Switcher.listProfiles()
.then(() => {
prompt.start();
prompt.get(['index'], (err, result) => {
if (err) {
throw err;
}
Switcher.getProfileNameByIndex(result.index).then(name => {
console.log(chalk.green(`Switching default aws profile to ${name}`));
return Switcher.switchProfileByName(name);
})
.catch(err => console.error(err));
});
});
}
});
commander.on('--help', () => {
console.log(' Examples:');
console.log('');
console.log(' $ switcher list');
console.log(' $ switcher switch -p profile_name');
console.log(' $ switcher switch -i 2');
console.log('');
});
commander.parse(process.argv);