1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Archivr\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Archivr\Configuration; |
6
|
|
|
use Archivr\ConfigurationFileWriter; |
7
|
|
|
use Archivr\ConflictHandler\ConflictHandlerFactory; |
8
|
|
|
use Archivr\IndexMerger\IndexMergerFactory; |
9
|
|
|
use Archivr\LockAdapter\LockAdapterFactory; |
10
|
|
|
use Archivr\OperationListBuilder\OperationListBuilderFactory; |
11
|
|
|
use Archivr\StorageDriver\StorageDriverFactory; |
12
|
|
|
use Archivr\PathUtils; |
13
|
|
|
use Archivr\VaultConfiguration; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class InitCommand extends AbstractCommand |
20
|
|
|
{ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
parent::configure(); |
24
|
|
|
|
25
|
|
|
$this->setName('init'); |
26
|
|
|
$this->setDescription('Sets up a local archive copy.'); |
27
|
|
|
$this->addArgument('target', InputArgument::OPTIONAL, 'Local target path to write the configuration file to.', './archivr.json'); |
28
|
|
|
$this->addOption('path', null, InputOption::VALUE_REQUIRED, 'Path to use as local path for the archive.'); |
29
|
|
|
$this->addOption('identity', 'i', InputOption::VALUE_REQUIRED, 'Identity to be used.'); |
30
|
|
|
$this->addOption('exclude', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Relative path exclusion(s).'); |
31
|
|
|
$this->addOption('writeDefaults', null, InputOption::VALUE_NONE, 'Forces writing of default values which are omitted as a default.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
38
|
|
|
{ |
39
|
|
|
parent::execute($input, $output); |
40
|
|
|
|
41
|
|
|
$configFilePath = PathUtils::getAbsolutePath($input->getArgument('target')); |
42
|
|
|
$configFileDir = dirname($configFilePath); |
43
|
|
|
|
44
|
|
|
if (!is_writable($configFileDir)) |
45
|
|
|
{ |
46
|
|
|
$output->writeln("<error>Cannot write to {$configFileDir}</error>"); |
47
|
|
|
|
48
|
|
|
return 1; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
$configuration = new Configuration($input->getOption('path') ?: $this->outputStyle->ask('Local path', '.')); |
53
|
|
|
$configuration->setIdentity($input->getOption('identity') ?: $this->outputStyle->ask('Identity', get_current_user())); |
54
|
|
|
$configuration->setExclude($input->getOption('exclude') ?: $this->outputStyle->askMultiple('Excluded path(s)')); |
55
|
|
|
|
56
|
|
|
// at least one storage driver has to be set up |
57
|
|
|
do |
58
|
|
|
{ |
59
|
|
|
$vaultConfig = new VaultConfiguration($this->outputStyle->choice('Storage driver', StorageDriverFactory::getProvidedServiceNames())); |
60
|
|
|
$vaultConfig->setTitle($this->outputStyle->ask('Title', $vaultConfig->getAdapter())); |
61
|
|
|
$vaultConfig->setLockAdapter($this->outputStyle->choice('Lock adapter', LockAdapterFactory::getProvidedServiceNames(), $vaultConfig->getLockAdapter())); |
62
|
|
|
$vaultConfig->setIndexMerger($this->outputStyle->choice('Index merger', IndexMergerFactory::getProvidedServiceNames(), $vaultConfig->getIndexMerger())); |
63
|
|
|
$vaultConfig->setConflictHandler($this->outputStyle->choice('Conflict handler', ConflictHandlerFactory::getProvidedServiceNames(), $vaultConfig->getConflictHandler())); |
64
|
|
|
$vaultConfig->setOperationListBuilder($this->outputStyle->choice('Operation list builder', OperationListBuilderFactory::getProvidedServiceNames(), $vaultConfig->getOperationListBuilder())); |
65
|
|
|
|
66
|
|
|
while ($settingName = $this->outputStyle->ask('Additional setting name')) |
67
|
|
|
{ |
68
|
|
|
if ($settingValue = $this->outputStyle->ask('Additional setting value')) |
69
|
|
|
{ |
70
|
|
|
$vaultConfig->setSetting($settingName, $settingValue); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$configuration->addVault($vaultConfig); |
75
|
|
|
} |
76
|
|
|
while($this->outputStyle->choice('Add another vault?', ['y', 'n'], 'n') === 'y'); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
$skipDefaults = !$input->getOption('writeDefaults'); |
80
|
|
|
|
81
|
|
|
$configurationFileWriter = new ConfigurationFileWriter(); |
82
|
|
|
|
83
|
|
|
$output->writeln("The following content will be written to {$configFilePath}:"); |
84
|
|
|
$output->writeln($configurationFileWriter->buildConfigurationFile($configuration, $skipDefaults)); |
85
|
|
|
|
86
|
|
|
if ($this->outputStyle->confirm('Continue? ', true)) |
87
|
|
|
{ |
88
|
|
|
$configurationFileWriter->writeConfigurationFile($configuration, $configFilePath, $skipDefaults); |
89
|
|
|
|
90
|
|
|
$output->writeln("<info>Successfully written config file to {$configFilePath}</info>"); |
91
|
|
|
} |
92
|
|
|
else |
93
|
|
|
{ |
94
|
|
|
$output->writeln("<comment>Aborted</comment>"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return 0; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|