Skip to content

Commit

Permalink
Add support for groups on sync tasks
Browse files Browse the repository at this point in the history
Support composer 2 libs in compiler
  • Loading branch information
dave-redfern committed Oct 26, 2020
1 parent 5483c7e commit bb0d592
Show file tree
Hide file tree
Showing 30 changed files with 140 additions and 277 deletions.
2 changes: 2 additions & 0 deletions .idea/php.xml

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

6 changes: 6 additions & 0 deletions .idea/symfony2.xml

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

2 changes: 2 additions & 0 deletions .idea/syncit.iml

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

14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
],
"require": {
"ramsey/uuid": "~3.9",
"somnambulist/domain": "^2.0",
"symfony/console": "^4.2",
"symfony/dotenv": "^4.2",
"symfony/finder": "^4.2",
"symfony/process": "^4.2",
"symfony/var-dumper": "^4.2",
"symfony/yaml": "^4.2"
"somnambulist/domain": "^2.5",
"symfony/console": "^4.4",
"symfony/dotenv": "^4.4",
"symfony/finder": "^4.4",
"symfony/process": "^4.4",
"symfony/var-dumper": "^4.4",
"symfony/yaml": "^4.4"
},
"require-dev": {
},
Expand Down
33 changes: 12 additions & 21 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SyncIt has only been tested on macOS Mojave.
* phar archive
* support for .env files (including overrides)
* docker container resolution from a specified name
* groups to start/stop multiple tasks at the same time

## Setup

Expand All @@ -49,27 +50,6 @@ This is provided as-is without warranty of any kind. Use at your own risk!

__Caution:__ mis-configuring a mutagen session can cause serious data-loss!

### Lazy Install

__Caution:__ you use the following at your own risk! No responsibility is taken
if the script causes bad things to happen. You have been warned.

The following will download the current (at the time of writing) phar archive,
verify the SHA384 hash and copy the phar to `/usr/local/bin`, then symlink it to
`syncit` and verify it runs by calling `syncit --version`. The script has been
set up with verbose output.

```bash
curl --silent --fail --location --retry 3 --output /tmp/mutagen-sync-it.phar --url https://github.com/somnambulist-tech/sync-it/releases/download/1.0.0-beta3/mutagen-sync-it.phar \
&& echo "e18ebaf1d7b2166797c33a5149e82d8cb1e810b7c52823615d717b612c6159c3483983992814acd459cd560bf6c7952d /tmp/mutagen-sync-it.phar" | shasum -a 384 -c \
&& mv -v /tmp/mutagen-sync-it.phar /usr/local/bin/mutagen-sync-it.phar \
&& chmod -v 755 /usr/local/bin/mutagen-sync-it.phar \
&& ln -vf -s /usr/local/bin/mutagen-sync-it.phar /usr/local/bin/syncit \
&& syncit --ansi --version --no-interaction
```

If the hash check fails, remove the phar archive.

### Removing SyncIt

Remove any symlinks you have and delete the phar file. No other files are created
Expand Down Expand Up @@ -113,6 +93,9 @@ The above ensures that all syncs are created using `one-way-replica`. This
ensures that no changes are written back to the source but the target will
be overwritten.

__Note:__ that this will happily override your local in the source is set as a remote
and the target is a local folder. Be very careful!

## The Config File

The config file is split into 2 sections:
Expand Down Expand Up @@ -144,6 +127,9 @@ mutagen:
source_files:
source: "${PROJECT_DIR}"
target: "docker://container_1/app"
groups:
- group1
- group2
options:
sync-mode: one-way-replica
ignore:
Expand Down Expand Up @@ -214,6 +200,11 @@ This allows the task name to be used consistently regardless of mutagen version.
The target supports different transport mechanisms e.g. docker:// ssh:// etc.
Be sure to read the format / rules at: https://mutagen.io/documentation/transports/

Multiple group names can be set on each tasks under the `groups:` entry. This
will allow start/stop to work with all those tasks tagged with that group name.
For example: a web app with a JavaScript build pipeline may have many tasks. These
can now be grouped into app/build making it easier to start/stop in one go.

#### Docker Containers

When using docker; to make life easier you should use named containers with
Expand Down
4 changes: 1 addition & 3 deletions src/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands;

Expand Down
46 changes: 46 additions & 0 deletions src/Commands/Behaviours/GetLabelsFromInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

namespace SyncIt\Commands\Behaviours;

