degesch: add an option to save config on quit

This commit is contained in:
Přemysl Eric Janouch 2015-07-11 02:45:24 +02:00
parent 0a657a0294
commit 8f1d81eefb
1 changed files with 32 additions and 18 deletions

View File

@ -1578,6 +1578,10 @@ static struct config_schema g_config_behaviour[] =
.type = CONFIG_ITEM_BOOLEAN, .type = CONFIG_ITEM_BOOLEAN,
.default_ = "off", .default_ = "off",
.on_change = on_config_logging_change }, .on_change = on_config_logging_change },
{ .name = "save_on_quit",
.comment = "Save configuration before quitting",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "on" },
{} {}
}; };
@ -6306,6 +6310,30 @@ dump_matching_options
} }
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
save_configuration (struct app_context *ctx)
{
struct str data;
str_init (&data);
serialize_configuration (ctx, &data);
struct error *e = NULL;
char *filename = write_configuration_file (&data, &e);
str_free (&data);
if (!filename)
{
log_global_error (ctx,
"#s: #s", "Saving configuration failed", e->message);
error_free (e);
}
else
log_global_status (ctx, "Configuration written to `#s'", filename);
free (filename);
}
// --- Server management ------------------------------------------------------- // --- Server management -------------------------------------------------------
static bool static bool
@ -6782,27 +6810,10 @@ handle_command_set (struct handler_args *a)
static bool static bool
handle_command_save (struct handler_args *a) handle_command_save (struct handler_args *a)
{ {
struct app_context *ctx = a->ctx;
if (*a->arguments) if (*a->arguments)
return false; return false;
struct str data; save_configuration (a->ctx);
str_init (&data);
serialize_configuration (ctx, &data);
struct error *e = NULL;
char *filename = write_configuration_file (&data, &e);
str_free (&data);
if (!filename)
{
log_global_error (ctx,
"#s: #s", "Saving configuration failed", e->message);
error_free (e);
}
else
log_global_status (ctx, "Configuration written to `#s'", filename);
free (filename);
return true; return true;
} }
@ -9011,6 +9022,9 @@ main (int argc, char *argv[])
while (ctx.polling) while (ctx.polling)
poller_run (&ctx.poller); poller_run (&ctx.poller);
if (get_config_boolean (ctx.config.root, "behaviour.save_on_quit"))
save_configuration (&ctx);
app_context_free (&ctx); app_context_free (&ctx);
free_terminal (); free_terminal ();
return EXIT_SUCCESS; return EXIT_SUCCESS;