| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Commands;
- use App\Service\Utils\Path;
- use Symfony\Component\Console\Attribute\AsCommand;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Contracts\Service\Attribute\Required;
- /**
- * Post install script: create or replace the symlink .env.local pointing
- * to the .env file matching the current environment.
- *
- * @see doc/env.md
- */
- #[AsCommand(
- name: 'ot:setup:env',
- description: 'Setup the env file according to the current host'
- )]
- class SetupEnvCommand extends Command
- {
- const ENVIRONMENTS_FILES = [
- 'ap2i' => '.env.docker',
- 'prod-2' => '.env.prod',
- 'test-2' => '.env.test',
- 'test1' => '.env.test1',
- 'test2' => '.env.test2',
- 'test3' => '.env.test3',
- 'test4' => '.env.test4',
- 'test5' => '.env.test5',
- 'ci' => '.env.staging',
- ];
- private string $projectDir;
- /**
- * @param string $projectDir
- * @return void
- */
- #[Required]
- public function setProjectDir(string $projectDir): void {
- $this->projectDir = $projectDir;
- }
- /**
- * Configures the command
- */
- protected function configure(): void
- {
- $this->addOption(
- 'host',
- null,
- InputOption::VALUE_REQUIRED,
- "Use this hostname instead of the current's host name."
- );
- }
- protected function execute(InputInterface $input, OutputInterface $output): int
- {
- if ($input->getOption('host')) {
- $hostname = $input->getOption('host');
- } else if (getenv('HOST')) {
- $hostname = getenv('HOST');
- } else {
- $hostname = gethostname();
- }
- if (!array_key_exists($hostname, self::ENVIRONMENTS_FILES)) {
- throw new \RuntimeException("Critical : unknown environment [" . $hostname . "]");
- }
- $envFile = Path::join($this->projectDir, 'env', self::ENVIRONMENTS_FILES[$hostname]);
- $symlinkLocation = Path::join($this->projectDir, '.env.local');
- if (file_exists($symlinkLocation)) {
- unlink($symlinkLocation);
- $output->writeln($symlinkLocation . " was deleted");
- }
- symlink($envFile, $symlinkLocation);
- $output->writeln("Symlink created : " . $symlinkLocation . " => " . $envFile);
- return Command::SUCCESS;
- }
- }
|