Skip to content

Commit

Permalink
bluechictl: add flags to enable and disable commands
Browse files Browse the repository at this point in the history
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 <mkemel@redhat.com>
  • Loading branch information
mkemel committed Jan 8, 2024
1 parent ab78631 commit 35b6fbd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
55 changes: 32 additions & 23 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,51 @@
#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);
return 0;
}

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() {
Expand Down
25 changes: 21 additions & 4 deletions src/client/method-unit-actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}

Expand All @@ -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]);
}

Expand Down
13 changes: 11 additions & 2 deletions src/libbluechi/common/opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 '?'

0 comments on commit 35b6fbd

Please sign in to comment.