Skip to content

Commit f47568b

Browse files
committed
Add docker compose, php, shell, mysql, and redis commands.
1 parent 672eab0 commit f47568b

8 files changed

Lines changed: 126 additions & 11 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"league/flysystem": "^3.0",
2222
"symfony/console": "^6.2",
2323
"symfony/finder": "^6.2",
24-
"symfony/process": "^6.2"
24+
"symfony/process": "^6.2",
25+
"vlucas/phpdotenv": "^5.5"
2526
},
2627
"require-dev": {
2728
"mockery/mockery": "^1.5.1",

devstack

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use Webteractive\Devstack\Commands\InitStack;
1313
use Webteractive\Devstack\RegisterDockerComposeCommands;
1414
use Webteractive\Devstack\Commands\DownloadRuntimes;
1515
use Webteractive\Devstack\Commands\MySql;
16+
use Webteractive\Devstack\Commands\Redis;
1617
use Webteractive\Devstack\Commands\RunComposerCommands;
1718
use Webteractive\Devstack\Commands\RunLaravelArtisanCommand;
1819
use Webteractive\Devstack\Commands\RunPHPCommand;
@@ -28,6 +29,7 @@ $app->add(new RunPHPCommand);
2829
$app->add(new RunComposerCommands);
2930
$app->add(new Shell);
3031
$app->add(new MySql);
32+
$app->add(new Redis);
3133

3234
RegisterDockerComposeCommands::register($app);
3335

src/Commands/Base.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Webteractive\Devstack\Commands;
44

5+
use Dotenv\Dotenv;
56
use Symfony\Component\Console\Command\Command;
67
use Symfony\Component\Console\Input\InputInterface;
78
use Symfony\Component\Console\Output\OutputInterface;
@@ -27,7 +28,7 @@ abstract class Base extends Command
2728
protected OutputInterface $output;
2829

2930
public function __construct()
30-
{
31+
{
3132
if (isset($this->signature)) {
3233
$this->setup();
3334
} else {
@@ -129,6 +130,11 @@ public function line($message = '')
129130
return $this;
130131
}
131132

133+
public function lineBreak()
134+
{
135+
return $this->line('');
136+
}
137+
132138
public function info($message = '')
133139
{
134140
$this->output->writeln(empty($message) ? '' : "<info>{$message}</info>");

src/Commands/MySql.php

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,81 @@
22

33
namespace Webteractive\Devstack\Commands;
44

5+
use Dotenv\Dotenv;
6+
use Webteractive\Devstack\Env;
57
use Webteractive\Devstack\Process;
8+
use Webteractive\Devstack\WithEnv;
69
use Webteractive\Devstack\WithSignalHandlers;
10+
use Webteractive\Devstack\WithStorage;
711

812
class MySql extends Base
913
{
10-
use WithSignalHandlers;
14+
use WithSignalHandlers,
15+
WithEnv;
1116

1217
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.}';
1622
protected $description = 'Start a MySQL CLI session within the <comment>mysql</comment> container.';
1723

1824
public function handle(): int
1925
{
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+
}
2171

2272
$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) . '"')
2474
);
2575

26-
$process->setTty(true)->run();
76+
$process->setTty(true)
77+
->setTimeout(60 * 60 * 2)
78+
->setIdleTimeout(60 * 60 * 8)
79+
->run();
2780

2881
return static::SUCCESS;
2982
}

src/Commands/Redis.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack\Commands;
4+
5+
use Webteractive\Devstack\Process;
6+
use Webteractive\Devstack\WithSignalHandlers;
7+
8+
class Redis extends Base
9+
{
10+
use WithSignalHandlers;
11+
12+
protected $signature = 'redis';
13+
protected $description = 'Start a Redis CLI session within the <comment>redis</comment> container.';
14+
15+
public function handle(): int
16+
{
17+
18+
$this->handleTerminationSignals(
19+
$process = Process::prepareFromShell('docker compose exec -it redis redis-cli')
20+
);
21+
22+
$process->setTty(true)
23+
->setTimeout(60 * 60 * 2)
24+
->setIdleTimeout(60 * 60 * 8)
25+
->run();
26+
27+
return static::SUCCESS;
28+
}
29+
}

src/Commands/RunPhpCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Webteractive\Devstack\Process;
66
use Webteractive\Devstack\WithSignalHandlers;
77

8-
class RunPHPCommand extends Base
8+
class RunPhpCommand extends Base
99
{
1010
use WithSignalHandlers;
1111

src/Commands/Shell.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public function handle(): int
2929
$process = Process::prepare(array_merge($command, ['app', 'bash']))
3030
);
3131

32-
$process->setTty(true)->run();
32+
$process->setTty(true)
33+
->setTimeout(60 * 60 * 2)
34+
->setIdleTimeout(60 * 60 * 8)
35+
->run();
3336

3437
return static::SUCCESS;
3538
}

src/Env.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack;
4+
5+
use Dotenv\Dotenv;
6+
use Illuminate\Support\Arr;
7+
8+
class Env
9+
{
10+
protected $dotenv;
11+
12+
public function __construct($path)
13+
{
14+
$this->dotenv = Dotenv::createImmutable($path)->safeLoad();
15+
}
16+
17+
public function get($key, $default = null)
18+
{
19+
return Arr::get($this->dotenv, $key, $default);
20+
}
21+
}

0 commit comments

Comments
 (0)