|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Storeman\Cli\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Storeman\Storeman; |
|
6
|
|
|
use Storeman\Cli\SynchronizationProgressListener; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class SynchronizeCommand extends AbstractCommand |
|
12
|
|
|
{ |
|
13
|
|
|
protected function configure() |
|
14
|
|
|
{ |
|
15
|
|
|
parent::configure(); |
|
16
|
|
|
|
|
17
|
|
|
$this->setName('sync'); |
|
18
|
|
|
$this->setDescription('Synchronizes the local state with the vault state.'); |
|
19
|
|
|
$this->addOption('vaults', null, InputOption::VALUE_REQUIRED, 'Comma-separated list of vault titles to synchronize with. Defaults to all configured.'); |
|
20
|
|
|
$this->addOption('prefer-local', null, InputOption::VALUE_NONE, 'Always prefers local changes over remote changes.'); |
|
21
|
|
|
$this->addOption('prefer-remote', null, InputOption::VALUE_NONE, 'Always prefers remote changes over local changes.'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int |
|
25
|
|
|
{ |
|
26
|
|
|
$vaultTitles = $input->getOption('vaults') ? explode(',', $input->getOption('vaults')) : []; |
|
27
|
|
|
$preferLocal = $input->getOption('prefer-local'); |
|
28
|
|
|
$preferRemote = $input->getOption('prefer-remote'); |
|
29
|
|
|
|
|
30
|
|
|
if ($preferLocal && $preferRemote) |
|
31
|
|
|
{ |
|
32
|
|
|
$output->writeln('<error>Only one of --prefer-local and --prefer-remote options can be used at the same time.</error>'); |
|
33
|
|
|
|
|
34
|
|
|
return 1; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$configuration = $storeman->getConfiguration(); |
|
38
|
|
|
|
|
39
|
|
|
if ($preferLocal) |
|
40
|
|
|
{ |
|
41
|
|
|
foreach ($configuration->getVaults() as $vaultConfiguration) |
|
42
|
|
|
{ |
|
43
|
|
|
$vaultConfiguration->setConflictHandler('preferLocal'); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
elseif ($preferRemote) |
|
47
|
|
|
{ |
|
48
|
|
|
foreach ($configuration->getVaults() as $vaultConfiguration) |
|
49
|
|
|
{ |
|
50
|
|
|
$vaultConfiguration->setConflictHandler('preferRemote'); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$storeman->synchronize($vaultTitles, new SynchronizationProgressListener($output)); |
|
55
|
|
|
|
|
56
|
|
|
$output->writeln(PHP_EOL . '<info>Done!</info>'); |
|
57
|
|
|
|
|
58
|
|
|
return 0; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|