add cli parameter -s to froxlor:api-call to show possible command-parameters

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2022-04-30 13:11:33 +02:00
parent 98c7c81fb8
commit 14c86d3d21
5 changed files with 58 additions and 65 deletions

View File

@@ -1,45 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can also view it online at
* https://files.froxlor.org/misc/COPYING.txt
*
* @copyright the authors
* @author Froxlor team <team@froxlor.org>
* @license https://files.froxlor.org/misc/COPYING.txt GPLv2
*/
namespace Froxlor\Api\Commands;
use Froxlor\Api\ApiCommand;
/**
* @since 0.10.0
*/
class ApiKeys extends ApiCommand
{
public function listing()
{
//
}
public function listingCount()
{
//
}
}

View File

@@ -296,6 +296,8 @@ class Froxlor extends ApiCommand
*
* @param string $module
* optional, return list of functions for a specific module
* @param string $function
* optional, return parameter information for a specific module and function
*
* @access admin, customer
* @return string json-encoded array
@@ -304,6 +306,7 @@ class Froxlor extends ApiCommand
public function listFunctions()
{
$module = $this->getParam('module', true, '');
$function = $this->getParam('function', true, '');
$functions = [];
if ($module != null) {
@@ -313,11 +316,13 @@ class Froxlor extends ApiCommand
$reflection = new ReflectionClass(__NAMESPACE__ . '\\' . $module);
$_functions = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($_functions as $func) {
if ($func->class == __NAMESPACE__ . '\\' . $module && $func->isPublic()) {
array_push($functions, array_merge([
'module' => $module,
'function' => $func->name
], $this->getParamListFromDoc($module, $func->name)));
if (empty($function) || ($function != null && $func->name == $function)) {
if ($func->class == __NAMESPACE__ . '\\' . $module && $func->isPublic()) {
array_push($functions, array_merge([
'module' => $module,
'function' => $func->name
], $this->getParamListFromDoc($module, $func->name)));
}
}
}
} else {

View File

@@ -29,7 +29,9 @@ use Exception;
use PDO;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Froxlor\Database\Database;
final class RunApiCommand extends CliCommand
@@ -42,6 +44,7 @@ final class RunApiCommand extends CliCommand
$this->addArgument('user', InputArgument::REQUIRED, 'Loginname of the user you want to run the command as')
->addArgument('api-command', InputArgument::REQUIRED, 'The command to execute in the form "Module.function"')
->addArgument('parameters', InputArgument::OPTIONAL, 'Paramaters to pass to the command as JSON array');
$this->addOption('show-params', 's', InputOption::VALUE_NONE, 'Show possible parameters for given api-command (given command will *not* be called)');
}
protected function execute(InputInterface $input, OutputInterface $output)
@@ -50,26 +53,54 @@ final class RunApiCommand extends CliCommand
$result = $this->validateRequirements($input, $output);
try {
$loginname = $input->getArgument('user');
$userinfo = $this->getUserByName($loginname);
$command = $input->getArgument('api-command');
$apicmd = $this->validateCommand($command);
$module = "\\Froxlor\\Api\\Commands\\" . $apicmd['class'];
$function = $apicmd['function'];
$params_json = $input->getArgument('parameters');
$params = json_decode($params_json ?? '', true);
$json_result = $module::getLocal($userinfo, $params)->{$function}();
$output->write($json_result);
$result = self::SUCCESS;
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</>');
$result = self::FAILURE;
if ($result == self::SUCCESS) {
try {
$loginname = $input->getArgument('user');
$userinfo = $this->getUserByName($loginname);
$command = $input->getArgument('api-command');
$apicmd = $this->validateCommand($command);
$module = "\\Froxlor\\Api\\Commands\\" . $apicmd['class'];
$function = $apicmd['function'];
if ($input->getOption('show-params') !== false) {
$json_result = \Froxlor\Api\Commands\Froxlor::getLocal($userinfo, ['module' => $apicmd['class'], 'function' => $function])->listFunctions();
$io = new SymfonyStyle($input, $output);
$result = $this->outputParamsList($json_result, $io);
} else {
$params_json = $input->getArgument('parameters');
$params = json_decode($params_json ?? '', true);
$json_result = $module::getLocal($userinfo, $params)->{$function}();
$output->write($json_result);
$result = self::SUCCESS;
}
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</>');
$result = self::FAILURE;
}
}
return $result;
}
private function outputParamsList(string $json, SymfonyStyle $io): int
{
$docs = json_decode($json, true);
$docs = array_shift($docs['data']);
if (!isset($docs['params'])) {
$io->warning(($docs['head'] ?? "unknown return"));
return self::INVALID;
}
if (empty($docs['params'])) {
$io->success("No parameters required");
} else {
$rows = [];
foreach ($docs['params'] as $param) {
$rows[] = [$param['type'], '<options=bold>'.$param['parameter'].'</>', $param['desc'] ?? ""];
}
$io->table(['Type', 'Name', 'Description'], $rows);
}
return self::SUCCESS;
}
private function validateCommand(string $command): array
{
$command = explode(".", $command);

View File

@@ -108,6 +108,7 @@ return [
'skipconfig' => 'Nicht (erneut) konfigurieren',
'recommendednote' => 'Empfohlene/benötigte Dienste anhand der aktuellen Systemeinstellungen',
'selectrecommended' => 'Empfohlene wählen',
'downloadselected' => 'Auswahl exportieren',
],
'templates' => [
'templates' => 'E-Mail-Vorlagen',

View File

@@ -110,6 +110,7 @@ return [
'skipconfig' => 'Don\'t (re)configure',
'recommendednote' => 'Recommended/required services based on current system settings',
'selectrecommended' => 'Select recommended',
'downloadselected' => 'Export selected',
],
'templates' => [
'templates' => 'Email-templates',