mu: implement new command-line parser
Implement a new command-line parser, based on CLI11. It's a bit more C++'ish, and allows for a lot of fancy things... some of which we have implemented here. Update the various commands to use the new Options struct Remove the old help strings; instead e.g. `mu help view` opens the manpage. Integrate the guile scripts more tightly.
This commit is contained in:
272
mu/mu-cmd.cc
272
mu/mu-cmd.cc
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** Copyright (C) 2010-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
** Copyright (C) 2010-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or modify it
|
||||
** under the terms of the GNU General Public License as published by the
|
||||
@ -28,11 +28,10 @@
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "mu-config.hh"
|
||||
#include "mu-options.hh"
|
||||
#include "mu-cmd.hh"
|
||||
#include "mu-maildir.hh"
|
||||
#include "mu-contacts-cache.hh"
|
||||
#include "mu-runtime.hh"
|
||||
#include "message/mu-message.hh"
|
||||
#include "message/mu-mime-object.hh"
|
||||
|
||||
@ -49,7 +48,7 @@
|
||||
using namespace Mu;
|
||||
|
||||
static Mu::Result<void>
|
||||
view_msg_sexp(const Message& message, const MuConfig* opts)
|
||||
view_msg_sexp(const Message& message, const Options& opts)
|
||||
{
|
||||
::fputs(message.sexp().to_string().c_str(), stdout);
|
||||
::fputs("\n", stdout);
|
||||
@ -59,7 +58,7 @@ view_msg_sexp(const Message& message, const MuConfig* opts)
|
||||
|
||||
|
||||
static std::string /* return comma-sep'd list of attachments */
|
||||
get_attach_str(const Message& message, const MuConfig* opts)
|
||||
get_attach_str(const Message& message, const Options& opts)
|
||||
{
|
||||
std::string str;
|
||||
seq_for_each(message.parts(), [&](auto&& part) {
|
||||
@ -100,12 +99,11 @@ print_field(const std::string& field, const std::string& val, bool color)
|
||||
|
||||
/* a summary_len of 0 mean 'don't show summary, show body */
|
||||
static void
|
||||
body_or_summary(const Message& message, const MuConfig* opts)
|
||||
body_or_summary(const Message& message, const Options& opts)
|
||||
{
|
||||
gboolean color;
|
||||
//int my_opts = mu_config_get_msg_options(opts) | MU_MSG_OPTION_CONSOLE_PASSWORD;
|
||||
|
||||
color = !opts->nocolor;
|
||||
color = !opts.nocolor;
|
||||
|
||||
const auto body{message.body_text()};
|
||||
if (!body || body->empty()) {
|
||||
@ -121,9 +119,9 @@ body_or_summary(const Message& message, const MuConfig* opts)
|
||||
return;
|
||||
}
|
||||
|
||||
if (opts->summary_len != 0) {
|
||||
if (opts.view.summary_len) {
|
||||
gchar* summ;
|
||||
summ = mu_str_summarize(body->c_str(), opts->summary_len);
|
||||
summ = mu_str_summarize(body->c_str(), *opts.view.summary_len);
|
||||
print_field("Summary", summ, color);
|
||||
g_free(summ);
|
||||
} else {
|
||||
@ -136,9 +134,9 @@ body_or_summary(const Message& message, const MuConfig* opts)
|
||||
/* we ignore fields for now */
|
||||
/* summary_len == 0 means "no summary */
|
||||
static Mu::Result<void>
|
||||
view_msg_plain(const Message& message, const MuConfig* opts)
|
||||
view_msg_plain(const Message& message, const Options& opts)
|
||||
{
|
||||
const auto color{!opts->nocolor};
|
||||
const auto color{!opts.nocolor};
|
||||
|
||||
print_field("From", to_string(message.from()), color);
|
||||
print_field("To", to_string(message.to()), color);
|
||||
@ -158,16 +156,18 @@ view_msg_plain(const Message& message, const MuConfig* opts)
|
||||
}
|
||||
|
||||
static Mu::Result<void>
|
||||
handle_msg(const std::string& fname, const MuConfig* opts)
|
||||
handle_msg(const std::string& fname, const Options& opts)
|
||||
{
|
||||
auto message{Message::make_from_path(fname, mu_config_message_options(opts))};
|
||||
using Format = Options::View::Format;
|
||||
|
||||
auto message{Message::make_from_path(fname, message_options(opts.view))};
|
||||
if (!message)
|
||||
return Err(message.error());
|
||||
|
||||
switch (opts->format) {
|
||||
case MU_CONFIG_FORMAT_PLAIN:
|
||||
switch (opts.view.format) {
|
||||
case Format::Plain:
|
||||
return view_msg_plain(*message, opts);
|
||||
case MU_CONFIG_FORMAT_SEXP:
|
||||
case Format::Sexp:
|
||||
return view_msg_sexp(*message, opts);
|
||||
default:
|
||||
g_critical("bug: should not be reached");
|
||||
@ -176,35 +176,13 @@ handle_msg(const std::string& fname, const MuConfig* opts)
|
||||
}
|
||||
|
||||
static Mu::Result<void>
|
||||
view_params_valid(const MuConfig* opts)
|
||||
cmd_view(const Options& opts)
|
||||
{
|
||||
/* note: params[0] will be 'view' */
|
||||
if (!opts->params[0] || !opts->params[1])
|
||||
return Err(Error::Code::InvalidArgument, "error in parameters");
|
||||
|
||||
switch (opts->format) {
|
||||
case MU_CONFIG_FORMAT_PLAIN:
|
||||
case MU_CONFIG_FORMAT_SEXP: break;
|
||||
default:
|
||||
return Err(Error::Code::InvalidArgument, "invalid output format");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
static Mu::Result<void>
|
||||
cmd_view(const MuConfig* opts)
|
||||
{
|
||||
if (!opts || opts->cmd != Mu::MU_CONFIG_CMD_VIEW)
|
||||
return Err(Error::Code::InvalidArgument, "invalid parameters");
|
||||
if (auto res = view_params_valid(opts); !res)
|
||||
return res;
|
||||
|
||||
for (auto i = 1; opts->params[i]; ++i) {
|
||||
if (auto res = handle_msg(opts->params[i], opts); !res)
|
||||
for (auto&& file: opts.view.files) {
|
||||
if (auto res = handle_msg(file, opts); !res)
|
||||
return res;
|
||||
/* add a separator between two messages? */
|
||||
if (opts->terminator)
|
||||
if (opts.view.terminate)
|
||||
g_print("%c", VIEW_TERMINATOR);
|
||||
}
|
||||
|
||||
@ -212,17 +190,11 @@ cmd_view(const MuConfig* opts)
|
||||
}
|
||||
|
||||
static Mu::Result<void>
|
||||
cmd_mkdir(const MuConfig* opts)
|
||||
cmd_mkdir(const Options& opts)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!opts->params[1])
|
||||
return Err(Error::Code::InvalidArgument,
|
||||
"missing directory parameter");
|
||||
|
||||
for (i = 1; opts->params[i]; ++i) {
|
||||
for (auto&& dir: opts.mkdir.dirs) {
|
||||
if (auto&& res =
|
||||
maildir_mkdir(opts->params[i], opts->dirmode, FALSE); !res)
|
||||
maildir_mkdir(dir, opts.mkdir.mode); !res)
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -230,42 +202,29 @@ cmd_mkdir(const MuConfig* opts)
|
||||
}
|
||||
|
||||
static Result<void>
|
||||
cmd_add(Mu::Store& store, const MuConfig* opts)
|
||||
cmd_add(Mu::Store& store, const Options& opts)
|
||||
{
|
||||
/* note: params[0] will be 'add' */
|
||||
if (!opts->params[0] || !opts->params[1])
|
||||
return Err(Error::Code::InvalidArgument,
|
||||
"expected some files to add");
|
||||
|
||||
for (auto u = 1; opts->params[u]; ++u) {
|
||||
|
||||
const auto docid{store.add_message(opts->params[u])};
|
||||
for (auto&& file: opts.add.files) {
|
||||
const auto docid{store.add_message(file)};
|
||||
if (!docid)
|
||||
return Err(docid.error());
|
||||
else
|
||||
g_debug("added message @ %s, docid=%u",
|
||||
opts->params[u], docid.value());
|
||||
file.c_str(), docid.value());
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
static Result<void>
|
||||
cmd_remove(Mu::Store& store, const MuConfig* opts)
|
||||
cmd_remove(Mu::Store& store, const Options& opts)
|
||||
{
|
||||
/* note: params[0] will be 'remove' */
|
||||
if (!opts->params[0] || !opts->params[1])
|
||||
return Err(Error::Code::InvalidArgument,
|
||||
"expected some files to remove");
|
||||
|
||||
for (auto u = 1; opts->params[u]; ++u) {
|
||||
|
||||
const auto res = store.remove_message(opts->params[u]);
|
||||
for (auto&& file: opts.remove.files) {
|
||||
const auto res = store.remove_message(file);
|
||||
if (!res)
|
||||
return Err(Error::Code::File, "failed to remove %s",
|
||||
opts->params[u]);
|
||||
return Err(Error::Code::File, "failed to remove %s", file.c_str());
|
||||
else
|
||||
g_debug("removed message @ %s", opts->params[u]);
|
||||
g_debug("removed message @ %s", file.c_str());
|
||||
}
|
||||
|
||||
return Ok();
|
||||
@ -286,9 +245,9 @@ key_val(const Mu::MaybeAnsi& col, const std::string& key, T val)
|
||||
|
||||
|
||||
static void
|
||||
print_signature(const Mu::MimeSignature& sig, const MuConfig *opts)
|
||||
print_signature(const Mu::MimeSignature& sig, const Options& opts)
|
||||
{
|
||||
Mu::MaybeAnsi col{!opts->nocolor};
|
||||
Mu::MaybeAnsi col{!opts.nocolor};
|
||||
|
||||
const auto created{sig.created()};
|
||||
key_val(col, "created",
|
||||
@ -318,10 +277,10 @@ print_signature(const Mu::MimeSignature& sig, const MuConfig *opts)
|
||||
|
||||
|
||||
static bool
|
||||
verify(const MimeMultipartSigned& sigpart, const MuConfig *opts)
|
||||
verify(const MimeMultipartSigned& sigpart, const Options& opts)
|
||||
{
|
||||
using VFlags = MimeMultipartSigned::VerifyFlags;
|
||||
const auto vflags{opts->auto_retrieve ?
|
||||
const auto vflags{opts.verify.auto_retrieve ?
|
||||
VFlags::EnableKeyserverLookups: VFlags::None};
|
||||
|
||||
auto ctx{MimeCryptoContext::make_gpg()};
|
||||
@ -329,11 +288,11 @@ verify(const MimeMultipartSigned& sigpart, const MuConfig *opts)
|
||||
return false;
|
||||
|
||||
const auto sigs{sigpart.verify(*ctx, vflags)};
|
||||
Mu::MaybeAnsi col{!opts->nocolor};
|
||||
Mu::MaybeAnsi col{!opts.nocolor};
|
||||
|
||||
if (!sigs || sigs->empty()) {
|
||||
|
||||
if (!opts->quiet)
|
||||
if (!opts.quiet)
|
||||
g_print("cannot find signatures in part\n");
|
||||
|
||||
return true;
|
||||
@ -344,10 +303,10 @@ verify(const MimeMultipartSigned& sigpart, const MuConfig *opts)
|
||||
|
||||
const auto status{sig.status()};
|
||||
|
||||
if (!opts->quiet)
|
||||
if (!opts.quiet)
|
||||
key_val(col, "status", to_string(status));
|
||||
|
||||
if (opts->verbose)
|
||||
if (opts.verbose)
|
||||
print_signature(sig, opts);
|
||||
|
||||
if (none_of(sig.status() & MimeSignature::Status::Green))
|
||||
@ -358,42 +317,44 @@ verify(const MimeMultipartSigned& sigpart, const MuConfig *opts)
|
||||
}
|
||||
|
||||
static Mu::Result<void>
|
||||
cmd_verify(const MuConfig* opts)
|
||||
cmd_verify(const Options& opts)
|
||||
{
|
||||
if (!opts || opts->cmd != MU_CONFIG_CMD_VERIFY)
|
||||
return Err(Error::Code::Internal, "error in parameters");
|
||||
bool all_ok{true};
|
||||
const auto mopts = message_options(opts.verify);
|
||||
|
||||
if (!opts->params[1])
|
||||
return Err(Error::Code::InvalidArgument,
|
||||
"missing message-file parameter");
|
||||
for (auto&& file: opts.verify.files) {
|
||||
|
||||
auto message{Message::make_from_path(opts->params[1],
|
||||
mu_config_message_options(opts))};
|
||||
if (!message)
|
||||
return Err(message.error());
|
||||
auto message{Message::make_from_path(file, mopts)};
|
||||
if (!message)
|
||||
return Err(message.error());
|
||||
|
||||
if (!opts.quiet && opts.verify.files.size() > 1)
|
||||
g_print("verifying %sn\n", file.c_str());
|
||||
|
||||
if (none_of(message->flags() & Flags::Signed)) {
|
||||
if (!opts->quiet)
|
||||
g_print("no signed parts found\n");
|
||||
return Ok();
|
||||
if (none_of(message->flags() & Flags::Signed)) {
|
||||
if (!opts.quiet)
|
||||
g_print("%s: no signed parts found\n", file.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
bool verified{true}; /* innocent until proven guilty */
|
||||
for(auto&& part: message->parts()) {
|
||||
|
||||
if (!part.is_signed())
|
||||
continue;
|
||||
|
||||
const auto& mobj{part.mime_object()};
|
||||
if (!mobj.is_multipart_signed())
|
||||
continue;
|
||||
|
||||
if (!verify(MimeMultipartSigned(mobj), opts))
|
||||
verified = false;
|
||||
}
|
||||
|
||||
all_ok = all_ok && verified;
|
||||
}
|
||||
|
||||
bool verified{true}; /* innocent until proven guilty */
|
||||
for(auto&& part: message->parts()) {
|
||||
|
||||
if (!part.is_signed())
|
||||
continue;
|
||||
|
||||
const auto& mobj{part.mime_object()};
|
||||
if (!mobj.is_multipart_signed())
|
||||
continue;
|
||||
|
||||
if (!verify(MimeMultipartSigned(mobj), opts))
|
||||
verified = false;
|
||||
}
|
||||
|
||||
if (verified)
|
||||
if (all_ok)
|
||||
return Ok();
|
||||
else
|
||||
return Err(Error::Code::UnverifiedSignature,
|
||||
@ -401,7 +362,7 @@ cmd_verify(const MuConfig* opts)
|
||||
}
|
||||
|
||||
static Result<void>
|
||||
cmd_info(const Mu::Store& store, const MuConfig* opts)
|
||||
cmd_info(const Mu::Store& store, const Options& opts)
|
||||
{
|
||||
using namespace tabulate;
|
||||
|
||||
@ -442,7 +403,7 @@ cmd_info(const Mu::Store& store, const MuConfig* opts)
|
||||
info.add_row({"last-change", tstamp(store.statistics().last_change)});
|
||||
info.add_row({"last-index", tstamp(store.statistics().last_index)});
|
||||
|
||||
if (!opts->nocolor)
|
||||
if (!opts.nocolor)
|
||||
colorify(info);
|
||||
|
||||
std::cout << info << '\n';
|
||||
@ -451,38 +412,24 @@ cmd_info(const Mu::Store& store, const MuConfig* opts)
|
||||
}
|
||||
|
||||
static Result<void>
|
||||
cmd_init(const MuConfig* opts)
|
||||
cmd_init(const Options& opts)
|
||||
{
|
||||
/* not provided, nor could we find a good default */
|
||||
if (!opts->maildir)
|
||||
if (opts.init.maildir.empty())
|
||||
return Err(Error::Code::InvalidArgument,
|
||||
"missing --maildir parameter and could "
|
||||
"not determine default");
|
||||
|
||||
if (opts->max_msg_size < 0)
|
||||
return Err(Error::Code::InvalidArgument,
|
||||
"invalid value for max-message-size");
|
||||
else if (opts->batch_size < 0)
|
||||
return Err(Error::Code::InvalidArgument,
|
||||
"invalid value for batch-size");
|
||||
|
||||
Mu::Store::Config conf{};
|
||||
conf.max_message_size = opts->max_msg_size;
|
||||
conf.batch_size = opts->batch_size;
|
||||
conf.max_message_size = opts.init.max_msg_size.value_or(0);
|
||||
conf.batch_size = opts.init.batch_size.value_or(0);
|
||||
|
||||
Mu::StringVec my_addrs;
|
||||
auto addrs = opts->my_addresses;
|
||||
while (addrs && *addrs) {
|
||||
my_addrs.emplace_back(*addrs);
|
||||
++addrs;
|
||||
}
|
||||
|
||||
auto store = Store::make_new(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB),
|
||||
opts->maildir, my_addrs, conf);
|
||||
auto store = Store::make_new(opts.runtime_path(RuntimePath::XapianDb),
|
||||
opts.init.maildir, opts.init.my_addresses, conf);
|
||||
if (!store)
|
||||
return Err(store.error());
|
||||
|
||||
if (!opts->quiet) {
|
||||
if (!opts.quiet) {
|
||||
cmd_info(*store, opts);
|
||||
std::cout << "\nstore created; use the 'index' command to fill/update it.\n";
|
||||
}
|
||||
@ -491,9 +438,9 @@ cmd_init(const MuConfig* opts)
|
||||
}
|
||||
|
||||
static Result<void>
|
||||
cmd_find(const MuConfig* opts)
|
||||
cmd_find(const Options& opts)
|
||||
{
|
||||
auto store{Store::make(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB))};
|
||||
auto store{Store::make(opts.runtime_path(RuntimePath::XapianDb))};
|
||||
if (!store)
|
||||
return Err(store.error());
|
||||
else
|
||||
@ -511,13 +458,13 @@ show_usage(void)
|
||||
}
|
||||
|
||||
|
||||
using ReadOnlyStoreFunc = std::function<Result<void>(const Store&, const MuConfig*)>;
|
||||
using WritableStoreFunc = std::function<Result<void>(Store&, const MuConfig*)>;
|
||||
using ReadOnlyStoreFunc = std::function<Result<void>(const Store&, const Options&)>;
|
||||
using WritableStoreFunc = std::function<Result<void>(Store&, const Options&)>;
|
||||
|
||||
static Result<void>
|
||||
with_readonly_store(const ReadOnlyStoreFunc& func, const MuConfig* opts)
|
||||
with_readonly_store(const ReadOnlyStoreFunc& func, const Options& opts)
|
||||
{
|
||||
auto store{Store::make(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB))};
|
||||
auto store{Store::make(opts.runtime_path(RuntimePath::XapianDb))};
|
||||
if (!store)
|
||||
return Err(store.error());
|
||||
|
||||
@ -525,9 +472,9 @@ with_readonly_store(const ReadOnlyStoreFunc& func, const MuConfig* opts)
|
||||
}
|
||||
|
||||
static Result<void>
|
||||
with_writable_store(const WritableStoreFunc func, const MuConfig* opts)
|
||||
with_writable_store(const WritableStoreFunc func, const Options& opts)
|
||||
{
|
||||
auto store{Store::make(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB),
|
||||
auto store{Store::make(opts.runtime_path(RuntimePath::XapianDb),
|
||||
Store::Options::Writable)};
|
||||
if (!store)
|
||||
return Err(store.error());
|
||||
@ -536,54 +483,53 @@ with_writable_store(const WritableStoreFunc func, const MuConfig* opts)
|
||||
}
|
||||
|
||||
Result<void>
|
||||
Mu::mu_cmd_execute(const MuConfig* opts) try {
|
||||
Mu::mu_cmd_execute(const Options& opts) try {
|
||||
|
||||
if (!opts || !opts->params || !opts->params[0])
|
||||
return Err(Error::Code::InvalidArgument, "error in parameters");
|
||||
|
||||
switch (opts->cmd) {
|
||||
case MU_CONFIG_CMD_HELP: /* already handled in mu-config.c */
|
||||
return Ok();
|
||||
if (!opts.sub_command)
|
||||
return Err(Error::Code::Internal, "missing subcommand");
|
||||
|
||||
switch (*opts.sub_command) {
|
||||
case Options::SubCommand::Help:
|
||||
return Ok(); /* already handled in mu-options.cc */
|
||||
/*
|
||||
* no store needed
|
||||
*/
|
||||
case MU_CONFIG_CMD_FIELDS:
|
||||
case Options::SubCommand::Fields:
|
||||
return mu_cmd_fields(opts);
|
||||
case MU_CONFIG_CMD_MKDIR:
|
||||
case Options::SubCommand::Mkdir:
|
||||
return cmd_mkdir(opts);
|
||||
case MU_CONFIG_CMD_SCRIPT:
|
||||
case Options::SubCommand::Script:
|
||||
return mu_cmd_script(opts);
|
||||
case MU_CONFIG_CMD_VIEW:
|
||||
case Options::SubCommand::View:
|
||||
return cmd_view(opts);
|
||||
case MU_CONFIG_CMD_VERIFY:
|
||||
case Options::SubCommand::Verify:
|
||||
return cmd_verify(opts);
|
||||
case MU_CONFIG_CMD_EXTRACT:
|
||||
case Options::SubCommand::Extract:
|
||||
return mu_cmd_extract(opts);
|
||||
/*
|
||||
* read-only store
|
||||
*/
|
||||
|
||||
case MU_CONFIG_CMD_CFIND:
|
||||
case Options::SubCommand::Cfind:
|
||||
return with_readonly_store(mu_cmd_cfind, opts);
|
||||
case MU_CONFIG_CMD_FIND:
|
||||
case Options::SubCommand::Find:
|
||||
return cmd_find(opts);
|
||||
case MU_CONFIG_CMD_INFO:
|
||||
case Options::SubCommand::Info:
|
||||
return with_readonly_store(cmd_info, opts);
|
||||
|
||||
/* writable store */
|
||||
|
||||
case MU_CONFIG_CMD_ADD:
|
||||
case Options::SubCommand::Add:
|
||||
return with_writable_store(cmd_add, opts);
|
||||
case MU_CONFIG_CMD_REMOVE:
|
||||
case Options::SubCommand::Remove:
|
||||
return with_writable_store(cmd_remove, opts);
|
||||
case MU_CONFIG_CMD_INDEX:
|
||||
case Options::SubCommand::Index:
|
||||
return with_writable_store(mu_cmd_index, opts);
|
||||
|
||||
/* commands instantiate store themselves */
|
||||
case MU_CONFIG_CMD_INIT:
|
||||
case Options::SubCommand::Init:
|
||||
return cmd_init(opts);
|
||||
case MU_CONFIG_CMD_SERVER:
|
||||
case Options::SubCommand::Server:
|
||||
return mu_cmd_server(opts);
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user