import clc from 'cli-color'; import commander from 'commander'; import { checkForUpdate } from '../checkForUpdate'; import { errorAndExit } from '../errorAndExit'; import { promptForAlksAccountAndRole } from '../promptForAlksAccountAndRole'; import { log } from '../log'; import { promptForOutputFormat } from '../promptForOutputFormat'; import { promptForServer } from '../promptForServer'; import { setServer } from '../state/server'; import { setAlksAccount } from '../state/alksAccount'; import { setAlksRole } from '../state/alksRole'; import { setOutputFormat } from '../state/outputFormat'; import { promptForToken } from '../promptForToken'; import { promptForAuthType, REFRESH_TOKEN_AUTH_CHOICE, CREDENTIAL_PROCESS_AUTH_CHOICE, } from '../promptForAuthType'; import { validateAlksAccount } from '../validateAlksAccount'; import tabtab from 'tabtab'; import { setToken } from '../state/token'; import { setCredentialProcess } from '../state/credentialProcess'; import { promptForCredentialProcess } from '../promptForCredentialProcess'; export async function handleAlksDeveloperConfigure( options: commander.OptionValues ) { try { if (options.nonInteractive) { console.log( 'Warning: configuring in non-interactive mode may leave the alks cli only partially configured. Running this command in interactive mode may still be needed to fully configure this tool' ); } const shouldPrompt = !options.nonInteractive; if (options.server || shouldPrompt) { await setServer(options.server ?? (await promptForServer())); } // Override authType flag if a credential process was provided let authTypeFlag = options.authType; if (options.credentialProcess) { authTypeFlag = CREDENTIAL_PROCESS_AUTH_CHOICE; } if (authTypeFlag || shouldPrompt) { const authType = authTypeFlag ?? (await promptForAuthType()); switch (authType) { case REFRESH_TOKEN_AUTH_CHOICE: { await setToken(await promptForToken()); break; } case CREDENTIAL_PROCESS_AUTH_CHOICE: { await setCredentialProcess( options.credentialProcess ?? (await promptForCredentialProcess()) ); break; } default: { throw new Error( `Invalid auth type "${authType}". Valid types are: ${REFRESH_TOKEN_AUTH_CHOICE}, ${CREDENTIAL_PROCESS_AUTH_CHOICE}` ); } } } if (options.account && options.role) { await validateAlksAccount(options.account, options.role); await setAlksAccount(options.account); await setAlksRole(options.role); } else if (shouldPrompt) { log('Getting ALKS accounts'); const { alksAccount, alksRole } = await promptForAlksAccountAndRole({ prompt: 'Please select your default ALKS account/role', }); await setAlksAccount(alksAccount); await setAlksRole(alksRole); } if (options.output || shouldPrompt) { log('Getting output formats'); setOutputFormat(options.output ?? (await promptForOutputFormat())); } console.error(clc.white('Your developer configuration has been updated.')); } catch (err) { errorAndExit( 'Error configuring developer: ' + (err as Error).message, err as Error ); } // Post-configure steps are non-critical: the configuration is already saved // by this point. A failure here should warn and continue, not abort with a // misleading "Error configuring developer:" message. if (process.stdin.isTTY && !options.nonInteractive) { try { await tabtab.install({ name: 'alks', completer: 'alks', }); } catch (err) { console.error( clc.yellow( 'Warning: failed to install shell tab completion: ' + (err as Error).message + '. Your ALKS configuration was saved successfully; this does not affect ALKS functionality.' ) ); } } try { await checkForUpdate(); } catch (err) { console.error( clc.yellow( 'Warning: failed to check for updates: ' + (err as Error).message ) ); } }