11#!/usr/bin/env node
2+
23import yargs from "yargs" ;
4+ import chalk from "chalk" ;
5+ import open from "open" ;
36import { hideBin } from "yargs/helpers" ;
47import Configstore from "configstore" ;
8+ import { getSigninUrl } from "./lib/web.js" ;
59import { refreshCredentials } from "./lib/auth.js" ;
610import { handleError } from "./lib/error.js" ;
711import {
@@ -23,19 +27,20 @@ const configstore = new Configstore("aws-sso-cli");
2327
2428const signInHandler = async ( argv ) => {
2529 try {
26- const profile =
27- "profile" in argv
28- ? argv . profile
29- : await chooseProfile ( configstore ) ;
30-
30+ const profile = "profile" in argv ? argv . profile : await chooseProfile ( configstore ) ;
31+
3132 const config = await refreshCredentials ( loadConfig ( configstore , profile ) , argv . forceNewToken ) ;
3233 updateConfig ( configstore , profile , config ) ;
33- const { token : { accessToken } , region, } = config ;
34+ const {
35+ token : { accessToken } ,
36+ region,
37+ } = config ;
3438
3539 const { accountId } =
3640 "account" in argv
3741 ? await findAccountByName ( accessToken , argv . account , region )
3842 : await chooseAccount ( accessToken , region ) ;
43+
3944 const { roleName } =
4045 "role" in argv
4146 ? await findRoleByName ( accessToken , argv . role , accountId , region )
@@ -45,14 +50,19 @@ const signInHandler = async (argv) => {
4550 roleCredentials : { accessKeyId, secretAccessKey, sessionToken } ,
4651 } = await getCredentials ( accessToken , accountId , roleName , region ) ;
4752
48- console . log (
49- "" ,
50- `export AWS_ACCESS_KEY_ID=${ accessKeyId } ` ,
51- "\n" ,
52- `export AWS_SECRET_ACCESS_KEY=${ secretAccessKey } ` ,
53- "\n" ,
54- `export AWS_SESSION_TOKEN=${ sessionToken } `
55- ) ;
53+ if ( argv . web ) {
54+ open ( await getSigninUrl ( accessKeyId , secretAccessKey , sessionToken , region ) ) ;
55+ } else {
56+ console . log (
57+ "" ,
58+ `export AWS_ACCESS_KEY_ID=${ accessKeyId } ` ,
59+ "\n" ,
60+ `export AWS_SECRET_ACCESS_KEY=${ secretAccessKey } ` ,
61+ "\n" ,
62+ `export AWS_SESSION_TOKEN=${ sessionToken } `
63+ ) ;
64+ }
65+ console . error ( chalk . bold . green ( "\nAll set!" ) ) ;
5666 } catch ( err ) {
5767 handleError ( err ) ;
5868 }
@@ -115,5 +125,10 @@ yargs(hideBin(process.argv))
115125 describe : "Force fetch a new access token for AWS SSO." ,
116126 type : "boolean" ,
117127 } )
128+ . option ( "w" , {
129+ alias : "web" ,
130+ describe : "Open selected AWS account in your web browser." ,
131+ type : "boolean" ,
132+ } )
118133 . wrap ( 90 )
119134 . help ( "help" , "Show help." ) . argv ;
0 commit comments