From efcc7fcffa4875af0c7ec96c30e58c353ee74be0 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Tue, 30 May 2023 15:08:53 +0200 Subject: [PATCH] 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. --- src/etc/inc/util.inc | 10 ----- src/sbin/pluginctl | 89 ++++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/src/etc/inc/util.inc b/src/etc/inc/util.inc index bc918e13b..2c283345e 100644 --- a/src/etc/inc/util.inc +++ b/src/etc/inc/util.inc @@ -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); diff --git a/src/sbin/pluginctl b/src/sbin/pluginctl index 5a2a42602..292f34d9a 100755 --- a/src/sbin/pluginctl +++ b/src/sbin/pluginctl @@ -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