mu-cmd: Use the new Store API
This commit is contained in:
89
mu/mu-cmd.cc
89
mu/mu-cmd.cc
@ -20,6 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -27,6 +28,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
#include "mu-msg.h"
|
#include "mu-msg.h"
|
||||||
#include "mu-msg-part.h"
|
#include "mu-msg-part.h"
|
||||||
#include "mu-cmd.hh"
|
#include "mu-cmd.hh"
|
||||||
@ -547,20 +549,33 @@ cmd_verify (const MuConfig *opts, GError **err)
|
|||||||
MU_OK : MU_ERROR;
|
MU_OK : MU_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static void key_val(const Mu::MaybeAnsi& col, const std::string& key, T val)
|
||||||
|
{
|
||||||
|
using Color = Mu::MaybeAnsi::Color;
|
||||||
|
|
||||||
|
std::cout << col.fg(Color::BrightBlue)
|
||||||
|
<< std::left << std::setw(18) << key
|
||||||
|
<< col.reset() << ": ";
|
||||||
|
|
||||||
|
std::cout << col.fg(Color::Green)
|
||||||
|
<< val << col.reset() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static MuError
|
static MuError
|
||||||
cmd_info (const Mu::Store& store, const MuConfig *opts, GError **err)
|
cmd_info (const Mu::Store& store, const MuConfig *opts, GError **err)
|
||||||
{
|
{
|
||||||
const auto green{opts->nocolor ? "" : MU_COLOR_GREEN};
|
Mu::MaybeAnsi col{!opts->nocolor};
|
||||||
const auto def{opts->nocolor ? "" : MU_COLOR_DEFAULT};
|
|
||||||
|
|
||||||
std::cout << "database-path : "
|
key_val(col, "maildir", store.metadata().root_maildir);
|
||||||
<< green << store.database_path() << def << "\n"
|
key_val(col, "database-path", store.metadata().database_path);
|
||||||
<< "messages in store : "
|
key_val(col, "schema-version", store.metadata().schema_version);
|
||||||
<< green << store.size() << def << "\n"
|
key_val(col, "max-message-size", store.metadata().max_message_size);
|
||||||
<< "schema-version : "
|
key_val(col, "batch-size", store.metadata().batch_size);
|
||||||
<< green << store.schema_version() << def << "\n";
|
key_val(col, "messages in store", store.size());
|
||||||
|
|
||||||
const auto created{store.created()};
|
const auto created{store.metadata().created};
|
||||||
const auto tstamp{::localtime (&created)};
|
const auto tstamp{::localtime (&created)};
|
||||||
|
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
@ -569,23 +584,14 @@ cmd_info (const Mu::Store& store, const MuConfig *opts, GError **err)
|
|||||||
strftime (tbuf, sizeof(tbuf), "%c", tstamp);
|
strftime (tbuf, sizeof(tbuf), "%c", tstamp);
|
||||||
#pragma GCC diagnostic pop
|
#pragma GCC diagnostic pop
|
||||||
|
|
||||||
std::cout << "created : " << green << tbuf << def << "\n"
|
key_val(col, "created", tbuf);
|
||||||
<< "maildir : "
|
|
||||||
<< green << store.root_maildir() << def << "\n";
|
|
||||||
|
|
||||||
std::cout << ("personal-addresses : ");
|
const auto addrs{store.metadata().personal_addresses};
|
||||||
|
|
||||||
const auto addrs{store.personal_addresses()};
|
|
||||||
if (addrs.empty())
|
if (addrs.empty())
|
||||||
std::cout << green << "<none>" << def << "\n";
|
key_val(col, "personal-address", "<none>");
|
||||||
else {
|
else
|
||||||
bool first{true};
|
for (auto&& c: addrs)
|
||||||
for (auto&& c: addrs) {
|
key_val(col, "personal-address", c);
|
||||||
std::cout << (!first ? " " : "")
|
|
||||||
<< green << c << def << "\n";
|
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return MU_OK;
|
return MU_OK;
|
||||||
}
|
}
|
||||||
@ -601,6 +607,20 @@ cmd_init (const MuConfig *opts, GError **err)
|
|||||||
return MU_ERROR_IN_PARAMETERS;
|
return MU_ERROR_IN_PARAMETERS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (opts->max_msg_size < 0) {
|
||||||
|
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
|
||||||
|
"invalid value for max-message-size");
|
||||||
|
return MU_ERROR_IN_PARAMETERS;
|
||||||
|
} else if (opts->batch_size < 0) {
|
||||||
|
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
|
||||||
|
"invalid value for batch-size");
|
||||||
|
return MU_ERROR_IN_PARAMETERS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mu::Store::Config conf{};
|
||||||
|
conf.max_message_size = opts->max_msg_size;
|
||||||
|
conf.batch_size = opts->batch_size;
|
||||||
|
|
||||||
Mu::StringVec my_addrs;
|
Mu::StringVec my_addrs;
|
||||||
auto addrs = opts->my_addresses;
|
auto addrs = opts->my_addresses;
|
||||||
while (addrs && *addrs) {
|
while (addrs && *addrs) {
|
||||||
@ -608,29 +628,16 @@ cmd_init (const MuConfig *opts, GError **err)
|
|||||||
++addrs;
|
++addrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mu::Store store(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB), opts->maildir, my_addrs);
|
Mu::Store store(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB),
|
||||||
|
opts->maildir, my_addrs, conf);
|
||||||
if (!opts->quiet) {
|
if (!opts->quiet) {
|
||||||
cmd_info (store, opts, NULL);
|
cmd_info (store, opts, NULL);
|
||||||
g_print ("\nstore created.\n"
|
std::cout << "\nstore created; use the 'index' command to fill/update it.\n";
|
||||||
"use 'mu index' to fill the database "
|
|
||||||
"with your messages.\n"
|
|
||||||
"see mu-index(1) for details\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return MU_OK;
|
return MU_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static MuError
|
|
||||||
cmd_index (Mu::Store& store, const MuConfig *opts, GError **err)
|
|
||||||
{
|
|
||||||
const auto res = mu_cmd_index(store, opts, err);
|
|
||||||
if (res == MU_OK && !opts->quiet)
|
|
||||||
cmd_info(store, opts, err);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static MuError
|
static MuError
|
||||||
cmd_find (const MuConfig *opts, GError **err)
|
cmd_find (const MuConfig *opts, GError **err)
|
||||||
{
|
{
|
||||||
@ -722,7 +729,7 @@ mu_cmd_execute (const MuConfig *opts, GError **err) try
|
|||||||
case MU_CONFIG_CMD_TICKLE:
|
case MU_CONFIG_CMD_TICKLE:
|
||||||
merr = with_writable_store (cmd_tickle, opts, err); break;
|
merr = with_writable_store (cmd_tickle, opts, err); break;
|
||||||
case MU_CONFIG_CMD_INDEX:
|
case MU_CONFIG_CMD_INDEX:
|
||||||
merr = with_writable_store (cmd_index, opts, err); break;
|
merr = with_writable_store (mu_cmd_index, opts, err); break;
|
||||||
|
|
||||||
/* commands instantiate store themselves */
|
/* commands instantiate store themselves */
|
||||||
case MU_CONFIG_CMD_INIT:
|
case MU_CONFIG_CMD_INIT:
|
||||||
|
|||||||
Reference in New Issue
Block a user