unglued
unglued

Reputation: 1061

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?

Upvotes: 6

Views: 2008

Answers (2)

Jens A. Koch
Jens A. Koch

Reputation: 41776

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));

Upvotes: 5

unglued
unglued

Reputation: 1061

One way to get input from the user is to put data into a file and read it in your bash script:

#!/bin/bash
source .env

Upvotes: -2

Related Questions