Skip to content

Commit 0b60eaf

Browse files
committed
Feature: Default to only available profile/account/role
1 parent 601f0bc commit 0b60eaf

File tree

4 files changed

+59
-42
lines changed

4 files changed

+59
-42
lines changed

lib/accounts.js

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Fuse from "fuse.js";
2+
import chalk from "chalk";
23
import inquirer from "inquirer";
34
import autocomplete from "inquirer-autocomplete-prompt";
45
import { buildError } from "./error.js";
@@ -9,40 +10,50 @@ prompt.registerPrompt("autocomplete", autocomplete);
910

1011
export const chooseAccount = async (accessToken, region) => {
1112
const accounts = await getAccounts(accessToken, createAWSClient(region));
12-
const fuse = new Fuse(accounts, { threshold: 0.4, keys: ["accountName"] });
13-
const answer = await prompt({
14-
type: "autocomplete",
15-
name: "account",
16-
message: "Select the AWS account you want to authenticate with:",
17-
pageSize: 20,
18-
source: (result, input) => {
19-
return input
20-
? fuse
21-
.search(input)
22-
.map((result) => `${result.item.accountName} (${result.item.accountId})`)
23-
: accounts.map((account) => `${account.accountName} (${account.accountId})`);
24-
},
25-
});
26-
return accounts.find(
27-
(account) => `${account.accountName} (${account.accountId})` === answer.account
28-
);
13+
if (accounts.length === 1) {
14+
console.error(chalk.green("Defaulting to only available account:", accounts[0].accountName));
15+
return accounts[0];
16+
} else {
17+
const fuse = new Fuse(accounts, { threshold: 0.4, keys: ["accountName"] });
18+
const answer = await prompt({
19+
type: "autocomplete",
20+
name: "account",
21+
message: "Select the AWS account you want to authenticate with:",
22+
pageSize: 20,
23+
source: (result, input) => {
24+
return input
25+
? fuse
26+
.search(input)
27+
.map((result) => `${result.item.accountName} (${result.item.accountId})`)
28+
: accounts.map((account) => `${account.accountName} (${account.accountId})`);
29+
},
30+
});
31+
return accounts.find(
32+
(account) => `${account.accountName} (${account.accountId})` === answer.account
33+
);
34+
}
2935
};
3036

3137
export const chooseRole = async (accessToken, accountId, region) => {
3238
const roles = await getAccountRoles(accessToken, accountId, createAWSClient(region));
33-
const fuse = new Fuse(roles, { threshold: 0.4, keys: ["roleName"] });
34-
const answer = await prompt({
35-
type: "autocomplete",
36-
name: "roleName",
37-
message: "Select the account role you wish to assume:",
38-
pageSize: 20,
39-
source: (result, input) => {
40-
return input
41-
? fuse.search(input).map((result) => result.item.roleName)
42-
: roles.map((role) => role.roleName);
43-
},
44-
});
45-
return roles.find((role) => role.roleName === answer.roleName);
39+
if (roles.length === 1) {
40+
console.error(chalk.green("Defaulting to only available role:", roles[0].roleName));
41+
return roles[0];
42+
} else {
43+
const fuse = new Fuse(roles, { threshold: 0.4, keys: ["roleName"] });
44+
const answer = await prompt({
45+
type: "autocomplete",
46+
name: "roleName",
47+
message: "Select the account role you wish to assume:",
48+
pageSize: 20,
49+
source: (result, input) => {
50+
return input
51+
? fuse.search(input).map((result) => result.item.roleName)
52+
: roles.map((role) => role.roleName);
53+
},
54+
});
55+
return roles.find((role) => role.roleName === answer.roleName);
56+
}
4657
};
4758

4859
export const findAccountByName = async (accessToken, accountName, region) => {

lib/profiles.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inquirer from "inquirer";
2+
import chalk from "chalk";
23
import autocomplete from "inquirer-autocomplete-prompt";
34
import { buildError } from "./error.js";
45

@@ -69,7 +70,7 @@ export const deleteProfile = (configstore) =>
6970
prompt({
7071
type: "list",
7172
name: "profile",
72-
message: "Select the profile you with to remove.",
73+
message: "Select the profile you wish to remove.",
7374
choices: Object.keys(configstore.all),
7475
}).then(({ profile }) => {
7576
console.error(profile);
@@ -82,16 +83,21 @@ export const deleteProfile = (configstore) =>
8283
export const listProfiles = (configstore) => Object.keys(configstore.all);
8384

8485
export const chooseProfile = async (configstore) => {
85-
if (!configstore || !Object.keys(configstore.all).length) {
86+
const profileNames = Object.keys(configstore.all);
87+
if (!configstore || !profileNames.length) {
8688
throw buildError("ERR_NO_PROFILE");
89+
} else if (profileNames.length === 1) {
90+
console.error(chalk.green("Defaulting to only available SSO profile:", profileNames[0]));
91+
return profileNames[0];
92+
} else {
93+
const answer = await prompt({
94+
type: "list",
95+
name: "profile",
96+
message: "Select the SSO profile.",
97+
choices: profileNames,
98+
});
99+
return answer.profile;
87100
}
88-
const answer = await prompt({
89-
type: "list",
90-
name: "profile",
91-
message: "Select the SSO profile.",
92-
choices: Object.keys(configstore.all),
93-
});
94-
return answer.profile;
95101
};
96102

97103
export const loadConfig = (configstore, profileName) => {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-sso-cli",
3-
"version": "0.5.3",
3+
"version": "0.6.0",
44
"description": "A tool for easily switching between different AWS SSO accounts.",
55
"main": "index.js",
66
"author": "eeno",

0 commit comments

Comments
 (0)