-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
95 lines (82 loc) · 2.3 KB
/
index.mjs
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
import restoreCursor from "restore-cursor";
import autocomplete from "./lib/autocomplete.mjs";
import { getArgs } from "./lib/cli.mjs";
import { getAccounts } from "./lib/accounts.mjs";
import { openConsole, getCredentialsAndWrite } from "./lib/assume.mjs";
import { getAccountRoles } from "./lib/accounts.mjs";
import { deleteCache, writeDefaultSessions } from "./lib/config.mjs";
import { showVersionInfo } from "./lib/version.mjs";
// restore cursor on ctrl+c
restoreCursor();
async function accountAutocomplete() {
await getAccounts({ showSpinner: true });
return autocomplete({
message: "Which account?",
pageSize: 7,
source: async (input) => {
const accounts = await getAccounts({
showSpinner: false,
});
if (input && input.length) {
return accounts.filter(
(x) => x.name.search(new RegExp(input, "i")) !== -1,
);
} else {
return accounts;
}
},
rightKeyCallback() {},
});
}
async function accountRolesAutocomplete(accountId) {
const roles = await getAccountRoles(accountId);
return autocomplete({
message: "Which Role?",
pageSize: 7,
source: async (input) => {
if (input && input.length) {
return roles.filter(
(x) => x.name.search(new RegExp(input, "i")) !== -1,
);
} else {
return roles;
}
},
rightKeyCallback(value) {
openConsole(value);
},
});
}
async function handleAutocomplete() {
const account = await accountAutocomplete();
const result = await accountRolesAutocomplete(account);
if (result) {
await getCredentialsAndWrite(result);
}
}
(async () => {
await showVersionInfo();
await writeDefaultSessions();
const { session, logout, role, account } = await getArgs();
if (logout) {
await deleteCache(session);
process.exit(0);
}
if (role && account) {
const accounts = await getAccounts({ showSpinner: true });
const found = accounts.find((x) => x.value.accountId === account);
if (!found) {
console.log(`Account with id ${account} not found`);
process.exit(1);
}
const result = { ...found.value, role };
await getCredentialsAndWrite(result);
process.exit(0);
}
while (true) {
await handleAutocomplete();
}
})();
process.on("SIGINT", () => {
process.exit();
});