mirror of
https://git.notmuchmail.org/git/notmuch
synced 2025-01-18 17:25:57 +01:00
cli: abstract config file reading to a separate function
Simplify and fix the coding style while at it.
This commit is contained in:
parent
5de84d0752
commit
59ec796024
1 changed files with 35 additions and 30 deletions
|
@ -202,6 +202,38 @@ get_username_from_passwd_file (void *ctx)
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static notmuch_bool_t
|
||||||
|
get_config_from_file (notmuch_config_t *config, notmuch_bool_t create_new)
|
||||||
|
{
|
||||||
|
GError *error = NULL;
|
||||||
|
notmuch_bool_t ret = FALSE;
|
||||||
|
|
||||||
|
if (g_key_file_load_from_file (config->key_file, config->filename,
|
||||||
|
G_KEY_FILE_KEEP_COMMENTS, &error))
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
|
if (error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT) {
|
||||||
|
/* If create_new is true, then the caller is prepared for a
|
||||||
|
* default configuration file in the case of FILE NOT FOUND.
|
||||||
|
*/
|
||||||
|
if (create_new) {
|
||||||
|
config->is_new = TRUE;
|
||||||
|
ret = TRUE;
|
||||||
|
} else {
|
||||||
|
fprintf (stderr, "Configuration file %s not found.\n"
|
||||||
|
"Try running 'notmuch setup' to create a configuration.\n",
|
||||||
|
config->filename);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fprintf (stderr, "Error reading configuration file %s: %s\n",
|
||||||
|
config->filename, error->message);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_error_free (error);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/* Open the named notmuch configuration file. If the filename is NULL,
|
/* Open the named notmuch configuration file. If the filename is NULL,
|
||||||
* the value of the environment variable $NOTMUCH_CONFIG will be used.
|
* the value of the environment variable $NOTMUCH_CONFIG will be used.
|
||||||
* If $NOTMUCH_CONFIG is unset, the default configuration file
|
* If $NOTMUCH_CONFIG is unset, the default configuration file
|
||||||
|
@ -289,37 +321,10 @@ notmuch_config_open (void *ctx,
|
||||||
config->search_exclude_tags_length = 0;
|
config->search_exclude_tags_length = 0;
|
||||||
config->crypto_gpg_path = NULL;
|
config->crypto_gpg_path = NULL;
|
||||||
|
|
||||||
if (! g_key_file_load_from_file (config->key_file,
|
if (! get_config_from_file (config, create_new)) {
|
||||||
config->filename,
|
|
||||||
G_KEY_FILE_KEEP_COMMENTS,
|
|
||||||
&error))
|
|
||||||
{
|
|
||||||
if (error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT) {
|
|
||||||
/* If create_new is true, then the caller is prepared for a
|
|
||||||
* default configuration file in the case of FILE NOT
|
|
||||||
* FOUND.
|
|
||||||
*/
|
|
||||||
if (create_new) {
|
|
||||||
g_error_free (error);
|
|
||||||
config->is_new = TRUE;
|
|
||||||
} else {
|
|
||||||
fprintf (stderr, "Configuration file %s not found.\n"
|
|
||||||
"Try running 'notmuch setup' to create a configuration.\n",
|
|
||||||
config->filename);
|
|
||||||
talloc_free (config);
|
talloc_free (config);
|
||||||
g_error_free (error);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf (stderr, "Error reading configuration file %s: %s\n",
|
|
||||||
config->filename, error->message);
|
|
||||||
talloc_free (config);
|
|
||||||
g_error_free (error);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Whenever we know of configuration sections that don't appear in
|
/* Whenever we know of configuration sections that don't appear in
|
||||||
* the configuration file, we add some comments to help the user
|
* the configuration file, we add some comments to help the user
|
||||||
|
|
Loading…
Reference in a new issue