mirror of https://github.com/djcb/mu.git
* basic implementation of --exec for mu find (WIP)
This commit is contained in:
parent
543a4400ab
commit
a31ea71e0a
|
@ -296,8 +296,6 @@ save_parts (const char *path, const char *filename, MuConfig *opts)
|
|||
return rv;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define color_maybe(C) do{ if (color) fputs ((C),stdout);}while(0)
|
||||
|
||||
static void
|
||||
|
|
|
@ -216,6 +216,54 @@ process_query (MuQuery *xapian, const gchar *query, MuConfig *opts,
|
|||
|
||||
|
||||
|
||||
static gboolean
|
||||
exec_cmd_on_query (MuQuery *xapian, const gchar *query, MuConfig *opts,
|
||||
size_t *count)
|
||||
{
|
||||
MuMsgIter *iter;
|
||||
gboolean rv;
|
||||
|
||||
iter = run_query (xapian, query, opts, count);
|
||||
if (!iter)
|
||||
return FALSE;
|
||||
|
||||
rv = TRUE;
|
||||
while (!mu_msg_iter_is_done (iter)) {
|
||||
const char* path;
|
||||
path = mu_msg_get_path (mu_msg_iter_get_msg (iter, NULL));
|
||||
if (access (path, R_OK) == 0) {
|
||||
gint status;
|
||||
GError *err;
|
||||
char *cmd;
|
||||
cmd = g_strdup_printf ("%s %s", opts->exec, path);
|
||||
err = NULL; /* FIXME: stdout/stderr */
|
||||
rv = g_spawn_command_line_sync (cmd, NULL, NULL, &status,
|
||||
&err);
|
||||
g_free (cmd);
|
||||
if (!rv) {
|
||||
g_warning ("command returned %d on %s: %s\n",
|
||||
status, path, err->message);
|
||||
g_error_free (err);
|
||||
break;
|
||||
}
|
||||
} else
|
||||
g_warning ("cannot execute command on %s: %s",
|
||||
path, strerror(errno));
|
||||
|
||||
mu_msg_iter_next (iter);
|
||||
}
|
||||
|
||||
if (rv && count && *count == 0)
|
||||
g_warning ("no matching messages found");
|
||||
|
||||
mu_msg_iter_destroy (iter);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static gboolean
|
||||
format_params_valid (MuConfig *opts)
|
||||
{
|
||||
|
@ -863,6 +911,8 @@ mu_cmd_find (MuConfig *opts)
|
|||
|
||||
if (format == FORMAT_XQUERY)
|
||||
rv = print_xapian_query (xapian, query, &count);
|
||||
else if (opts->exec)
|
||||
rv = exec_cmd_on_query (xapian, query, opts, &count);
|
||||
else
|
||||
rv = process_query (xapian, query, opts, format, &count);
|
||||
|
||||
|
|
|
@ -177,8 +177,8 @@ config_options_group_find (MuConfig *opts)
|
|||
{"format", 'o', 0, G_OPTION_ARG_STRING, &opts->formatstr,
|
||||
"output format ('plain'(*), 'links', 'xml',"
|
||||
"'json', 'sexp', 'xquery')", NULL},
|
||||
/* {"exec", 'e', 0, G_OPTION_ARG_STRING, &opts->exec, */
|
||||
/* "execute command on each match message", NULL}, */
|
||||
{"exec", 'e', 0, G_OPTION_ARG_STRING, &opts->exec,
|
||||
"execute command on each match message", NULL},
|
||||
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue