degesch: refactor handle_command_set_assign()

This commit is contained in:
Přemysl Eric Janouch 2015-05-03 18:45:05 +02:00
parent 3b5c3c3b4e
commit 1e3a273690

View File

@ -3878,6 +3878,42 @@ handle_command_set_replace (struct app_context *ctx,
return false; return false;
} }
static void
handle_command_set_assign_item (struct app_context *ctx,
char *key, struct config_item_ *new_, bool add, bool remove)
{
struct config_item_ *item =
config_item_get (ctx->config.root, key, NULL);
hard_assert (item);
struct error *e = NULL;
if ((add | remove) && item->type != CONFIG_ITEM_STRING_ARRAY)
// FIXME: it can also be null, which makes this message confusing
error_set (&e, "not a string array");
else if (add)
handle_command_set_add (item, new_->value.string.str, &e);
else if (remove)
handle_command_set_remove (item, new_->value.string.str, &e);
else
handle_command_set_replace (ctx, item, new_, &e);
if (e)
{
buffer_send_error (ctx, ctx->global_buffer,
"Failed to set option \"%s\": %s", key, e->message);
error_free (e);
}
else
{
struct str_vector tmp;
str_vector_init (&tmp);
dump_matching_options (ctx, key, &tmp);
buffer_send_status (ctx, ctx->global_buffer,
"Option changed: %s", tmp.vector[0]);
str_vector_free (&tmp);
}
}
static bool static bool
handle_command_set_assign handle_command_set_assign
(struct app_context *ctx, struct str_vector *all, char *arguments) (struct app_context *ctx, struct str_vector *all, char *arguments)
@ -3912,40 +3948,10 @@ handle_command_set_assign
config_item_destroy (new_); config_item_destroy (new_);
return true; return true;
} }
for (size_t i = 0; i < all->len; i++) for (size_t i = 0; i < all->len; i++)
{ {
char *key = xstrndup (all->vector[i], strcspn (all->vector[i], " ")); char *key = xstrndup (all->vector[i], strcspn (all->vector[i], " "));
struct config_item_ *item = handle_command_set_assign_item (ctx, key, new_, add, remove);
config_item_get (ctx->config.root, key, NULL);
hard_assert (item);
struct error *e = NULL;
if ((add | remove) && item->type != CONFIG_ITEM_STRING_ARRAY)
// FIXME: it can also be null, which makes this message confusing
error_set (&e, "not a string array");
else if (add)
handle_command_set_add (item, new_->value.string.str, &e);
else if (remove)
handle_command_set_remove (item, new_->value.string.str, &e);
else
handle_command_set_replace (ctx, item, new_, &e);
if (e)
{
buffer_send_error (ctx, ctx->global_buffer,
"Failed to set option \"%s\": %s", key, e->message);
error_free (e);
}
else
{
struct str_vector tmp;
str_vector_init (&tmp);
dump_matching_options (ctx, key, &tmp);
buffer_send_status (ctx, ctx->global_buffer,
"Option changed: %s", tmp.vector[0]);
str_vector_free (&tmp);
}
free (key); free (key);
} }
config_item_destroy (new_); config_item_destroy (new_);