From 35b6fbd4261b591ee65400288f29e7d2b6c88237 Mon Sep 17 00:00:00 2001 From: Mark Kemel Date: Mon, 8 Jan 2024 11:59:08 +0200 Subject: [PATCH] bluechictl: add flags to enable and disable commands Adding the following flags: enable: `--force`, `--runtime` and `--no-reload` disable: `--runtime` and `--no-reload` Removed the short `-f` option for `--filter` option, since `-f` is more common for --force than for filter Signed-off-by: Mark Kemel --- src/client/main.c | 55 +++++++++++++++++++------------- src/client/method-unit-actions.c | 25 ++++++++++++--- src/libbluechi/common/opt.h | 13 ++++++-- 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/src/client/main.c b/src/client/main.c index a041a1124a..7c3afa4db3 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -18,6 +18,9 @@ #define OPT_NONE 0u #define OPT_HELP 1u << 0u #define OPT_FILTER 1u << 1u +#define OPT_FORCE 1u << 2u +#define OPT_RUNTIME 1u << 3u +#define OPT_NO_RELOAD 1u << 4u int method_version(UNUSED Command *command, UNUSED void *userdata) { printf("bluechictl version %s\n", CONFIG_H_BC_VERSION); @@ -25,35 +28,41 @@ int method_version(UNUSED Command *command, UNUSED void *userdata) { } const Method methods[] = { - {"help", 0, 0, OPT_NONE, method_help, usage_bluechi}, - { "list-units", 0, 1, OPT_FILTER, method_list_units, usage_bluechi}, - { "start", 2, 2, OPT_NONE, method_start, usage_bluechi}, - { "stop", 2, 2, OPT_NONE, method_stop, usage_bluechi}, - { "freeze", 2, 2, OPT_NONE, method_freeze, usage_bluechi}, - { "thaw", 2, 2, OPT_NONE, method_thaw, usage_bluechi}, - { "restart", 2, 2, OPT_NONE, method_restart, usage_bluechi}, - { "reload", 2, 2, OPT_NONE, method_reload, usage_bluechi}, - { "monitor", 0, 2, OPT_NONE, method_monitor, usage_bluechi}, - { "metrics", 1, 1, OPT_NONE, method_metrics, usage_bluechi}, - { "enable", 2, ARG_ANY, OPT_NONE, method_enable, usage_bluechi}, - { "disable", 2, ARG_ANY, OPT_NONE, method_disable, usage_bluechi}, - { "daemon-reload", 1, 1, OPT_NONE, method_daemon_reload, usage_bluechi}, - { "status", 2, ARG_ANY, OPT_NONE, method_status, usage_bluechi}, - { "set-loglevel", 1, 2, OPT_NONE, method_set_loglevel, usage_bluechi}, - { "version", 0, 0, OPT_NONE, method_version, usage_bluechi}, - { NULL, 0, 0, 0, NULL, NULL } + {"help", 0, 0, OPT_NONE, method_help, usage_bluechi}, + { "list-units", 0, 1, OPT_FILTER, method_list_units, usage_bluechi}, + { "start", 2, 2, OPT_NONE, method_start, usage_bluechi}, + { "stop", 2, 2, OPT_NONE, method_stop, usage_bluechi}, + { "freeze", 2, 2, OPT_NONE, method_freeze, usage_bluechi}, + { "thaw", 2, 2, OPT_NONE, method_thaw, usage_bluechi}, + { "restart", 2, 2, OPT_NONE, method_restart, usage_bluechi}, + { "reload", 2, 2, OPT_NONE, method_reload, usage_bluechi}, + { "monitor", 0, 2, OPT_NONE, method_monitor, usage_bluechi}, + { "metrics", 1, 1, OPT_NONE, method_metrics, usage_bluechi}, + { "enable", 2, ARG_ANY, OPT_FORCE | OPT_RUNTIME | OPT_NO_RELOAD, method_enable, usage_bluechi}, + { "disable", 2, ARG_ANY, OPT_NONE, method_disable, usage_bluechi}, + { "daemon-reload", 1, 1, OPT_NONE, method_daemon_reload, usage_bluechi}, + { "status", 2, ARG_ANY, OPT_NONE, method_status, usage_bluechi}, + { "set-loglevel", 1, 2, OPT_NONE, method_set_loglevel, usage_bluechi}, + { "version", 0, 0, OPT_NONE, method_version, usage_bluechi}, + { NULL, 0, 0, 0, NULL, NULL } }; const OptionType option_types[] = { - {ARG_FILTER_SHORT, ARG_FILTER, OPT_FILTER}, - { 0, NULL, 0 } + {ARG_FILTER_SHORT, ARG_FILTER, OPT_FILTER }, + { ARG_FORCE_SHORT, ARG_FORCE, OPT_FORCE }, + { ARG_RUNTIME_SHORT, ARG_RUNTIME, OPT_RUNTIME }, + { ARG_NO_RELOAD_SHORT, ARG_NO_RELOAD, OPT_NO_RELOAD}, + { 0, NULL, 0 } }; -#define OPTIONS_STR ARG_HELP_SHORT_S ARG_FILTER_SHORT_S +#define OPTIONS_STR ARG_HELP_SHORT_S ARG_FORCE_SHORT_S const struct option getopt_options[] = { - {ARG_HELP, no_argument, 0, ARG_HELP_SHORT }, - { ARG_FILTER, required_argument, 0, ARG_FILTER_SHORT}, - { NULL, 0, 0, '\0' } + {ARG_HELP, no_argument, 0, ARG_HELP_SHORT }, + { ARG_FILTER, required_argument, 0, ARG_FILTER_SHORT }, + { ARG_FORCE, no_argument, 0, ARG_FORCE_SHORT }, + { ARG_RUNTIME, no_argument, 0, ARG_RUNTIME_SHORT }, + { ARG_NO_RELOAD, no_argument, 0, ARG_NO_RELOAD_SHORT}, + { NULL, 0, 0, '\0' } }; static void usage() { diff --git a/src/client/method-unit-actions.c b/src/client/method-unit-actions.c index 82a94fae95..2b78f30a9a 100644 --- a/src/client/method-unit-actions.c +++ b/src/client/method-unit-actions.c @@ -2,6 +2,8 @@ #include "method-unit-actions.h" #include "client.h" +#include "libbluechi/common/opt.h" + static int method_lifecycle_action_on(Client *client, char *node_name, char *unit, char *method) { _cleanup_sd_bus_error_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_sd_bus_message_ sd_bus_message *message = NULL; @@ -328,13 +330,22 @@ int method_thaw(Command *command, void *userdata) { int method_enable(Command *command, void *userdata) { int r = 0; - r = method_enable_unit_on(userdata, command->opargv[0], &command->opargv[1], command->opargc - 1, 0, 0); + r = method_enable_unit_on( + userdata, + command->opargv[0], + &command->opargv[1], + command->opargc - 1, + command_flag_exists(command, ARG_RUNTIME_SHORT), + command_flag_exists(command, ARG_FORCE_SHORT)); if (r < 0) { fprintf(stderr, "Failed to enable the units on node [%s] - %s", command->opargv[0], strerror(-r)); - } else { + return r; + } + + if (!command_flag_exists(command, ARG_NO_RELOAD_SHORT)) { r = method_daemon_reload_on(userdata, command->opargv[0]); } @@ -343,13 +354,19 @@ int method_enable(Command *command, void *userdata) { int method_disable(Command *command, void *userdata) { int r = 0; - r = method_disable_unit_on(userdata, command->opargv[0], &command->opargv[1], command->opargc - 1, 0); + r = method_disable_unit_on( + userdata, + command->opargv[0], + &command->opargv[1], + command->opargc - 1, + command_flag_exists(command, ARG_RUNTIME_SHORT)); if (r < 0) { fprintf(stderr, "Failed to disable the units on node [%s] - %s", command->opargv[0], strerror(-r)); - } else { + } + if (!command_flag_exists(command, ARG_NO_RELOAD_SHORT)) { r = method_daemon_reload_on(userdata, command->opargv[0]); } diff --git a/src/libbluechi/common/opt.h b/src/libbluechi/common/opt.h index 94ed276a80..459f8b59cf 100644 --- a/src/libbluechi/common/opt.h +++ b/src/libbluechi/common/opt.h @@ -30,8 +30,7 @@ #define ARG_NAME_SHORT_S "n:" #define ARG_FILTER "filter" -#define ARG_FILTER_SHORT 'f' -#define ARG_FILTER_SHORT_S "f:" +#define ARG_FILTER_SHORT 1000 #define ARG_HELP "help" #define ARG_HELP_SHORT 'h' @@ -41,4 +40,14 @@ #define ARG_VERSION_SHORT 'v' #define ARG_VERSION_SHORT_S "v" +#define ARG_FORCE "force" +#define ARG_FORCE_SHORT 'f' +#define ARG_FORCE_SHORT_S "f" + +#define ARG_RUNTIME "runtime" +#define ARG_RUNTIME_SHORT 1001 + +#define ARG_NO_RELOAD "no-reload" +#define ARG_NO_RELOAD_SHORT 1002 + #define GETOPT_UNKNOWN_OPTION '?'