How to automatically respond to confirmations in CLI tools
- Published at
- Updated at
- Reading time
Recently, we released a new CLI tool to migrate data in Contentful. It's currently in beta and we'll work on it to make it better.
Last week we received a question in our community forum. A user asked if he could use the CLI programmatically. He had the problem that the CLI asks at some point for confirmation and this makes it hard to run in a CI environment.
? Do you want to apply the migration? (Y/n)
How could you answer this question when it's not you running this command but a potential CI environment.
My colleage Stephan had a cool solution. He responded that the unix command
yes could help out here.
yes? What kind of command is that?
The man page for
yes is not giving much information but according to [Wikipedia](https://en.wikipedia.org/wiki/Yes_(Unix) this is what it does:
When you execute
yes sunshine this is what you'll get:
$ yes sunshine sunshine sunshine sunshine sunshine sunshine ...
A lot of sunshine! ☀️
Use it to combine
yes with other CLI commands and auto-respond to confirmations.
$ yes Y | contentful-migration --space-id xxx migration.js
There is another way to answer confirmation in a CLI automatically, though. It turns out, you can also
echo and pipe a string into another command.
$ echo yes | contentful-migration --space-id xxx migration.js
I think that's pretty cool stuff and it will definitely help me for my next automation tasks!