User input in composer script

In the composer package, I have a 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 installI 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 can I request user input in composer scripts?

+4
source share
2 answers

Ok, you are trying to run a bash / shell script to read a line and pass it to your php file. I am not sure why this will not work.

An alternative is to stay in your PHP code post-install-cmd. This is an event and it has access to Composer and IO. To use the Console, you need an object $iofrom Composer. You can get it with getIO().

ask() askConfirmation() "" .

:

$io = $this->getIO();
$pw = $io->ask('Enter your Password: ');

exec('php setup/index.php database_password=' . escapeshellarg($pw));
+3

, bash script:

#!/bin/bash
source .env
0

Source: https://habr.com/ru/post/1620981/


All Articles