Split swanctl --raw mode into single-line and --pretty mode
This commit is contained in:
@@ -185,7 +185,7 @@ static void add_sections(vici_req_t *req, settings_t *cfg, char *section)
|
||||
* Load an IKE_SA config with CHILD_SA configs from a section
|
||||
*/
|
||||
static bool load_conn(vici_conn_t *conn, settings_t *cfg,
|
||||
char *section, bool raw)
|
||||
char *section, command_format_options_t format)
|
||||
{
|
||||
vici_req_t *req;
|
||||
vici_res_t *res;
|
||||
@@ -207,9 +207,10 @@ static bool load_conn(vici_conn_t *conn, settings_t *cfg,
|
||||
fprintf(stderr, "load-conn request failed: %s\n", strerror(errno));
|
||||
return FALSE;
|
||||
}
|
||||
if (raw)
|
||||
if (format & COMMAND_FORMAT_RAW)
|
||||
{
|
||||
vici_dump(res, "load-conn reply", stdout);
|
||||
vici_dump(res, "load-conn reply", format & COMMAND_FORMAT_PRETTY,
|
||||
stdout);
|
||||
}
|
||||
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
|
||||
{
|
||||
@@ -243,7 +244,8 @@ CALLBACK(list_conn, int,
|
||||
/**
|
||||
* Create a list of currently loaded connections
|
||||
*/
|
||||
static linked_list_t* list_conns(vici_conn_t *conn, bool raw)
|
||||
static linked_list_t* list_conns(vici_conn_t *conn,
|
||||
command_format_options_t format)
|
||||
{
|
||||
linked_list_t *list;
|
||||
vici_res_t *res;
|
||||
@@ -253,9 +255,10 @@ static linked_list_t* list_conns(vici_conn_t *conn, bool raw)
|
||||
res = vici_submit(vici_begin("get-conns"), conn);
|
||||
if (res)
|
||||
{
|
||||
if (raw)
|
||||
if (format & COMMAND_FORMAT_RAW)
|
||||
{
|
||||
vici_dump(res, "get-conns reply", stdout);
|
||||
vici_dump(res, "get-conns reply", format & COMMAND_FORMAT_PRETTY,
|
||||
stdout);
|
||||
}
|
||||
vici_parse_cb(res, NULL, NULL, list_conn, list);
|
||||
vici_free_res(res);
|
||||
@@ -286,7 +289,8 @@ static void remove_from_list(linked_list_t *list, char *str)
|
||||
/**
|
||||
* Unload a connection by name
|
||||
*/
|
||||
static bool unload_conn(vici_conn_t *conn, char *name, bool raw)
|
||||
static bool unload_conn(vici_conn_t *conn, char *name,
|
||||
command_format_options_t format)
|
||||
{
|
||||
vici_req_t *req;
|
||||
vici_res_t *res;
|
||||
@@ -300,9 +304,10 @@ static bool unload_conn(vici_conn_t *conn, char *name, bool raw)
|
||||
fprintf(stderr, "unload-conn request failed: %s\n", strerror(errno));
|
||||
return FALSE;
|
||||
}
|
||||
if (raw)
|
||||
if (format & COMMAND_FORMAT_RAW)
|
||||
{
|
||||
vici_dump(res, "unload-conn reply", stdout);
|
||||
vici_dump(res, "unload-conn reply", format & COMMAND_FORMAT_PRETTY,
|
||||
stdout);
|
||||
}
|
||||
else if (!streq(vici_find_str(res, "no", "success"), "yes"))
|
||||
{
|
||||
@@ -316,8 +321,8 @@ static bool unload_conn(vici_conn_t *conn, char *name, bool raw)
|
||||
|
||||
static int load_conns(vici_conn_t *conn)
|
||||
{
|
||||
bool raw = FALSE;
|
||||
u_int found = 0, loaded = 0, unloaded = 0;
|
||||
command_format_options_t format = COMMAND_FORMAT_NONE;
|
||||
char *arg, *section;
|
||||
enumerator_t *enumerator;
|
||||
linked_list_t *conns;
|
||||
@@ -329,8 +334,11 @@ static int load_conns(vici_conn_t *conn)
|
||||
{
|
||||
case 'h':
|
||||
return command_usage(NULL);
|
||||
case 'P':
|
||||
format |= COMMAND_FORMAT_PRETTY;
|
||||
/* fall through to raw */
|
||||
case 'r':
|
||||
raw = TRUE;
|
||||
format |= COMMAND_FORMAT_RAW;
|
||||
continue;
|
||||
case EOF:
|
||||
break;
|
||||
@@ -347,14 +355,14 @@ static int load_conns(vici_conn_t *conn)
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
conns = list_conns(conn, raw);
|
||||
conns = list_conns(conn, format);
|
||||
|
||||
enumerator = cfg->create_section_enumerator(cfg, "connections");
|
||||
while (enumerator->enumerate(enumerator, §ion))
|
||||
{
|
||||
remove_from_list(conns, section);
|
||||
found++;
|
||||
if (load_conn(conn, cfg, section, raw))
|
||||
if (load_conn(conn, cfg, section, format))
|
||||
{
|
||||
loaded++;
|
||||
}
|
||||
@@ -366,7 +374,7 @@ static int load_conns(vici_conn_t *conn)
|
||||
/* unload all connection in daemon, but not in file */
|
||||
while (conns->remove_first(conns, (void**)§ion) == SUCCESS)
|
||||
{
|
||||
if (unload_conn(conn, section, raw))
|
||||
if (unload_conn(conn, section, format))
|
||||
{
|
||||
unloaded++;
|
||||
}
|
||||
@@ -374,7 +382,7 @@ static int load_conns(vici_conn_t *conn)
|
||||
}
|
||||
conns->destroy(conns);
|
||||
|
||||
if (raw)
|
||||
if (format & COMMAND_FORMAT_RAW)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -401,10 +409,11 @@ static void __attribute__ ((constructor))reg()
|
||||
{
|
||||
command_register((command_t) {
|
||||
load_conns, 'c', "load-conns", "(re-)load connection configuration",
|
||||
{"[--raw]"},
|
||||
{"[--raw|--pretty]"},
|
||||
{
|
||||
{"help", 'h', 0, "show usage information"},
|
||||
{"raw", 'r', 0, "dump raw response message"},
|
||||
{"pretty", 'P', 0, "dump raw response message in pretty print"},
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user