system: pluginctl -s batch mode like -S #6582

Refactor the code to take advantage of the -S behaviour and
also ditch the extra service_control_status() since already
fetched the status ourselves and it's used nowhere else.
This commit is contained in:
Franco Fichtner 2023-05-30 15:08:53 +02:00
parent e454ed03bf
commit efcc7fcffa
2 changed files with 52 additions and 47 deletions

View File

@ -198,16 +198,6 @@ function service_control_get($name, $extras)
return $service;
}
function service_control_status($name, $extras)
{
$service = service_control_get($name, $extras);
if (!is_array($service)) {
return $service;
}
return htmlspecialchars(service_message($service));
}
function service_control_start($name, $extras)
{
$service = service_control_get($name, $extras);

View File

@ -63,6 +63,31 @@ function get_config_prop($path, $cnf = null)
}
}
function get_all_services($name = '', $id = '')
{
$services = [];
foreach (plugins_services() as $service) {
/* fail if name filter does not match */
if ($name != '' && $service['name'] != $name) {
continue;
}
/* fail if ID lookup is not possible or does not match */
if ($id !== '' && (!array_key_exists('id', $service) || $service['id'] != $id)) {
continue;
}
/* fetch status so the caller does not have to */
$service['status'] = service_message($service);
/* collect all matches contrary to what service_by_name() is doing */
$services[] = $service;
}
return $services;
}
$opts = getopt('cDdghIirSs', [], $optind);
$args = array_slice($argv, $optind);
@ -119,45 +144,35 @@ if (isset($opts['h'])) {
echo json_encode($result, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT) . PHP_EOL;
}
} elseif (isset($opts['S'])) {
$services = [];
foreach (plugins_services() as $service) {
/* fail if name filter does not match */
if (!empty($args[0]) && $service['name'] != $args[0]) {
continue;
}
/* fail if ID lookup is not possible or does not match */
if (isset($args[1]) && $args[1] !== '' && (!array_key_exists('id', $service) || $service['id'] != $args[1])) {
continue;
}
/* fetch status so the caller does not have to */
$service['status'] = service_message($service);
/* collect all matches contrary to what service_by_name() is doing */
$services[] = $service;
}
$services = get_all_services(isset($args[0]) ? $args[0] : '', isset($args[1]) ? $args[1] : '');
echo json_encode($services, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT) . PHP_EOL;
} elseif (isset($opts['s'])) {
$name = !empty($args[0]) ? $args[0] : '';
$act = !empty($args[1]) ? $args[1] : '';
$filter = [];
if (isset($args[2]) && $args !== '') {
$filter['id'] = $args[2];
}
switch ($act) {
case 'start':
echo service_control_start($name, $filter) . PHP_EOL;
break;
case 'stop':
echo service_control_stop($name, $filter) . PHP_EOL;
break;
case 'restart':
echo service_control_restart($name, $filter) . PHP_EOL;
break;
case 'status':
echo service_control_status($name, $filter) . PHP_EOL;
break;
default:
echo "Unknown command `$act'\n";
break;
$services = get_all_services(isset($args[0]) ? $args[0] : '', isset($args[2]) ? $args[2] : '');
foreach ($services as $service) {
$filter = isset($service['id']) ? ['id' => $service['id']] : [];
$act = isset($args[1]) ? $args[1] : '';
$name = $service['name'];
switch ($act) {
case 'start':
echo service_control_start($name, $filter) . PHP_EOL;
break;
case 'stop':
echo service_control_stop($name, $filter) . PHP_EOL;
break;
case 'restart':
echo service_control_restart($name, $filter) . PHP_EOL;
break;
case 'status':
echo htmlspecialchars($service['status']) . PHP_EOL;
break;
case 'status':
echo "Missing command\n";
break;
default:
echo "Unknown command `$act'\n";
break;
}
}
} elseif (empty($args[0])) {
// no arguments, list plugins of selected type