Skip to content

Commit

Permalink
Add ability to specify groups of commands to execute
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Sanchez committed Oct 24, 2017
1 parent 5ac7136 commit 557ab7a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ sleepers:
- 'sleep 6'
- 'sleep 2'
- 'sleep 4'
even_more_sleeps:
- 'sleep 2'
- 'sleep 8'
- 'sleep 1'
highlight-keywords:
- 'words'
- 'to be'
Expand All @@ -51,12 +55,24 @@ Save the contents of the file and close vim. Then you can choose the option numb

### Running

To execute the groups, just run by the desired key, in this case will be `sleepers`:
To execute the groups in the order coming from the config file, just run by the desired key, in this case will be `sleepers`:

```bash
$ ./ace execute -k sleepers
```

You can filter by groups just adding them after the key name:

```bash
$ ./ace execute -k sleepers even_more_sleeps more_sleeps
```

Note that the groups will be executed in order. You can even repeat a group if you want to execute it twice (or more):

```bash
$ ./ace execute -k sleepers sleeps more_sleeps sleeps
```

If you want to see the diagnosis output (aka `STDERR` stream output) while running, add the `--diagnosis` option (or `-d`).

> The diagnosis output will be interleaved with other diagnosis outputs from other commands. Anyway, the diagnosis output for each command will be logged in a separate file.
Expand Down
2 changes: 2 additions & 0 deletions src/Command/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DalaiLomo\ACE\Helper\FileHandler;
use DalaiLomo\ACE\Log\LogScanner;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -27,6 +28,7 @@ protected function configure()

$this->addOption('diagnosis', 'd', InputOption::VALUE_NONE, 'Show process diagnosis output while running.');
$this->addOption('key', 'k', InputOption::VALUE_REQUIRED, 'Key for groups to execute.');
$this->addArgument('groups', InputArgument::IS_ARRAY, 'Filter by groups.', []);
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand Down
36 changes: 33 additions & 3 deletions src/Config/ACEConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,41 @@ public function __construct($configFilePath)

public function onEachProcess($key, $groupName, \Closure $closure)
{
$this->throwExceptionOnInvalidKey($key);

foreach ($this->config[$key][self::COMMAND_GROUPS_KEY][$groupName] as $command) {
$closure($command);
}
}

public function onEachGroup($key, \Closure $closure)
public function onEachGroup($key, \Closure $closure, $onDemandGroups = [])
{
foreach ($this->config[$key][self::COMMAND_GROUPS_KEY] as $groupName => $commands) {
$closure($commands, $groupName);
$this->throwExceptionOnInvalidKey($key);

if (empty($onDemandGroups)) {
foreach ($this->config[$key][self::COMMAND_GROUPS_KEY] as $groupName => $commands) {
$closure($commands, $groupName);
}

return;
}

foreach ($onDemandGroups as $onDemandGroup) {
if (isset($this->config[$key][self::COMMAND_GROUPS_KEY][$onDemandGroup])) {
$closure($this->config[$key][self::COMMAND_GROUPS_KEY][$onDemandGroup], $onDemandGroup);
}
}
}

public function getGroup($key, $groupName)
{
$this->throwExceptionOnInvalidKey($key);

return isset($this->config[$key][self::COMMAND_GROUPS_KEY][$groupName])
? $this->config[$key][self::COMMAND_GROUPS_KEY][$groupName]
: null;
}

public function onEachKey(\Closure $closure)
{
foreach($this->config as $key => $groups) {
Expand All @@ -63,4 +86,11 @@ public function getKeywordsToHighlight($key)
? $this->config[$key][self::HIGHLIGHT_KEYWORDS_KEY]
: [];
}

private function throwExceptionOnInvalidKey($key)
{
if (false === isset($this->config[$key])) {
throw new \InvalidArgumentException("Invalid Key");
}
}
}
13 changes: 12 additions & 1 deletion src/Group/GroupExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function executeProcessGroups()

$this->config->onEachGroup($this->key, function($commandGroup, $groupName) {
$this->createAndRunProcessGroup($groupName);
});
}, $this->input->getArgument('groups'));

$endTime = microtime(true);

Expand Down Expand Up @@ -97,4 +97,15 @@ private function createAndRunProcessGroup($groupName)

$this->commandsOutput[$groupName] = $processGroup->getCommandsOutput();
}

private function shouldExecuteGroup($groupName)
{
$inputGroups = $this->input->getArgument('groups');

if (empty($inputGroups)) {
return true;
}

return in_array($groupName, $inputGroups);
}
}

0 comments on commit 557ab7a

Please sign in to comment.