The library already supports profile-based configuration resolution via the
NOTMUCH_PROFILE environment variable, and all three library entry points (open,
create, load) accept a profile parameter. However, the CLI never exposed this as
a command-line option.

Add --profile=PROFILE to the global options parsed in main(), before subcommand
dispatch. Like --config, this is intentionally kept out of
notmuch_shared_options so it is parsed once and does not appear in
per-subcommand help output. The profile value is passed to all three library
call sites and propagated to external commands via the NOTMUCH_PROFILE
environment variable.

Signed-off-by: inwit <[email protected]>
---
 notmuch.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/notmuch.c b/notmuch.c
index 814b9e42..eb03361d 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -54,6 +54,7 @@ notmuch_exit_if_unmatched_db_uuid (notmuch_database_t 
*notmuch);
 
 static bool print_version = false, print_help = false;
 static const char *notmuch_requested_db_uuid = NULL;
+static const char *notmuch_requested_profile = NULL;
 static int query_syntax = NOTMUCH_QUERY_SYNTAX_XAPIAN;
 
 const notmuch_opt_desc_t notmuch_shared_options [] = {
@@ -429,7 +430,8 @@ notmuch_command (notmuch_database_t *notmuch,
  * false on errors.
  */
 static bool
-try_external_command (const char *config_file_name, char *argv[])
+try_external_command (const char *config_file_name, const char *profile,
+                     char *argv[])
 {
     char *old_argv0 = argv[0];
     bool ret = true;
@@ -441,6 +443,13 @@ try_external_command (const char *config_file_name, char 
*argv[])
        }
     }
 
+    if (profile) {
+       if (setenv ("NOTMUCH_PROFILE", profile, 1)) {
+           perror ("setenv");
+           exit (1);
+       }
+    }
+
     argv[0] = talloc_asprintf (NULL, "notmuch-%s", old_argv0);
 
     /*
@@ -474,6 +483,7 @@ main (int argc, char *argv[])
 
     notmuch_opt_desc_t options[] = {
        { .opt_string = &config_file_name, .name = "config", .allow_empty = 
TRUE },
+       { .opt_string = &notmuch_requested_profile, .name = "profile" },
        { .opt_inherit = notmuch_shared_options },
        { }
     };
@@ -500,7 +510,8 @@ main (int argc, char *argv[])
     /* if command->function is NULL, try external command */
     if (! command || ! command->function) {
        /* This won't return if the external command is found. */
-       if (try_external_command (config_file_name, argv + opt_index))
+       if (try_external_command (config_file_name, notmuch_requested_profile,
+                                 argv + opt_index))
            fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch 
help\")\n",
                     command_name);
        ret = EXIT_FAILURE;
@@ -521,7 +532,7 @@ main (int argc, char *argv[])
        if (command->mode & NOTMUCH_COMMAND_DATABASE_CREATE) {
            status = notmuch_database_create_with_config (NULL,
                                                          config_file_name,
-                                                         NULL,
+                                                         
notmuch_requested_profile,
                                                          &notmuch,
                                                          &status_string);
            if (status && status != NOTMUCH_STATUS_DATABASE_EXISTS) {
@@ -541,7 +552,7 @@ main (int argc, char *argv[])
            status = notmuch_database_open_with_config (NULL,
                                                        mode,
                                                        config_file_name,
-                                                       NULL,
+                                                       
notmuch_requested_profile,
                                                        &notmuch,
                                                        &status_string);
            if (status) {
@@ -560,7 +571,7 @@ main (int argc, char *argv[])
        notmuch_status_t status;
        status = notmuch_database_load_config (NULL,
                                               config_file_name,
-                                              NULL,
+                                              notmuch_requested_profile,
                                               &notmuch,
                                               &status_string);
        if (status_string) {
-- 
2.53.0

_______________________________________________
notmuch mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to