mu init: implement --reinit option

Create new mu database from an existing one.
This commit is contained in:
Dirk-Jan C. Binnema 2023-01-29 13:40:40 +02:00
parent cc664b8128
commit e21d59e346
3 changed files with 41 additions and 13 deletions

View File

@ -34,6 +34,13 @@ mailing list messages.
wrapped in */* (such as =/foo-.*@example\\.com/=). Depending on your shell, the
argument may need to be quoted.
** --reinit
reinitialize the database from an earlier version; that is, create a new
empty database witht the existing settings. This cannot be combined
with the other ~init~ options.
#+include: "muhome.inc" :minlevel 2
#+include: "prefooter.inc" :minlevel 1

View File

@ -410,26 +410,41 @@ cmd_info(const Mu::Store& store, const Options& opts)
static Result<void>
cmd_init(const Options& opts)
{
/* not provided, nor could we find a good default */
if (opts.init.maildir.empty())
return Err(Error::Code::InvalidArgument,
"missing --maildir parameter and could "
"not determine default");
auto store = std::invoke([&]()->Result<Store> {
Mu::Store::Config conf{};
conf.max_message_size = opts.init.max_msg_size.value_or(0);
conf.batch_size = opts.init.batch_size.value_or(0);
/*
* reinit
*/
if (opts.init.reinit)
return Store::make(opts.runtime_path(RuntimePath::XapianDb),
Store::Options::ReInit|Store::Options::Writable);
/*
* full init
*/
/* not provided, nor could we find a good default */
if (opts.init.maildir.empty())
return Err(Error::Code::InvalidArgument,
"missing --maildir parameter and could "
"not determine default");
Mu::Store::Config conf{};
conf.max_message_size = opts.init.max_msg_size.value_or(0);
conf.batch_size = opts.init.batch_size.value_or(0);
return Store::make_new(opts.runtime_path(RuntimePath::XapianDb),
opts.init.maildir, opts.init.my_addresses, 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) {
cmd_info(*store, opts);
std::cout << "\nstore created; use the 'index' command to fill/update it.\n";
std::cout << "database "
<< (opts.init.reinit ? "reinitialized" : "created")
<< "; use the 'index' command to fill/update it.\n";
}
return Ok();
}

View File

@ -364,11 +364,17 @@ sub_init(CLI::App& sub, Options& opts)
->type_name("<maildir>");
sub.add_option("--my-address", opts.init.my_addresses,
"Personal e-mail addresses")
->type_name("<addresses>");
->type_name("<address>");
sub.add_option("--max-message-size", opts.init.max_msg_size,
"Maximum allowed message size in bytes");
sub.add_option("--batch-size", opts.init.batch_size,
"Maximum size of database transaction");
sub.add_flag("--reinit", opts.init.reinit,
"Re-initialize database with current settings")
->excludes("--maildir")
->excludes("--my-address")
->excludes("--max-message-size")
->excludes("--batch-size");
}
static void