How to automatically respond to confirmations in CLI tools (#tilPost)

Recently we released a new CLI tool to migrate data in Contentful. It’s currently in beta and we’ll constantly 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 C…

Recently we released a new CLI tool to migrate data in Contentful. It's currently in beta and we'll constantly 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)

So how could you answer that question when it's not you running this command but the 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:

By itself, the yes command outputs 'y' or whatever is specified as an argument, followed by a newline repeatedly until stopped by the user or otherwise killed; when piped into a command, it will continue until the pipe breaks (i.e., the program completes its execution).

So when you execute yes sunshine this is what you get:

$ yes sunshine
sunshine
sunshine
sunshine
sunshine
sunshine
...

And you get a lot of sunshine then. ;)

So what you can do with yes is that you can combine it with 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 that you can also just echo and pipe the string into the command.

$ echo yes | contentful-migration --space-id xxx migration.js

I think that's pretty cool stuff and this will definitely help me for my next automation tasks.


Reply to Stefan
Print Share Comment Cite Upload Translate
CITATION GOES HERE CITATION GOES HERE
Select a language: