1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

Allow multiple globs on the command line to tag command.

This commit is contained in:
King_DuckZ 2016-05-18 10:26:34 +02:00
parent ad556f4be4
commit 9d76957c7f
4 changed files with 38 additions and 15 deletions

View file

@ -36,14 +36,14 @@ namespace din {
po::options_description positional_options("Positional options");
positional_options.add_options()
("tags", po::value<std::string>(), "comma-separated tag list")
("glob", po::value<std::string>(), "glob to match against")
("globs", po::value<std::vector<std::string>>(), "List of globs to match against")
;
const auto desc = dinlib::get_default_commandline();
po::options_description all("Available options");
po::positional_options_description pd;
all.add(desc).add(positional_options).add(set_options);
pd.add("tags", 1).add("glob", 1);
pd.add("tags", 1).add("globs", -1);
try {
po::store(po::command_line_parser(parArgc, parArgv).options(all).positional(pd).run(), parVarMap);
}
@ -53,7 +53,7 @@ namespace din {
po::notify(parVarMap);
const char* const help_text = "[options...] tag[,tag2...] (--ids id1[,id2...] | <glob>)";
const char* const help_text = "[options...] tag[,tag2...] (--ids id1[,id2...] | <glob> [glob...])";
if (dinlib::manage_common_commandline(std::cout, ACTION_NAME, help_text, parVarMap, {std::cref(desc), std::cref(set_options)})) {
return true;
}

View file

@ -25,8 +25,21 @@
#include <ciso646>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <vector>
#include <iterator>
namespace {
std::vector<std::string> globs_to_regex_list (const std::vector<std::string>& parGlobs) {
std::vector<std::string> retval;
retval.reserve(parGlobs.size());
std::transform(
parGlobs.begin(),
parGlobs.end(),
std::back_inserter(retval),
[](const std::string& s) { return g2r::convert(s, false); }
);
return retval;
}
} //unnamed namespace
int main (int parArgc, char* parArgv[]) {
@ -46,7 +59,7 @@ int main (int parArgc, char* parArgv[]) {
}
const bool id_mode = static_cast<bool>(vm.count("ids"));
const bool glob_mode = static_cast<bool>(vm.count("glob"));
const bool glob_mode = static_cast<bool>(vm.count("globs"));
if (not id_mode and not glob_mode) {
std::cerr << "No IDs or glob specified\n";
return 2;
@ -77,8 +90,8 @@ int main (int parArgc, char* parArgv[]) {
din::tag_files(settings.db, ids, tags);
}
else if (glob_mode) {
const auto regex = g2r::convert(vm["glob"].as<std::string>());
din::tag_files(settings.db, regex, true, tags);
const auto regexes(globs_to_regex_list(vm["globs"].as<std::vector<std::string>>()));
din::tag_files(settings.db, regexes, tags);
}
else {
assert(false);

View file

@ -31,16 +31,21 @@ namespace din {
conn.query(query, parTags, parFiles);
}
void tag_files (const dinlib::SettingsDB& parDB, std::string parRegex, bool parCaseSensitive, const std::vector<boost::string_ref>& parTags) {
void tag_files (const dinlib::SettingsDB& parDB, const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags) {
pq::Connection conn(std::string(parDB.username), std::string(parDB.password), std::string(parDB.dbname), std::string(parDB.address), parDB.port);
conn.connect();
if (not parCaseSensitive)
parRegex = "(?i)" + parRegex;
const std::string query =
"UPDATE \"files\" SET \"tags\" = ARRAY(SELECT DISTINCT UNNEST(\"tags\" || $1) ORDER BY 1) WHERE \"path\" ~ $2;";
conn.query(query, parTags, parRegex);
if (parRegexes.size() == 1) {
const std::string query = "UPDATE \"files\" SET \"tags\" = ARRAY(SELECT DISTINCT UNNEST(\"tags\" || $1) ORDER BY 1) WHERE \"path\" ~ $2;";
conn.query(query, parTags, parRegexes.front());
}
else if (parRegexes.size() > 1) {
const std::string query = "UPDATE \"files\" SET \"tags\" = ARRAY(SELECT DISTINCT UNNEST(\"tags\" || $1) ORDER BY 1) WHERE \"path\" ~ ANY($2);";
conn.query(query, parTags, parRegexes);
}
else if (parRegexes.size() == 0) {
const std::string query = "UPDATE \"files\" SET \"tags\" = ARRAY(SELECT DISTINCT UNNEST(\"tags\" || $1) ORDER BY 1);";
conn.query(query, parTags);
}
}
} //namespace din

View file

@ -27,8 +27,13 @@ namespace dinlib {
} //namespace dinlib
namespace din {
struct OwnerSetInfo {
uint32_t group_id;
bool is_valid;
};
void tag_files ( const dinlib::SettingsDB& parDB, const std::vector<uint64_t>& parFiles, const std::vector<boost::string_ref>& parTags );
void tag_files ( const dinlib::SettingsDB& parDB, std::string parRegex, bool parCaseSensitive, const std::vector<boost::string_ref>& parTags );
void tag_files ( const dinlib::SettingsDB& parDB, const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags );
} //namespace din
#endif