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.
This commit is contained in:
Carl Worth 2010-10-27 15:38:16 -07:00
parent 49d90ede87
commit 65f2e61f28
3 changed files with 118 additions and 36 deletions

View file

@ -562,29 +562,42 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
config->new_tags = NULL; config->new_tags = NULL;
} }
int /* Given a configuration item of the form <group>.<key> return the
notmuch_config_command (void *ctx, int argc, char *argv[]) * 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)
{
char *period;
*group = item;
period = index (item, '.');
if (period == NULL || *(period+1) == '\0') {
fprintf (stderr,
"Invalid configuration name: %s\n"
"(Should be of the form <section>.<item>)\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; notmuch_config_t *config;
char *item;
if (argc != 2) {
fprintf (stderr, "Error: notmuch config requires two arguments.\n");
return 1;
}
if (strcmp (argv[0], "get")) {
fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
argv[0]);
return 1;
}
config = notmuch_config_open (ctx, NULL, NULL); config = notmuch_config_open (ctx, NULL, NULL);
if (config == NULL) if (config == NULL)
return 1; return 1;
item = argv[1];
if (strcmp(item, "database.path") == 0) { if (strcmp(item, "database.path") == 0) {
printf ("%s\n", notmuch_config_get_database_path (config)); printf ("%s\n", notmuch_config_get_database_path (config));
} else if (strcmp(item, "user.name") == 0) { } else if (strcmp(item, "user.name") == 0) {
@ -608,20 +621,10 @@ notmuch_config_command (void *ctx, int argc, char *argv[])
} else { } else {
char **value; char **value;
size_t i, length; size_t i, length;
char *group, *period, *key; char *group, *key;
group = item; if (_item_split (item, &group, &key))
period = index (item, '.');
if (period == NULL || *(period+1) == '\0') {
fprintf (stderr,
"Invalid configuration name: %s\n"
"(Should be of the form <section>.<item>)\n", item);
return 1; return 1;
}
*period = '\0';
key = period + 1;
value = g_key_file_get_string_list (config->key_file, value = g_key_file_get_string_list (config->key_file,
group, key, group, key,
@ -642,3 +645,61 @@ notmuch_config_command (void *ctx, int argc, char *argv[])
return 0; 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;
}

View file

@ -406,15 +406,13 @@ section below for details of the supported syntax for <search-terms>.
The The
.B config .B config
command can be used to get settings from the notmuch configuration command can be used to get or set settings int the notmuch
file. configuration file.
.RS 4 .RS 4
.TP 4 .TP 4
.BR "config get " <section> . <item> .BR "config get " <section> . <item>
Get settings from the notmuch configuration file.
The value of the specified configuration item is printed to stdout. If The value of the specified configuration item is printed to stdout. If
the item has multiple values, each value is separated by a newline the item has multiple values, each value is separated by a newline
character. character.
@ -432,6 +430,18 @@ Available configuration items include at least
new.tags new.tags
.RE .RE
.RS 4
.TP 4
.BR "config set " <section> . "<item> [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 .SH SEARCH SYNTAX
Several notmuch commands accept a common syntax for search terms. Several notmuch commands accept a common syntax for search terms.

View file

@ -322,19 +322,30 @@ command_t commands[] = {
"\tmessage specified by the search terms does not include a\n" "\tmessage specified by the search terms does not include a\n"
"\tpart with the specified \"id\" there will be no output." }, "\tpart with the specified \"id\" there will be no output." },
{ "config", notmuch_config_command, { "config", notmuch_config_command,
"get <section>.<item>", "[get|set] <section>.<item> [value ...]",
"Get settings from the notmuch configuration file.", "Get or set settings in the notmuch configuration file.",
" config get <section>.<item>\n"
"\n"
"\tThe value of the specified configuration item is printed\n" "\tThe value of the specified configuration item is printed\n"
"\tto stdout. If the item has multiple values, each value\n" "\tto stdout. If the item has multiple values, each value\n"
"\tis separated by a newline character.\n" "\tis separated by a newline character.\n"
"\n" "\n"
"\tAvailable configuration items include at least" "\tAvailable configuration items include at least\n"
"\n" "\n"
"\t\tdatabase.path\n" "\t\tdatabase.path\n"
"\t\tuser.name\n" "\t\tuser.name\n"
"\t\tuser.primary_email\n" "\t\tuser.primary_email\n"
"\t\tuser.other_email\n" "\t\tuser.other_email\n"
"\t\tnew.tags\n" }, "\t\tnew.tags\n"
"\n"
" config set <section>.<item> [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, { "help", notmuch_help_command,
"[<command>]", "[<command>]",
"This message, or more detailed help for the named command.", "This message, or more detailed help for the named command.",