diff options
| author | David Bremner <david@tethera.net> | 2016-03-22 07:54:52 -0300 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2016-05-25 07:40:44 -0300 |
| commit | 2d2a13966c74ffe86fc10abfbe1ac4c9798788ce (patch) | |
| tree | 94666c873f8ad22877f6d5e9b0e53f5cff8225a4 /notmuch-config.c | |
| parent | c6fcc555dde2a50ac779d5871720a4f074322457 (diff) | |
CLI: add notmuch-config support for named queries
Most of the infrastructure here is general, only the validation/dispatch
is hardcoded to a particular prefix.
A notable change in behaviour is that notmuch-config now opens the
database e.g. on every call to list, which fails with an error message
if the database doesn't exit yet.
Diffstat (limited to 'notmuch-config.c')
| -rw-r--r-- | notmuch-config.c | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/notmuch-config.c b/notmuch-config.c index 01bb1859..c618f2ca 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -751,6 +751,28 @@ _item_split (char *item, char **group, char **key) } #define BUILT_WITH_PREFIX "built_with." +#define QUERY_PREFIX "query." + +static int +_print_db_config(notmuch_config_t *config, const char *name) +{ + notmuch_database_t *notmuch; + char *val; + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) + return EXIT_FAILURE; + + /* XXX Handle UUID mismatch? */ + + if (print_status_database ("notmuch config", notmuch, + notmuch_database_get_config (notmuch, name, &val))) + return EXIT_FAILURE; + + puts (val); + + return EXIT_SUCCESS; +} static int notmuch_config_command_get (notmuch_config_t *config, char *item) @@ -778,6 +800,8 @@ notmuch_config_command_get (notmuch_config_t *config, char *item) } else if (STRNCMP_LITERAL (item, BUILT_WITH_PREFIX) == 0) { printf ("%s\n", notmuch_built_with (item + strlen (BUILT_WITH_PREFIX)) ? "true" : "false"); + } else if (STRNCMP_LITERAL (item, QUERY_PREFIX) == 0) { + return _print_db_config (config, item); } else { char **value; size_t i, length; @@ -805,6 +829,39 @@ notmuch_config_command_get (notmuch_config_t *config, char *item) } static int +_set_db_config(notmuch_config_t *config, const char *key, int argc, char **argv) +{ + notmuch_database_t *notmuch; + const char *val = ""; + + if (argc > 1) { + /* XXX handle lists? */ + fprintf (stderr, "notmuch config set: at most one value expected for %s\n", key); + return EXIT_FAILURE; + } + + if (argc > 0) { + val = argv[0]; + } + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) + return EXIT_FAILURE; + + /* XXX Handle UUID mismatch? */ + + if (print_status_database ("notmuch config", notmuch, + notmuch_database_set_config (notmuch, key, val))) + return EXIT_FAILURE; + + if (print_status_database ("notmuch config", notmuch, + notmuch_database_close (notmuch))) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} + +static int notmuch_config_command_set (notmuch_config_t *config, char *item, int argc, char *argv[]) { char *group, *key; @@ -814,6 +871,10 @@ notmuch_config_command_set (notmuch_config_t *config, char *item, int argc, char return 1; } + if (STRNCMP_LITERAL (item, QUERY_PREFIX) == 0) { + return _set_db_config (config, item, argc, argv); + } + if (_item_split (item, &group, &key)) return 1; @@ -853,6 +914,31 @@ _notmuch_config_list_built_with () } static int +_list_db_config (notmuch_config_t *config) +{ + notmuch_database_t *notmuch; + notmuch_config_list_t *list; + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) + return EXIT_FAILURE; + + /* XXX Handle UUID mismatch? */ + + + if (print_status_database ("notmuch config", notmuch, + notmuch_database_get_config_list (notmuch, "", &list))) + return EXIT_FAILURE; + + for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) { + printf("%s=%s\n", notmuch_config_list_key (list), notmuch_config_list_value(list)); + } + notmuch_config_list_destroy (list); + + return EXIT_SUCCESS; +} + +static int notmuch_config_command_list (notmuch_config_t *config) { char **groups; @@ -888,7 +974,7 @@ notmuch_config_command_list (notmuch_config_t *config) g_strfreev (groups); _notmuch_config_list_built_with (); - return 0; + return _list_db_config (config); } int |
