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