From 65f2e61f28a0f5c5dc38dd3cf8a1de023934b330 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 27 Oct 2010 15:38:16 -0700 Subject: [PATCH] notmuch config: Allow for new "notmuch config set" in addition to get It is now possible to set configuration items from the command-line in a manner quite similar to the support for querying configuration items. --- notmuch-config.c | 111 ++++++++++++++++++++++++++++++++++++----------- notmuch.1 | 18 ++++++-- notmuch.c | 19 ++++++-- 3 files changed, 115 insertions(+), 33 deletions(-) diff --git a/notmuch-config.c b/notmuch-config.c index 188ecd71..dcdb0369 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -562,29 +562,42 @@ notmuch_config_set_new_tags (notmuch_config_t *config, config->new_tags = NULL; } -int -notmuch_config_command (void *ctx, int argc, char *argv[]) +/* Given a configuration item of the form . return the + * component group and key. If any error occurs, print a message on + * stderr and return 1. Otherwise, return 0. + * + * Note: This function modifies the original 'item' string. + */ +static int +_item_split (char *item, char **group, char **key) { - notmuch_config_t *config; - char *item; + char *period; - if (argc != 2) { - fprintf (stderr, "Error: notmuch config requires two arguments.\n"); - return 1; - } + *group = item; - if (strcmp (argv[0], "get")) { - fprintf (stderr, "Unrecognized argument for notmuch config: %s\n", - argv[0]); + period = index (item, '.'); + if (period == NULL || *(period+1) == '\0') { + fprintf (stderr, + "Invalid configuration name: %s\n" + "(Should be of the form
.)\n", item); return 1; } + *period = '\0'; + *key = period + 1; + + return 0; +} + +static int +notmuch_config_command_get (void *ctx, char *item) +{ + notmuch_config_t *config; + config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) return 1; - item = argv[1]; - if (strcmp(item, "database.path") == 0) { printf ("%s\n", notmuch_config_get_database_path (config)); } else if (strcmp(item, "user.name") == 0) { @@ -608,20 +621,10 @@ notmuch_config_command (void *ctx, int argc, char *argv[]) } else { char **value; size_t i, length; - char *group, *period, *key; - - group = item; + char *group, *key; - period = index (item, '.'); - if (period == NULL || *(period+1) == '\0') { - fprintf (stderr, - "Invalid configuration name: %s\n" - "(Should be of the form
.)\n", item); + if (_item_split (item, &group, &key)) return 1; - } - - *period = '\0'; - key = period + 1; value = g_key_file_get_string_list (config->key_file, group, key, @@ -642,3 +645,61 @@ notmuch_config_command (void *ctx, int argc, char *argv[]) return 0; } + +static int +notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[]) +{ + notmuch_config_t *config; + char *group, *key; + int ret; + + if (_item_split (item, &group, &key)) + return 1; + + config = notmuch_config_open (ctx, NULL, NULL); + if (config == NULL) + return 1; + + /* With only the name of an item, we clear it from the + * configuration file. + * + * With a single value, we set it as a string. + * + * With multiple values, we set them as a string list. + */ + switch (argc) { + case 0: + g_key_file_remove_key (config->key_file, group, key, NULL); + break; + case 1: + g_key_file_set_string (config->key_file, group, key, argv[0]); + break; + default: + g_key_file_set_string_list (config->key_file, group, key, + (const gchar **) argv, argc); + break; + } + + ret = notmuch_config_save (config); + notmuch_config_close (config); + + return ret; +} + +int +notmuch_config_command (void *ctx, int argc, char *argv[]) +{ + if (argc < 2) { + fprintf (stderr, "Error: notmuch config requires at least two arguments.\n"); + return 1; + } + + if (strcmp (argv[0], "get") == 0) + return notmuch_config_command_get (ctx, argv[1]); + else if (strcmp (argv[0], "set") == 0) + return notmuch_config_command_set (ctx, argv[1], argc - 2, argv + 2); + + fprintf (stderr, "Unrecognized argument for notmuch config: %s\n", + argv[0]); + return 1; +} diff --git a/notmuch.1 b/notmuch.1 index 41f7c7e4..f803f908 100644 --- a/notmuch.1 +++ b/notmuch.1 @@ -406,15 +406,13 @@ section below for details of the supported syntax for . The .B config -command can be used to get settings from the notmuch configuration -file. +command can be used to get or set settings int the notmuch +configuration file. .RS 4 .TP 4 .BR "config get "
. -Get settings from the notmuch configuration file. - The value of the specified configuration item is printed to stdout. If the item has multiple values, each value is separated by a newline character. @@ -432,6 +430,18 @@ Available configuration items include at least new.tags .RE +.RS 4 +.TP 4 +.BR "config set "
. " [values ...]" + +The specified configuration item is set to the given value. To +specify a multiple-value item, provide each value as a separate +command-line argument. + +If no values are provided, the specified configuration item will be +removed from the configuration file. +.RE + .SH SEARCH SYNTAX Several notmuch commands accept a common syntax for search terms. diff --git a/notmuch.c b/notmuch.c index f9acfa36..326aa4c8 100644 --- a/notmuch.c +++ b/notmuch.c @@ -322,19 +322,30 @@ command_t commands[] = { "\tmessage specified by the search terms does not include a\n" "\tpart with the specified \"id\" there will be no output." }, { "config", notmuch_config_command, - "get
.", - "Get settings from the notmuch configuration file.", + "[get|set]
. [value ...]", + "Get or set settings in the notmuch configuration file.", + " config get
.\n" + "\n" "\tThe value of the specified configuration item is printed\n" "\tto stdout. If the item has multiple values, each value\n" "\tis separated by a newline character.\n" "\n" - "\tAvailable configuration items include at least" + "\tAvailable configuration items include at least\n" "\n" "\t\tdatabase.path\n" "\t\tuser.name\n" "\t\tuser.primary_email\n" "\t\tuser.other_email\n" - "\t\tnew.tags\n" }, + "\t\tnew.tags\n" + "\n" + " config set
. [value ...]\n" + "\n" + "\tThe specified configuration item is set to the given value.\n" + "\tTo specify a multiple-value item, provide each value as\n" + "\ta separate command-line argument.\n" + "\n" + "\tIf no values are provided, the specified configuration item\n" + "\twill be removed from the configuration file." }, { "help", notmuch_help_command, "[]", "This message, or more detailed help for the named command.", -- 2.43.0