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…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2017-10-17T21:00:00+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 ,<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 ]. 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 . 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: ]
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/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.