|
2 | 2 |
|
3 | 3 | namespace Webteractive\Devstack\Commands; |
4 | 4 |
|
| 5 | +use Dotenv\Dotenv; |
| 6 | +use Webteractive\Devstack\Env; |
5 | 7 | use Webteractive\Devstack\Process; |
| 8 | +use Webteractive\Devstack\WithEnv; |
6 | 9 | use Webteractive\Devstack\WithSignalHandlers; |
| 10 | +use Webteractive\Devstack\WithStorage; |
7 | 11 |
|
8 | 12 | class MySql extends Base |
9 | 13 | { |
10 | | - use WithSignalHandlers; |
| 14 | + use WithSignalHandlers, |
| 15 | + WithEnv; |
11 | 16 |
|
12 | 17 | protected $signature = 'mysql |
13 | | - {--p|password=password : The password to use.} |
14 | | - {--u|user=user : The user to use.} |
15 | | - {--db|database= : The database to use.}'; |
| 18 | + {--p|password= : The password to use.} |
| 19 | + {--u|user= : The user to use.} |
| 20 | + {--db|database= : The database to use.} |
| 21 | + {--env= : The path to the .env file. Default\'s to the current working directory.}'; |
16 | 22 | protected $description = 'Start a MySQL CLI session within the <comment>mysql</comment> container.'; |
17 | 23 |
|
18 | 24 | public function handle(): int |
19 | 25 | { |
20 | | - $password = $this->output-> |
| 26 | + $defaults = [ |
| 27 | + 'password' => 'password', |
| 28 | + 'user' => 'user', |
| 29 | + 'database' => null, |
| 30 | + ]; |
| 31 | + $envPath = $this->input->getOption('env') ?? getcwd(); |
| 32 | + if (file_exists($envPath . '/.env')) { |
| 33 | + $env = new Env($envPath); |
| 34 | + $defaults['password'] = $env->get('DB_PASSWORD'); |
| 35 | + $defaults['user'] = $env->get('DB_USERNAME'); |
| 36 | + $defaults['database'] = $env->get('DB_DATABASE'); |
| 37 | + $this->lineBreak(); |
| 38 | + $this->line('Found an <comment>.env</comment> file in your current working directory, values will now be used as defaults.'); |
| 39 | + $this->lineBreak(); |
| 40 | + } else { |
| 41 | + $this->lineBreak(); |
| 42 | + $this->line('Unable to find an <comment>.env</comment> file in your current working directory, now using the defaults.'); |
| 43 | + $this->line('If these were changed in your <comment>docker-compose.yml</comment> file, please supply it as a command'); |
| 44 | + $this->line('flag or add an <comment>.env</comment> file and add it there.'); |
| 45 | + |
| 46 | + $this->lineBreak(); |
| 47 | + $this->line('If you want to use the .env route, create a .env file and add the variables below including the values:'); |
| 48 | + $this->line('DB_USERNAME='); |
| 49 | + $this->line('DB_PASSWORD='); |
| 50 | + $this->line('DB_DATABASE='); |
| 51 | + $this->lineBreak(); |
| 52 | + $this->line('If your .env is located somewhere else, you may use the <comment>--env=/path/to/your/.env</comment> flag.'); |
| 53 | + $this->line('For example, <comment>devstack mysql --env=/path/to/your/.env/directory</comment>.'); |
| 54 | + $this->lineBreak(); |
| 55 | + $this->line('Finally for the command flag, just do <comment>devstack mysql --user=the_user --password=the_password --database=the_db</comment>.'); |
| 56 | + $this->line('For more details on the <comment>devsack mysql</comment> command flags, run <comment>devstack help mysql</comment>.'); |
| 57 | + $this->lineBreak(); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + $password = $this->input->getOption('password') ?? $defaults['password']; |
| 62 | + $user = $this->input->getOption('user') ?? $defaults['user']; |
| 63 | + $database = $this->input->getOption('database') ?? $defaults['database']; |
| 64 | + |
| 65 | + $bashCommand = []; |
| 66 | + $bashCommand[] = "MYSQL_PWD={$password}"; |
| 67 | + $bashCommand[] = "mysql -u {$user}"; |
| 68 | + if ($database) { |
| 69 | + $bashCommand[] = $database; |
| 70 | + } |
21 | 71 |
|
22 | 72 | $this->handleTerminationSignals( |
23 | | - $process = Process::prepareFromShell('docker compose exec -it mysql bash -c "MYSQL_PWD=password mysql -u user;"') |
| 73 | + $process = Process::prepareFromShell('docker compose exec -it mysql bash -c "' . join(' ', $bashCommand) . '"') |
24 | 74 | ); |
25 | 75 |
|
26 | | - $process->setTty(true)->run(); |
| 76 | + $process->setTty(true) |
| 77 | + ->setTimeout(60 * 60 * 2) |
| 78 | + ->setIdleTimeout(60 * 60 * 8) |
| 79 | + ->run(); |
27 | 80 |
|
28 | 81 | return static::SUCCESS; |
29 | 82 | } |
|
0 commit comments