Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Added ability to change the working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rickkuilman authored and peterjaap committed Nov 22, 2022
1 parent 407fc4c commit 03a8091
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"illuminate/database": "^8.75",
"symfony/console": "^5.4",
"symfony/yaml": "^5.4",
"fakerphp/faker": "^1.17"
"fakerphp/faker": "^1.17",
"webmozart/assert": "^1.11"
},
"license": "MIT",
"authors": [
Expand Down
62 changes: 60 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/Elgentos/Masquerade/Console/GroupsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elgentos\Masquerade\Console;

use Elgentos\Masquerade\Helper\Config;
use Elgentos\Masquerade\WorkingDirectory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -41,7 +42,8 @@ protected function configure()
->setName($this->name)
->setDescription($this->description)
->addOption('config', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'One or more extra config directories for config.yaml or platform configs')
->addOption('platform', null, InputOption::VALUE_REQUIRED);
->addOption('platform', null, InputOption::VALUE_REQUIRED)
->addOption(WorkingDirectory::OPTION_NAME, null, InputOption::VALUE_OPTIONAL, WorkingDirectory::DESCRIPTION);
}

/**
Expand Down Expand Up @@ -86,6 +88,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
private function setup()
{
WorkingDirectory::change($this->input);

$this->configHelper = new Config($this->input->getOptions());
$databaseConfig = $this->configHelper->readConfigFile();

Expand Down
6 changes: 5 additions & 1 deletion src/Elgentos/Masquerade/Console/IdentifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elgentos\Masquerade\Console;

use Elgentos\Masquerade\Helper\Config;
use Elgentos\Masquerade\WorkingDirectory;
use Faker\Documentor;
use Faker\Generator;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -69,7 +70,8 @@ protected function configure()
->addOption('password', null, InputOption::VALUE_OPTIONAL)
->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Database host [localhost]')
->addOption('prefix', null, InputOption::VALUE_OPTIONAL, 'Database prefix [empty]')
->addOption('charset', null, InputOption::VALUE_OPTIONAL, 'Database charset [utf8]');
->addOption('charset', null, InputOption::VALUE_OPTIONAL, 'Database charset [utf8]')
->addOption(WorkingDirectory::OPTION_NAME, null, InputOption::VALUE_OPTIONAL, WorkingDirectory::DESCRIPTION);
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand Down Expand Up @@ -163,6 +165,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
private function setup()
{
WorkingDirectory::change($this->input);

$this->configHelper = new Config($this->input->getOptions());
$databaseConfig = $this->configHelper->readConfigFile();

Expand Down
6 changes: 5 additions & 1 deletion src/Elgentos/Masquerade/Console/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Elgentos\Masquerade\DataProcessorFactory;
use Elgentos\Masquerade\Helper\Config;
use Elgentos\Masquerade\Output;
use Elgentos\Masquerade\WorkingDirectory;
use Faker\Generator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -102,7 +103,8 @@ protected function configure()
->addOption('group', null, InputOption::VALUE_OPTIONAL, 'Which groups to run masquerade on [all]')
->addOption('charset', null, InputOption::VALUE_OPTIONAL, 'Database charset [utf8]')
->addOption('with-integrity', null, InputOption::VALUE_NONE, 'Run with foreign key checks enabled')
->addOption('batch-size', null, InputOption::VALUE_REQUIRED, 'Batch size to use for anonymization', 500);
->addOption('batch-size', null, InputOption::VALUE_REQUIRED, 'Batch size to use for anonymization', 500)
->addOption(WorkingDirectory::OPTION_NAME, null, InputOption::VALUE_OPTIONAL, WorkingDirectory::DESCRIPTION);
}

/**
Expand Down Expand Up @@ -276,6 +278,8 @@ private function asScalar($value)
*/
private function setup()
{
WorkingDirectory::change($this->input);

$this->configHelper = new Config($this->input->getOptions());

$databaseConfig = $this->configHelper->readConfigFile();
Expand Down
33 changes: 33 additions & 0 deletions src/Elgentos/Masquerade/WorkingDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Elgentos\Masquerade;

use Symfony\Component\Console\Input\InputInterface;
use Webmozart\Assert\Assert;

final class WorkingDirectory
{
const OPTION_NAME = 'working-dir';
const DESCRIPTION = 'Set the working directory';

public static function change(InputInterface $input)
{
$workingDir = $input->getOption(self::OPTION_NAME) ?? null;

if (null === $workingDir) {
return;
}

Assert::directory($workingDir, 'Could not change the working directory to %s: directory does not exists or file is not a directory.');

if (false === chdir($workingDir)) {
throw new \RuntimeException(
sprintf(
'Failed to change the working directory to "%s" from "%s".',
$workingDir,
getcwd(),
),
);
}
}
}

0 comments on commit 03a8091

Please sign in to comment.