use Somnambulist\Collection\MutableCollection as Collection;
use Symfony\Component\Console\Input\InputInterface;
use SyncIt\Models\SyncTask;
use function count;
use function trim;

/**
* Trait GetLabelsFromInput
*
* @package SyncIt\Commands\Behaviours
* @subpackage SyncIt\Commands\Behaviours\GetLabelsFromInput
*/
trait GetLabelsFromInput
{

private function getLabelsFromInput(InputInterface $input, Collection $tasks): array
{
if (!$labels = $input->getOption('label')) {
if (!$labels = $input->getArgument('label')) {
return [];
}
}

if (count($labels) === 1) {
$label = trim($labels[0]);

if ('all' == $label) {
$labels = $tasks->keys()->toArray();
} elseif (!$tasks->keys()->contains($label)) {
$labels = $tasks
->filter(function (SyncTask $task) use ($label) {
return $task->getGroups()->contains($label);
})
->keys()
->toArray()
;
}
}

return $labels;
}
}
4 changes: 1 addition & 3 deletions src/Commands/Behaviours/ListConfiguredTasks.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands\Behaviours;

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/Behaviours/RunWrappedProcess.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands\Behaviours;

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/DebugCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands;

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/EnvParametersCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands;

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands;

Expand Down
4 changes: 1 addition & 3 deletions src/Commands/MonitorCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands;

Expand Down
30 changes: 6 additions & 24 deletions src/Commands/StartCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands;

Expand All @@ -9,6 +7,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use SyncIt\Commands\Behaviours\GetLabelsFromInput;
use SyncIt\Commands\Behaviours\ListConfiguredTasks;
use SyncIt\Commands\Behaviours\RunWrappedProcess;
use SyncIt\Models\SyncTask;
Expand All @@ -22,6 +21,7 @@
class StartCommand extends BaseCommand
{

use GetLabelsFromInput;
use ListConfiguredTasks;
use RunWrappedProcess;

Expand All @@ -30,8 +30,8 @@ protected function configure()
$this
->setName('start')
->setDescription('Starts the configured mutagen sync tasks')
->addArgument('label', InputArgument::IS_ARRAY|InputArgument::OPTIONAL, 'The labels to stop or all', [])
->addOption('label', 'l', InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'The task label(s) to start (mutagen >0.9.0)')
->addArgument('label', InputArgument::IS_ARRAY|InputArgument::OPTIONAL, 'The labels/group to start or all to start all', [])
->addOption('label', 'l', InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'The task label(s) to start or group name')
->addOption('list', null, InputOption::VALUE_NONE, 'List available tasks')
->setHelp(<<<'HELP'
Starts the specified, or all, configured sync tasks as defined in the current
Expand Down Expand Up @@ -116,32 +116,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

private function getLabelsFromInput(InputInterface $input, Collection $tasks): array
{
if (!$labels = $input->getOption('label')) {
if (!$labels = $input->getArgument('label')) {
return [];
}
}

if (isset($labels[0]) && 'all' === trim($labels[0])) {
$labels = $tasks->keys()->toArray();
}

return $labels;
}

private function buildStartCommand(SyncTask $task): Collection
{
$command = new Collection([
'mutagen', 'sync', 'create',
$task->getSource(),
$task->getTarget(),
]);

if ($this->getMutagen()->hasLabels()) {
$command->add(sprintf('--label="%s"', $task->getLabel()));
}
$command->add(sprintf('--label="%s"', $task->getLabel()));

$task->getOptions()->each(function ($value, $key) use ($command) {
if (is_null($value) && in_array($key, ['ignore-vcs', 'no-ignore-vcs'])) {
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/StatusCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace SyncIt\Commands;

Expand Down Expand Up @@ -44,20 +42,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->getMutagen()->getVersion()
)
)
->setHeaders(['Label', 'Identifier', 'Conn State', 'Sync Status'])
->setHeaders(['Label', 'Groups', 'Identifier', 'Conn State', 'Sync Status'])
;

$tasks->each(function (SyncTask $task) use ($table) {
if ($task->isRunning()) {
$table->addRow([
$task->getLabel(),
$task->getGroups()->implode(', '),
$task->getSession()->getId(),
$task->getSession()->getConnectionState(),
$task->getSession()->getStatus() ?? '--',
]);
} else {
$table->addRow([
$task->getLabel(),
$task->getGroups()->implode(', '),
'--',
'--',
'<comment>stopped</comment>',
Expand Down
Loading

0 comments on commit bb0d592

Please sign in to comment.