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
APA
Stefan Judis | Sciencx (2024-03-29T05:31:28+00:00) » How to automatically respond to confirmations in CLI tools (#tilPost). Retrieved from https://www.scien.cx/2017/10/17/how-to-automatically-respond-to-confirmations-in-cli-tools-tilpost/.
MLA
" » How to automatically respond to confirmations in CLI tools (#tilPost)." Stefan Judis | Sciencx - Tuesday October 17, 2017, https://www.scien.cx/2017/10/17/how-to-automatically-respond-to-confirmations-in-cli-tools-tilpost/
HARVARD
Stefan Judis | Sciencx Tuesday October 17, 2017 » How to automatically respond to confirmations in CLI tools (#tilPost)., viewed 2024-03-29T05:31:28+00:00,<https://www.scien.cx/2017/10/17/how-to-automatically-respond-to-confirmations-in-cli-tools-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to automatically respond to confirmations in CLI tools (#tilPost). [Internet]. [Accessed 2024-03-29T05:31:28+00:00]. Available from: https://www.scien.cx/2017/10/17/how-to-automatically-respond-to-confirmations-in-cli-tools-tilpost/
CHICAGO
" » How to automatically respond to confirmations in CLI tools (#tilPost)." Stefan Judis | Sciencx - Accessed 2024-03-29T05:31:28+00:00. https://www.scien.cx/2017/10/17/how-to-automatically-respond-to-confirmations-in-cli-tools-tilpost/
IEEE
" » How to automatically respond to confirmations in CLI tools (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/10/17/how-to-automatically-respond-to-confirmations-in-cli-tools-tilpost/. [Accessed: 2024-03-29T05:31:28+00:00]
rf:citation
» How to automatically respond to confirmations in CLI tools (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/10/17/how-to-automatically-respond-to-confirmations-in-cli-tools-tilpost/ | 2024-03-29T05:31:28+00:00
https://github.com/addpipe/simple-recorderjs-demo