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:
@@ -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()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -296,6 +296,8 @@ class Froxlor extends ApiCommand
|
|||||||
*
|
*
|
||||||
* @param string $module
|
* @param string $module
|
||||||
* optional, return list of functions for a specific 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
|
* @access admin, customer
|
||||||
* @return string json-encoded array
|
* @return string json-encoded array
|
||||||
@@ -304,6 +306,7 @@ class Froxlor extends ApiCommand
|
|||||||
public function listFunctions()
|
public function listFunctions()
|
||||||
{
|
{
|
||||||
$module = $this->getParam('module', true, '');
|
$module = $this->getParam('module', true, '');
|
||||||
|
$function = $this->getParam('function', true, '');
|
||||||
|
|
||||||
$functions = [];
|
$functions = [];
|
||||||
if ($module != null) {
|
if ($module != null) {
|
||||||
@@ -313,11 +316,13 @@ class Froxlor extends ApiCommand
|
|||||||
$reflection = new ReflectionClass(__NAMESPACE__ . '\\' . $module);
|
$reflection = new ReflectionClass(__NAMESPACE__ . '\\' . $module);
|
||||||
$_functions = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
|
$_functions = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||||
foreach ($_functions as $func) {
|
foreach ($_functions as $func) {
|
||||||
if ($func->class == __NAMESPACE__ . '\\' . $module && $func->isPublic()) {
|
if (empty($function) || ($function != null && $func->name == $function)) {
|
||||||
array_push($functions, array_merge([
|
if ($func->class == __NAMESPACE__ . '\\' . $module && $func->isPublic()) {
|
||||||
'module' => $module,
|
array_push($functions, array_merge([
|
||||||
'function' => $func->name
|
'module' => $module,
|
||||||
], $this->getParamListFromDoc($module, $func->name)));
|
'function' => $func->name
|
||||||
|
], $this->getParamListFromDoc($module, $func->name)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -29,7 +29,9 @@ use Exception;
|
|||||||
use PDO;
|
use PDO;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
use Froxlor\Database\Database;
|
use Froxlor\Database\Database;
|
||||||
|
|
||||||
final class RunApiCommand extends CliCommand
|
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')
|
$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('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');
|
->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)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
@@ -50,26 +53,54 @@ final class RunApiCommand extends CliCommand
|
|||||||
|
|
||||||
$result = $this->validateRequirements($input, $output);
|
$result = $this->validateRequirements($input, $output);
|
||||||
|
|
||||||
try {
|
if ($result == self::SUCCESS) {
|
||||||
$loginname = $input->getArgument('user');
|
try {
|
||||||
$userinfo = $this->getUserByName($loginname);
|
$loginname = $input->getArgument('user');
|
||||||
$command = $input->getArgument('api-command');
|
$userinfo = $this->getUserByName($loginname);
|
||||||
$apicmd = $this->validateCommand($command);
|
$command = $input->getArgument('api-command');
|
||||||
$module = "\\Froxlor\\Api\\Commands\\" . $apicmd['class'];
|
$apicmd = $this->validateCommand($command);
|
||||||
$function = $apicmd['function'];
|
$module = "\\Froxlor\\Api\\Commands\\" . $apicmd['class'];
|
||||||
$params_json = $input->getArgument('parameters');
|
$function = $apicmd['function'];
|
||||||
$params = json_decode($params_json ?? '', true);
|
if ($input->getOption('show-params') !== false) {
|
||||||
$json_result = $module::getLocal($userinfo, $params)->{$function}();
|
$json_result = \Froxlor\Api\Commands\Froxlor::getLocal($userinfo, ['module' => $apicmd['class'], 'function' => $function])->listFunctions();
|
||||||
$output->write($json_result);
|
$io = new SymfonyStyle($input, $output);
|
||||||
$result = self::SUCCESS;
|
$result = $this->outputParamsList($json_result, $io);
|
||||||
} catch (Exception $e) {
|
} else {
|
||||||
$output->writeln('<error>' . $e->getMessage() . '</>');
|
$params_json = $input->getArgument('parameters');
|
||||||
$result = self::FAILURE;
|
$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;
|
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
|
private function validateCommand(string $command): array
|
||||||
{
|
{
|
||||||
$command = explode(".", $command);
|
$command = explode(".", $command);
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ return [
|
|||||||
'skipconfig' => 'Nicht (erneut) konfigurieren',
|
'skipconfig' => 'Nicht (erneut) konfigurieren',
|
||||||
'recommendednote' => 'Empfohlene/benötigte Dienste anhand der aktuellen Systemeinstellungen',
|
'recommendednote' => 'Empfohlene/benötigte Dienste anhand der aktuellen Systemeinstellungen',
|
||||||
'selectrecommended' => 'Empfohlene wählen',
|
'selectrecommended' => 'Empfohlene wählen',
|
||||||
|
'downloadselected' => 'Auswahl exportieren',
|
||||||
],
|
],
|
||||||
'templates' => [
|
'templates' => [
|
||||||
'templates' => 'E-Mail-Vorlagen',
|
'templates' => 'E-Mail-Vorlagen',
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ return [
|
|||||||
'skipconfig' => 'Don\'t (re)configure',
|
'skipconfig' => 'Don\'t (re)configure',
|
||||||
'recommendednote' => 'Recommended/required services based on current system settings',
|
'recommendednote' => 'Recommended/required services based on current system settings',
|
||||||
'selectrecommended' => 'Select recommended',
|
'selectrecommended' => 'Select recommended',
|
||||||
|
'downloadselected' => 'Export selected',
|
||||||
],
|
],
|
||||||
'templates' => [
|
'templates' => [
|
||||||
'templates' => 'Email-templates',
|
'templates' => 'Email-templates',
|
||||||
|
|||||||
Reference in New Issue
Block a user