Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User input in composer scripts

In composer package I have post-install-cmd script, something like this:

#!/bin/bash
echo 'Hello!'
read -p 'Database password: ' DB_PASS
php setup/index.php database_password=$DB_PASS
echo 'Complete!'

But after composer install I got this error:

...
Generating autoload files
> post-install-cmd: _scripts/ask_db_data.sh
Executing command (CWD): _scripts/ask_db_data.sh
Hello!
Script _scripts/ask_db_data.sh handling the post-install-cmd event returned with an error


[RuntimeException]                                                   
Error Output: Hello!  

Exception trace:
() at phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php:196
 Composer\EventDispatcher\EventDispatcher->doDispatch() at phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php:94
 Composer\EventDispatcher\EventDispatcher->dispatchScript() at phar:///usr/local/bin/composer/src/Composer/Installer.php:350
 Composer\Installer->run() at phar:///usr/local/bin/composer/src/Composer/Command/InstallCommand.php:134
 Composer\Command\InstallCommand->execute() at phar:///usr/local/bin/composer/vendor/symfony/console/Command/Command.php:256
 Symfony\Component\Console\Command\Command->run() at phar:///usr/local/bin/composer/vendor/symfony/console/Application.php:838
 Symfony\Component\Console\Application->doRunCommand() at phar:///usr/local/bin/composer/vendor/symfony/console/Application.php:189
 Symfony\Component\Console\Application->doRun() at phar:///usr/local/bin/composer/src/Composer/Console/Application.php:167
 Composer\Console\Application->doRun() at phar:///usr/local/bin/composer/vendor/symfony/console/Application.php:120
 Symfony\Component\Console\Application->run() at phar:///usr/local/bin/composer/src/Composer/Console/Application.php:98
 Composer\Console\Application->run() at phar:///usr/local/bin/composer/bin/composer:43
 require() at /usr/local/bin/composer:25

How I can ask user input in composer scripts?

like image 794
Alexander Matrosov Avatar asked Dec 20 '15 13:12

Alexander Matrosov


People also ask

How do I update dependency in composer?

Updating dependencies to their latest versions# To update to the latest versions, use the update command. This will fetch the latest matching versions (according to your composer.json file) and update the lock file with the new versions.

What is a composer script?

A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command. Scripts are useful for executing a package's custom code or package-specific commands during the Composer execution process. Note: Only scripts defined in the root package's composer.


1 Answers

Ok, you are trying to execute a bash/shell script to read the string and pass it to your php file. I'm not sure, why that doesn't work out.

The alternative is to stay in the PHP code of your post-install-cmd. Thats an Event and it has access to Composer and IO. For working with the Console you need the $io object from Composer. You can fetch it with getIO().

You might then use the methods ask() or askConfirmation() to interactively ask the user for "missing" values.

Very brief:

$io = $this->getIO();
$user = $io->ask('Enter your Username: ');
/// Its better not displaying the password:
$pw = $io->askAndHideAnswer('Enter your Password: ');

exec('php setup/index.php database_password=' . escapeshellarg($pw));
like image 162
Jens A. Koch Avatar answered Sep 22 '22 12:09

Jens A. Koch