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 82f7cb5 commit f2f6c1f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/agent/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const struct option options[] = {
{ NULL, 0, 0, '\0' }
};

#define OPTIONS_STR \
#define GETOPT_OPTSTRING \
ARG_PORT_SHORT_S ARG_HOST_SHORT_S ARG_ADDRESS_SHORT_S ARG_HELP_SHORT_S ARG_CONFIG_SHORT_S \
ARG_NAME_SHORT_S ARG_USER_SHORT_S ARG_HEARTBEAT_INTERVAL_SHORT_S ARG_VERSION_SHORT_S

Expand Down Expand Up @@ -72,7 +72,7 @@ static void usage(char *argv[]) {
static int get_opts(int argc, char *argv[]) {
int opt = 0;

while ((opt = getopt_long(argc, argv, OPTIONS_STR, options, NULL)) != -1) {
while ((opt = getopt_long(argc, argv, GETOPT_OPTSTRING, options, NULL)) != -1) {
switch (opt) {
case ARG_HELP_SHORT:
usage(argv);
Expand Down
57 changes: 33 additions & 24 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 GETOPT_OPTSTRING 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 All @@ -63,7 +72,7 @@ static void usage() {
static int parse_cli_opts(int argc, char *argv[], Command *command) {
int opt = 0;

while ((opt = getopt_long(argc, argv, OPTIONS_STR, getopt_options, NULL)) != -1) {
while ((opt = getopt_long(argc, argv, GETOPT_OPTSTRING, getopt_options, NULL)) != -1) {
if (opt == ARG_HELP_SHORT) {
command->is_help = true;
} else if (opt == GETOPT_UNKNOWN_OPTION) {
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 '?'
4 changes: 2 additions & 2 deletions src/manager/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const struct option options[] = {
{ NULL, 0, 0, '\0' }
};

#define OPTIONS_STR ARG_PORT_SHORT_S ARG_HELP_SHORT_S ARG_CONFIG_SHORT_S ARG_VERSION_SHORT_S
#define GETOPT_OPTSTRING ARG_PORT_SHORT_S ARG_HELP_SHORT_S ARG_CONFIG_SHORT_S ARG_VERSION_SHORT_S

static const char *opt_port = 0;
static const char *opt_config = NULL;
Expand All @@ -44,7 +44,7 @@ static void usage(char *argv[]) {
static int get_opts(int argc, char *argv[]) {
int opt = 0;

while ((opt = getopt_long(argc, argv, OPTIONS_STR, options, NULL)) != -1) {
while ((opt = getopt_long(argc, argv, GETOPT_OPTSTRING, options, NULL)) != -1) {
switch (opt) {
case ARG_HELP_SHORT:
usage(argv);
Expand Down
4 changes: 2 additions & 2 deletions src/proxy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const struct option options[] = {
{ NULL, 0, 0, '\0' }
};

#define OPTIONS_STR ARG_HELP_SHORT_S ARG_USER_SHORT_S ARG_VERSION_SHORT_S
#define GETOPT_OPTSTRING ARG_HELP_SHORT_S ARG_USER_SHORT_S ARG_VERSION_SHORT_S

static const char *opt_node_unit = NULL;
static const char *opt_operation = NULL;
Expand Down Expand Up @@ -71,7 +71,7 @@ int parse_node_unit_opt(const char *opt_node_unit, char **ret_node_name, char **

static int get_opts(int argc, char *argv[]) {
int opt = 0;
while ((opt = getopt_long(argc, argv, OPTIONS_STR, options, NULL)) != -1) {
while ((opt = getopt_long(argc, argv, GETOPT_OPTSTRING, options, NULL)) != -1) {
switch (opt) {
case ARG_HELP_SHORT:
usage(argv);
Expand Down

0 comments on commit f2f6c1f

Please sign in to comment.