-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a way to pass configuration to flow cli
- Loading branch information
1 parent
634e2f4
commit 208bb8c
Showing
12 changed files
with
240 additions
and
60 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\CLI\Command\Traits; | ||
|
||
use Flow\ETL\Config; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
trait ConfigOptions | ||
{ | ||
private function addConfigOptions(Command $command) : void | ||
{ | ||
$command | ||
->addOption('config', null, InputOption::VALUE_REQUIRED, 'Path to a local php file that MUST return instance of: ' . Config::class); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\CLI\Options; | ||
|
||
use function Flow\CLI\option_string_nullable; | ||
use function Flow\Filesystem\DSL\path_real; | ||
use Flow\ETL\Config; | ||
use Flow\Filesystem\Local\NativeLocalFilesystem; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
|
||
final class ConfigOption | ||
{ | ||
public function __construct(private readonly string $optionName) | ||
{ | ||
} | ||
|
||
public function get(InputInterface $input) : Config | ||
{ | ||
$configPath = option_string_nullable($this->optionName, $input); | ||
|
||
if ($configPath === null) { | ||
return \Flow\ETL\DSL\config(); | ||
} | ||
|
||
$path = path_real($configPath); | ||
|
||
$fs = new NativeLocalFilesystem(); | ||
|
||
if ($fs->status($path) === null) { | ||
throw new InvalidArgumentException("File '{$path->path()}' does not exist."); | ||
} | ||
|
||
/** @psalm-suppress UnresolvableInclude */ | ||
$config = require $path->path(); | ||
|
||
if ($config instanceof Config\ConfigBuilder) { | ||
$config = $config->build(); | ||
} | ||
|
||
if (!$config instanceof Config) { | ||
throw new InvalidArgumentException('File "{$path->path()}" does not return instance of "' . Config::class . '" or "' . Config\ConfigBuilder::class . '".'); | ||
} | ||
|
||
return $config; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/cli/tests/Flow/CLI/Tests/Integration/Options/ConfigOptionTest.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\CLI\Tests\Integration\Options; | ||
|
||
use Flow\CLI\Options\ConfigOption; | ||
use Flow\ETL\Adapter\Elasticsearch\Tests\Integration\TestCase; | ||
use Flow\ETL\Config; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Input\{ArrayInput, InputDefinition, InputOption}; | ||
|
||
final class ConfigOptionTest extends TestCase | ||
{ | ||
public function test_getting_config_from_option() : void | ||
{ | ||
$option = new InputOption('config', null, InputOption::VALUE_REQUIRED); | ||
$definition = new InputDefinition([$option]); | ||
|
||
$config = (new ConfigOption('config'))->get(new ArrayInput(['--config' => __DIR__ . '/Fixtures/.flow.config.php'], $definition)); | ||
|
||
self::assertInstanceOf(Config::class, $config); | ||
self::assertSame('execution-id', $config->id()); | ||
} | ||
|
||
public function test_getting_default_config() : void | ||
{ | ||
$option = new InputOption('config', null, InputOption::VALUE_OPTIONAL); | ||
$definition = new InputDefinition([$option]); | ||
|
||
$config = (new ConfigOption('config'))->get(new ArrayInput([], $definition)); | ||
|
||
self::assertInstanceOf(Config::class, $config); | ||
self::assertNotSame('execution-id', $config->id()); | ||
} | ||
|
||
public function test_throwing_exception_when_config_file_does_not_exist() : void | ||
{ | ||
$option = new InputOption('config', null, InputOption::VALUE_REQUIRED); | ||
$definition = new InputDefinition([$option]); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
|
||
(new ConfigOption('config'))->get(new ArrayInput(['--config' => 'non-existing-file.php'], $definition)); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/cli/tests/Flow/CLI/Tests/Integration/Options/Fixtures/.flow.config.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
use function Flow\ETL\DSL\config_builder; | ||
|
||
return config_builder() | ||
->id('execution-id'); |
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
Oops, something went wrong.