Skip to content

Commit 9069cdb

Browse files
npotierjames2001
andauthored
allow user to recreate only one index (acseo#98) (acseo#99)
Co-authored-by: Rathgeber Stéphane <stephane@kiora.tech>
1 parent 4f800a8 commit 9069cdb

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Command/CreateCommand.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use ACSEO\TypesenseBundle\Manager\CollectionManager;
88
use Symfony\Component\Console\Command\Command;
99
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
1011
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Style\SymfonyStyle;
1113

1214
class CreateCommand extends Command
1315
{
@@ -24,16 +26,33 @@ protected function configure()
2426
{
2527
$this
2628
->setName(self::$defaultName)
29+
->addOption('indexes', null, InputOption::VALUE_OPTIONAL, 'The index(es) to repopulate. Comma separated values')
2730
->setDescription('Create Typsenses indexes')
2831

2932
;
3033
}
3134

3235
protected function execute(InputInterface $input, OutputInterface $output): int
3336
{
34-
$defs = $this->collectionManager->getCollectionDefinitions();
37+
$io = new SymfonyStyle($input, $output);
3538

36-
foreach ($defs as $name => $def) {
39+
$collectionDefinitions = $this->collectionManager->getCollectionDefinitions();
40+
$indexes = (null !== $indexes = $input->getOption('indexes')) ? explode(',', $indexes) : \array_keys($collectionDefinitions);
41+
42+
foreach ($indexes as $index) {
43+
if (!isset($collectionDefinitions[$index])) {
44+
$io->error('Unable to find index "'.$index.'" in collection definition (available : '.implode(', ', array_keys($collectionDefinitions)).')');
45+
46+
return 2;
47+
}
48+
}
49+
50+
// filter collection definitions
51+
$collectionDefinitions = array_filter($collectionDefinitions, function ($key) use ($indexes) {
52+
return \in_array($key, $indexes, true);
53+
}, ARRAY_FILTER_USE_KEY);
54+
55+
foreach ($collectionDefinitions as $name => $def) {
3756
$name = $def['name'];
3857
$typesenseName = $def['typesense_name'];
3958
try {

0 commit comments

Comments
 (0)