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