Created
March 25, 2021 14:40
-
-
Save takali/63ace9fd56c3d8ff6a7b5da096bf457e to your computer and use it in GitHub Desktop.
Laravel command - list all docker container and display command to connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Symfony\Component\Yaml\Parser; | |
use Symfony\Component\Yaml\Yaml; | |
// package symfony/yaml is require | |
class ContainerConnect extends Command | |
{ | |
/** | |
* @var Parser | |
*/ | |
protected $yamlParser; | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'container:connect'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Liste all containers and command to connect'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->yamlParser = new Parser(); | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$services = []; | |
$parser = $this->yamlParser->parseFile('./docker-compose.yml'); | |
foreach ($parser['services'] as $key => $value) { | |
$containerName = $value['container_name'] ?? $key; | |
$services[$key] = $containerName; | |
} | |
$container = $this->choice('Select container to connect', $services); | |
$this->info(sprintf('docker exec -ti %s /bin/bash', $services[$container])); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment