multicall: migrate execution options to common code

This commit is contained in:
Ariadne Conill 2020-08-11 22:51:53 -06:00
parent 52509c508c
commit fdfe8ac080
6 changed files with 144 additions and 29 deletions

View file

@ -42,6 +42,7 @@ LIBIFUPDOWN_LIB = libifupdown.a
MULTICALL_SRC = \ MULTICALL_SRC = \
cmd/multicall.c \ cmd/multicall.c \
cmd/multicall-options.c \ cmd/multicall-options.c \
cmd/multicall-exec-options.c \
cmd/multicall-match-options.c cmd/multicall-match-options.c
MULTICALL_OBJ = ${MULTICALL_SRC:.c=.o} MULTICALL_OBJ = ${MULTICALL_SRC:.c=.o}
MULTICALL = ifupdown MULTICALL = ifupdown

View file

@ -21,11 +21,6 @@
#include "libifupdown/libifupdown.h" #include "libifupdown/libifupdown.h"
#include "cmd/multicall.h" #include "cmd/multicall.h"
static struct lif_execute_opts exec_opts = {
.interfaces_file = INTERFACES_FILE,
.executor_path = EXECUTOR_PATH
};
void void
print_interface(struct lif_interface *iface) print_interface(struct lif_interface *iface)
{ {
@ -250,18 +245,14 @@ ifquery_main(int argc, char *argv[])
struct lif_dict state = {}; struct lif_dict state = {};
struct lif_dict collection = {}; struct lif_dict collection = {};
struct option long_options[] = { struct option long_options[] = {
{"interfaces", required_argument, 0, 'i'},
{"list", no_argument, 0, 'L'}, {"list", no_argument, 0, 'L'},
{"pretty-print", no_argument, 0, 'P'}, {"pretty-print", no_argument, 0, 'P'},
{"state-file", required_argument, 0, 'S'},
{"state", no_argument, 0, 's'}, {"state", no_argument, 0, 's'},
{"dot", no_argument, 0, 'D'}, {"dot", no_argument, 0, 'D'},
{"property", required_argument, 0, 'p'}, {"property", required_argument, 0, 'p'},
{"executor-path", required_argument, 0, 'E'},
{NULL, 0, 0, 0} {NULL, 0, 0, 0}
}; };
bool listing = false, listing_stat = false; bool listing = false, listing_stat = false;
char *state_file = STATE_FILE;
for (;;) for (;;)
{ {
@ -270,18 +261,12 @@ ifquery_main(int argc, char *argv[])
break; break;
switch (c) { switch (c) {
case 'i':
exec_opts.interfaces_file = optarg;
break;
case 'L': case 'L':
listing = true; listing = true;
break; break;
case 'P': case 'P':
match_opts.pretty_print = true; match_opts.pretty_print = true;
break; break;
case 'S':
state_file = optarg;
break;
case 's': case 's':
listing_stat = true; listing_stat = true;
break; break;
@ -291,15 +276,12 @@ ifquery_main(int argc, char *argv[])
case 'p': case 'p':
match_opts.property = optarg; match_opts.property = optarg;
break; break;
case 'E':
exec_opts.executor_path = optarg;
break;
} }
} }
if (!lif_state_read_path(&state, state_file)) if (!lif_state_read_path(&state, exec_opts.state_file))
{ {
fprintf(stderr, "%s: could not parse %s\n", argv0, state_file); fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.state_file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -360,5 +342,5 @@ struct if_applet ifquery_applet = {
.desc = "query interface configuration", .desc = "query interface configuration",
.main = ifquery_main, .main = ifquery_main,
.usage = ifquery_usage, .usage = ifquery_usage,
.groups = { &global_option_group, &match_option_group, NULL }, .groups = { &global_option_group, &match_option_group, &exec_option_group },
}; };

View file

@ -25,11 +25,6 @@
#include "cmd/multicall.h" #include "cmd/multicall.h"
static bool up; static bool up;
static struct lif_execute_opts exec_opts = {
.executor_path = EXECUTOR_PATH,
.interfaces_file = INTERFACES_FILE,
.state_file = STATE_FILE,
};
void void
ifupdown_usage(int status) ifupdown_usage(int status)
@ -218,9 +213,6 @@ ifupdown_main(int argc, char *argv[])
case 'V': case 'V':
lif_common_version(); lif_common_version();
break; break;
case 'i':
exec_opts.interfaces_file = optarg;
break;
case 'a': case 'a':
match_opts.is_auto = true; match_opts.is_auto = true;
break; break;
@ -230,6 +222,9 @@ ifupdown_main(int argc, char *argv[])
case 'X': case 'X':
match_opts.exclude_pattern = optarg; match_opts.exclude_pattern = optarg;
break; break;
case 'i':
exec_opts.interfaces_file = optarg;
break;
case 'S': case 'S':
exec_opts.state_file = optarg; exec_opts.state_file = optarg;
break; break;

View file

@ -0,0 +1,77 @@
/*
* cmd/multicall-exec-options.c
* Purpose: multi-call binary frontend -- option handling
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#define _GNU_SOURCE
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "cmd/multicall.h"
struct lif_execute_opts exec_opts = {
.interfaces_file = INTERFACES_FILE,
.executor_path = EXECUTOR_PATH,
.state_file = STATE_FILE
};
static void handle_exec(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet)
{
(void) opt;
(void) applet;
switch (short_opt)
{
case 'i':
exec_opts.interfaces_file = opt_arg;
break;
case 'S':
exec_opts.state_file = opt_arg;
break;
case 'n':
exec_opts.mock = true;
exec_opts.verbose = true;
break;
case 'v':
exec_opts.verbose = true;
break;
case 'E':
exec_opts.executor_path = opt_arg;
break;
case 'f':
break;
case 'L':
exec_opts.no_lock = true;
break;
default:
break;
}
}
static struct if_option exec_options[] = {
{'f', "force", NULL, "force (de)configuration", true, handle_exec},
{'i', "interfaces", "interfaces FILE", "use FILE for interface definitions", true, handle_exec},
{'n', "no-act", NULL, "do not actually run any commands", false, handle_exec},
{'v', "verbose", NULL, "show what commands are being run", false, handle_exec},
{'E', "executor-path", "executor-path PATH", "use PATH for executor directory", true, handle_exec},
{'L', "no-lock", NULL, "do not use a lockfile to serialize state changes", false, handle_exec},
{'S', "state-file", "state-file FILE", "use FILE for state", true, handle_exec},
};
struct if_option_group exec_option_group = {
.desc = "Execution",
.group_size = ARRAY_SIZE(exec_options),
.group = exec_options
};

View file

@ -0,0 +1,57 @@
/*
* cmd/multicall-match-options.c
* Purpose: multi-call binary frontend -- option handling
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#define _GNU_SOURCE
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "cmd/multicall.h"
struct match_options match_opts = {};
static void handle_match(int short_opt, const struct if_option *opt, const char *opt_arg, const struct if_applet *applet)
{
(void) opt;
(void) applet;
switch (short_opt)
{
case 'a':
match_opts.is_auto = true;
break;
case 'I':
match_opts.include_pattern = opt_arg;
break;
case 'X':
match_opts.exclude_pattern = opt_arg;
break;
default:
break;
}
}
static struct if_option match_options[] = {
{'a', "auto", NULL, "only match against interfaces hinted as 'auto'", false, handle_match},
{'I', "include", "include PATTERN", "only match against interfaces matching PATTERN", true, handle_match},
{'X', "exclude", "exclude PATTERN", "never match against interfaces matching PATTERN", true, handle_match},
};
struct if_option_group match_option_group = {
.desc = "Matching interfaces",
.group_size = ARRAY_SIZE(match_options),
.group = match_options
};

View file

@ -67,4 +67,7 @@ extern const struct if_option *lookup_option(const struct if_applet *applet, int
extern struct if_option_group match_option_group; extern struct if_option_group match_option_group;
extern struct lif_execute_opts exec_opts;
extern struct if_option_group exec_option_group;
#endif #endif