Skip to content

Commit

Permalink
feat: add option to not override default profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnig committed Oct 4, 2024
1 parent e5540f8 commit 7210dca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
24 changes: 17 additions & 7 deletions lib/assume.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ async function getCredentials(accountId, roleName) {
}

export async function getCredentialsAndWrite(account) {
const { session } = await getArgs();
const { session, skipDefaultProfile } = await getArgs();

const credentials = await getCredentials(account.accountId, account.role);
const { vt_default_region, sso_start_url, sso_region } =
await getCurrentSession();

const name = account.shortId + "-" + account.role;

await writeCredentials("default", credentials);
if (!skipDefaultProfile) {
await writeCredentials("default", credentials);
}
await writeCredentials(name, credentials);

await writeProfile(name, {
Expand All @@ -68,11 +70,19 @@ export async function getCredentialsAndWrite(account) {
region: vt_default_region || "eu-central-1",
});

console.log(
chalk.green(
`AWS credentials set for account ${account.name}.\nYou can use credentials with your default profile or --profile ${name}\n`,
),
);
if (!skipDefaultProfile) {
console.log(
chalk.green(
`AWS credentials set for account ${account.name}.\nYou can use the credentials with your default profile or --profile ${name} in the aws cli.\n`,
),
);
} else {
console.log(
chalk.green(
`AWS credentials set for account ${account.name}.\nYou can use the credentials with --profile ${name} in the aws cli.\n`,
),
);
}
}

function getRandomColor() {
Expand Down
10 changes: 6 additions & 4 deletions lib/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export async function getArgs() {
"-r, --role [value]",
"The name of a role which can be assumed. For example VT-DevOps. Must be used in combination with -a.",
)

.option(
"-a, --account [value]",
"The account in which the role should be assumed. For example 015572128213.",
);

program.option("-l, --logout", "Logout the defined sso-session.");
)
.option(
"-n, --skip-default-profile",
"Do not write credentials to the default profile.",
)
.option("-l, --logout", "Logout the defined sso-session.");

opts = await program.parse(process.argv).opts();

Expand Down
2 changes: 1 addition & 1 deletion lib/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export async function readCache(sessionName) {
}

try {
JSON.parse(await readFile(cacheFile, "utf8"));
return JSON.parse(await readFile(cacheFile, "utf8"));
} catch (_) {
return {};
}
Expand Down

0 comments on commit 7210dca

Please sign in to comment.