-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(toolkit): request response #32926
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ import { formatSdkLoggerContent } from 'aws-cdk/lib/api/aws-auth/sdk-logger'; | |
import { ToolkitAction } from '../types'; | ||
import { IIoHost, IoMessage, IoRequest } from './io-host'; | ||
import { trace } from './messages'; | ||
import { withCorkedLogging } from 'aws-cdk/lib/logging'; | ||
import { ToolkitError } from '../api/errors'; | ||
import chalk from 'chalk'; | ||
|
||
export function withAction(ioHost: IIoHost, action: ToolkitAction) { | ||
return { | ||
|
@@ -109,3 +112,37 @@ export function asSdkLogger(ioHost: IIoHost, action: ToolkitAction): Logger { | |
} | ||
}; | ||
} | ||
|
||
let TESTING = false; | ||
|
||
export function markTesting() { | ||
TESTING = true; | ||
} | ||
|
||
/** | ||
* Ask the user for a yes/no confirmation | ||
* | ||
* Automatically fail the confirmation in case we're in a situation where the confirmation | ||
* cannot be interactively obtained from a human at the keyboard. | ||
*/ | ||
export async function askUserConfirmation( | ||
ioHost: IIoHost, | ||
concurrency: number, | ||
motivation: string, | ||
question: string, | ||
) { | ||
await withCorkedLogging(async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #32898 needs to be implemented before we can get rid of this, right? |
||
// only talk to user if STDIN is a terminal (otherwise, fail) | ||
if (!TESTING && !process.stdin.isTTY) { | ||
throw new ToolkitError(`${motivation}, but terminal (TTY) is not attached so we are unable to get a confirmation from the user`); | ||
} | ||
|
||
// only talk to user if concurrency is 1 (otherwise, fail) | ||
if (concurrency > 1) { | ||
throw new ToolkitError(`${motivation}, but concurrency is greater than 1 so we are unable to get a confirmation from the user`); | ||
} | ||
|
||
const confirmed = await ioHost.requestResponse<any, boolean>(prompt(`${chalk.cyan(question)} (y/n)?`)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not too sure what to do here. |
||
if (!confirmed) { throw new ToolkitError('Aborted by user'); } | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assume this would still be necessary when we test