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