[notmuch] [PATCH] [RFC] notmuch search: Return a non-zero exitcode if the search returns no hits.
If the user is explicitly searching for a message, then if notmuch fails to find it, it is useful to set a failure exit code. This makes it easier for scripting. However, we will want to then distinguish between fatal errors (such as out-of-memory, invalid arguments, corrupt db, etc) from the simple no message. To that end, we introduce a set of enumerated exit-codes which may prove useful to use consistently across notmuch. (I'm not that convinced highly differentiated errors is particularly useful, though separate CONFIG_ERROR and DATABASE_ERROR would seem to have value for scripts.) --- notmuch-search.c | 34 -- 1 files changed, 24 insertions(+), 10 deletions(-) diff --git a/notmuch-search.c b/notmuch-search.c index dc44eb6..6ca903b 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -20,7 +20,17 @@ #include "notmuch-client.h" -static void +/* XXX After some review, make these universal? */ +enum { +NOTMUCH_EXIT_SUCCESS = 0, +NOTMUCH_EXIT_NOT_FOUND, +NOTMUCH_EXIT_INVALID_ARGUMENT, +NOTMUCH_EXIT_SYSTEM_ERROR, /* XXX just out-of-memory? */ +NOTMUCH_EXIT_CONFIG_ERROR, +NOTMUCH_EXIT_DATABASE_ERROR, +}; + +static int do_search_threads (const void *ctx, notmuch_query_t *query, notmuch_sort_t sort) @@ -30,6 +40,7 @@ do_search_threads (const void *ctx, notmuch_tags_t *tags; time_t date; const char *relative_date; +int count = 0; for (threads = notmuch_query_search_threads (query); notmuch_threads_has_more (threads); @@ -67,7 +78,10 @@ do_search_threads (const void *ctx, printf (")\n"); notmuch_thread_destroy (thread); + count++; } + +return count; } int @@ -94,11 +108,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) sort = NOTMUCH_SORT_NEWEST_FIRST; } else { fprintf (stderr, "Invalid value for --sort: %s\n", opt); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } } else { fprintf (stderr, "Unrecognized option: %s\n", argv[i]); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } } @@ -107,35 +121,35 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) - return 1; + return NOTMUCH_EXIT_CONFIG_ERROR; notmuch = notmuch_database_open (notmuch_config_get_database_path (config), NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) - return 1; + return NOTMUCH_EXIT_DATABASE_ERROR; query_str = query_string_from_args (ctx, argc, argv); if (query_str == NULL) { fprintf (stderr, "Out of memory.\n"); - return 1; + return NOTMUCH_EXIT_SYSTEM_ERROR; } if (*query_str == '\0') { fprintf (stderr, "Error: notmuch search requires at least one search term.\n"); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } query = notmuch_query_create (notmuch, query_str); if (query == NULL) { fprintf (stderr, "Out of memory\n"); - return 1; + return NOTMUCH_EXIT_SYSTEM_ERROR; } notmuch_query_set_sort (query, sort); -do_search_threads (ctx, query, sort); +i = do_search_threads (ctx, query, sort); notmuch_query_destroy (query); notmuch_database_close (notmuch); -return 0; +return i ? NOTMUCH_EXIT_SUCCESS : NOTMUCH_EXIT_NOT_FOUND; } -- 1.6.6
[notmuch] [PATCH] [RFC] notmuch search: Return a non-zero exitcode if the search returns no hits.
If the user is explicitly searching for a message, then if notmuch fails to find it, it is useful to set a failure exit code. This makes it easier for scripting. However, we will want to then distinguish between fatal errors (such as out-of-memory, invalid arguments, corrupt db, etc) from the simple no message. To that end, we introduce a set of enumerated exit-codes which may prove useful to use consistently across notmuch. (I'm not that convinced highly differentiated errors is particularly useful, though separate CONFIG_ERROR and DATABASE_ERROR would seem to have value for scripts.) --- notmuch-search.c | 34 -- 1 files changed, 24 insertions(+), 10 deletions(-) diff --git a/notmuch-search.c b/notmuch-search.c index dc44eb6..6ca903b 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -20,7 +20,17 @@ #include notmuch-client.h -static void +/* XXX After some review, make these universal? */ +enum { +NOTMUCH_EXIT_SUCCESS = 0, +NOTMUCH_EXIT_NOT_FOUND, +NOTMUCH_EXIT_INVALID_ARGUMENT, +NOTMUCH_EXIT_SYSTEM_ERROR, /* XXX just out-of-memory? */ +NOTMUCH_EXIT_CONFIG_ERROR, +NOTMUCH_EXIT_DATABASE_ERROR, +}; + +static int do_search_threads (const void *ctx, notmuch_query_t *query, notmuch_sort_t sort) @@ -30,6 +40,7 @@ do_search_threads (const void *ctx, notmuch_tags_t *tags; time_t date; const char *relative_date; +int count = 0; for (threads = notmuch_query_search_threads (query); notmuch_threads_has_more (threads); @@ -67,7 +78,10 @@ do_search_threads (const void *ctx, printf ()\n); notmuch_thread_destroy (thread); + count++; } + +return count; } int @@ -94,11 +108,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) sort = NOTMUCH_SORT_NEWEST_FIRST; } else { fprintf (stderr, Invalid value for --sort: %s\n, opt); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } } else { fprintf (stderr, Unrecognized option: %s\n, argv[i]); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } } @@ -107,35 +121,35 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) - return 1; + return NOTMUCH_EXIT_CONFIG_ERROR; notmuch = notmuch_database_open (notmuch_config_get_database_path (config), NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) - return 1; + return NOTMUCH_EXIT_DATABASE_ERROR; query_str = query_string_from_args (ctx, argc, argv); if (query_str == NULL) { fprintf (stderr, Out of memory.\n); - return 1; + return NOTMUCH_EXIT_SYSTEM_ERROR; } if (*query_str == '\0') { fprintf (stderr, Error: notmuch search requires at least one search term.\n); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } query = notmuch_query_create (notmuch, query_str); if (query == NULL) { fprintf (stderr, Out of memory\n); - return 1; + return NOTMUCH_EXIT_SYSTEM_ERROR; } notmuch_query_set_sort (query, sort); -do_search_threads (ctx, query, sort); +i = do_search_threads (ctx, query, sort); notmuch_query_destroy (query); notmuch_database_close (notmuch); -return 0; +return i ? NOTMUCH_EXIT_SUCCESS : NOTMUCH_EXIT_NOT_FOUND; } -- 1.6.6 ___ notmuch mailing list notmuch@notmuchmail.org http://notmuchmail.org/mailman/listinfo/notmuch