setName("api:catalog") ->setDescription("Manage the API catalogs") ->addOption("create", "c", InputOption::VALUE_REQUIRED, "Create a new catalog") ->addOption("remove", "r", InputOption::VALUE_REQUIRED, "Remove a catalog") ->addOption("set-props", null, InputOption::VALUE_REQUIRED, "Apply properties to a catalog") ->addArgument("properties", InputArgument::IS_ARRAY, "Default properties for the catalog") ->addOption("list", null, InputOption::VALUE_NONE, "Only list catalogs, not methods") ; } protected function execute(InputInterface $input, OutputInterface $output) { $api = get_plugin('com.noccy.apiclient'); $list = $input->getOption("list"); $dest = get_environment()->getConfigDirectory() . "/api/catalogs"; if ($create = $input->getOption("create")) { if (file_exists($dest."/".$create.".json")) { $output->writeln("Catalog {$create} already exists!"); return Command::FAILURE; } $catalog = new Catalog([ 'catalog' => [ 'name' => $create ] ]); file_put_contents($dest."/".$create.".json", json_encode($catalog, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); $output->writeln("Created new catalog {$create}"); return Command::SUCCESS; } if ($remove = $input->getOption("remove")) { if (!file_exists($dest."/".$remove.".json")) { $output->writeln("Catalog {$remove} does not exist!"); return Command::FAILURE; } unlink($dest."/".$remove.".json"); $output->writeln("Removed catalog {$remove}"); return Command::SUCCESS; } if ($setprops = $input->getOption("set-props")) { $proparr = []; $props = $input->getArgument("properties"); foreach ($props as $str) { if (!str_contains($str,"=")) { $output->writeln("Ignoring parameter argument '{$str}'"); } else { [$k,$v] = explode("=",$str,2); $proparr[$k] = $v; } } $catalog = $api->getCatalog($setprops); $catalog->applyProperties($proparr); $api->saveCatalog($catalog); $output->writeln("Updated properties on catalog {$setprops}"); return Command::SUCCESS; } $catalogs = $api->getCatalogNames(); foreach ($catalogs as $catalog) { $c = $api->getCatalog($catalog); if ($list) { $output->writeln($catalog); } else { $output->writeln("\u{25e9} {$catalog}: {$c->getInfo()}"); $ms = $c->getMethods(); foreach ($ms as $name=>$m) { $last = ($m === end($ms)); $output->writeln(($last?"\u{2514}\u{2500}":"\u{251c}\u{2500}")."\u{25a2} {$catalog}.{$name}: {$m->getInfo()}"); } } } return Command::SUCCESS; } }