Skip to content

Commit

Permalink
Add filter option to dot-process edit commad.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Jul 19, 2018
1 parent 37fd48e commit ab1e9dd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/Cli/OpCommands.php
Expand Up @@ -2,7 +2,9 @@

namespace Consolidation\Filter\Cli;

use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\Filter\LogicalOpFactory;
use Consolidation\Filter\FilterOutputData;
use Symfony\Component\Yaml\Yaml;

class OpCommands extends \Robo\Tasks
Expand Down Expand Up @@ -32,13 +34,32 @@ public function parse($expr, $options = ['format' => 'yaml', 'dump' => false])
*
* @command edit
* @aliases ed
* @filter-output
* @return array
*/
public function edit($data, $options = ['format' => 'yaml', 'in' => 'auto'])
{
return $this->read($data, $options['in']);
}

/**
* @hook alter @filter-output
* @option $filter Filter output based on provided expression
* @default $filter ''
*/
public function filterOutput($result, CommandData $commandData)
{
$expr = $commandData->input()->getOption('filter');
if (!empty($expr)) {
$factory = LogicalOpFactory::get();
$op = $factory->evaluate($expr);
$filter = new FilterOutputData();
$result = $filter->filter($result, $op);
}

return $result;
}

/**
* Read the data provided to this command.
*/
Expand Down
9 changes: 0 additions & 9 deletions src/OperatorFactory.php
Expand Up @@ -89,12 +89,3 @@ protected function splitOnOperator($expression)
return $matches;
}
}

// Find all quoted strings in $expression and save their values
// in a value map

// Substitute references from the value map into the expression

// Split the expression on the logical operators (& and | only)

// Iterate over the strings between the logical operators
8 changes: 8 additions & 0 deletions tests/OpCommandsTest.php
Expand Up @@ -61,6 +61,14 @@ public function exampleTestCommandParameters()
'Could not parse expression a', self::STATUS_ERROR,
'parse', 'a&b',
],

[
'a:
color: red
shape: round', self::STATUS_OK,
'edit', 'tests/fixtures/data.yml', '--filter=color=red',
],

];
}

Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/data.yml
@@ -0,0 +1,9 @@
a:
color: red
shape: round
b:
color: blue
shape: square
c:
color: green
shape: triangular

0 comments on commit ab1e9dd

Please sign in to comment.