forked from vitalif/vitastor
Add glob pattern matching for ls
parent
b262938bca
commit
20d5ed799a
|
@ -85,8 +85,8 @@ void cli_tool_t::help()
|
||||||
"(c) Vitaliy Filippov, 2019+ (VNPL-1.1)\n"
|
"(c) Vitaliy Filippov, 2019+ (VNPL-1.1)\n"
|
||||||
"\n"
|
"\n"
|
||||||
"USAGE:\n"
|
"USAGE:\n"
|
||||||
"%s ls [-l] [-p POOL] [--sort FIELD] [-r] [-n N] [<name> ...]\n"
|
"%s ls [-l] [-p POOL] [--sort FIELD] [-r] [-n N] [<glob> ...]\n"
|
||||||
" List images (only specified if <name> passed).\n"
|
" List images (only matching <glob> patterns if passed).\n"
|
||||||
" -p|--pool POOL Filter images by pool ID or name\n"
|
" -p|--pool POOL Filter images by pool ID or name\n"
|
||||||
" -l|--long Also report allocated size and I/O statistics\n"
|
" -l|--long Also report allocated size and I/O statistics\n"
|
||||||
" --del Also include delete operation statistics\n"
|
" --del Also include delete operation statistics\n"
|
||||||
|
|
|
@ -16,6 +16,8 @@ std::string format_lat(uint64_t lat);
|
||||||
|
|
||||||
std::string format_q(double depth);
|
std::string format_q(double depth);
|
||||||
|
|
||||||
|
bool stupid_glob(const std::string str, const std::string glob);
|
||||||
|
|
||||||
// List existing images
|
// List existing images
|
||||||
//
|
//
|
||||||
// Again, you can just look into etcd, but this console tool incapsulates it
|
// Again, you can just look into etcd, but this console tool incapsulates it
|
||||||
|
@ -213,10 +215,21 @@ resume_1:
|
||||||
json11::Json::array list;
|
json11::Json::array list;
|
||||||
for (auto & kv: stats)
|
for (auto & kv: stats)
|
||||||
{
|
{
|
||||||
if (!only_names.size() || only_names.find(kv.second["name"].string_value()) != only_names.end())
|
if (!only_names.size())
|
||||||
{
|
{
|
||||||
list.push_back(kv.second);
|
list.push_back(kv.second);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (auto glob: only_names)
|
||||||
|
{
|
||||||
|
if (stupid_glob(kv.second["name"].string_value(), glob))
|
||||||
|
{
|
||||||
|
list.push_back(kv.second);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (sort_field == "name" || sort_field == "pool_name")
|
if (sort_field == "name" || sort_field == "pool_name")
|
||||||
{
|
{
|
||||||
|
@ -493,6 +506,62 @@ std::string format_q(double depth)
|
||||||
return std::string(buf);
|
return std::string(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct glob_stack_t
|
||||||
|
{
|
||||||
|
int glob_pos;
|
||||||
|
int str_pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Yes I know I could do it by translating the pattern to std::regex O:-)
|
||||||
|
bool stupid_glob(const std::string str, const std::string glob)
|
||||||
|
{
|
||||||
|
std::vector<glob_stack_t> wildcards;
|
||||||
|
int pos = 0, gp = 0;
|
||||||
|
bool m;
|
||||||
|
back:
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (gp >= glob.length())
|
||||||
|
{
|
||||||
|
if (pos >= str.length())
|
||||||
|
return true;
|
||||||
|
m = false;
|
||||||
|
}
|
||||||
|
else if (glob[gp] == '*')
|
||||||
|
{
|
||||||
|
wildcards.push_back((glob_stack_t){ .glob_pos = ++gp, .str_pos = pos });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (glob[gp] == '?')
|
||||||
|
m = pos < str.size();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (glob[gp] == '\\' && gp < glob.length()-1)
|
||||||
|
gp++;
|
||||||
|
m = pos < str.size() && str[pos] == glob[gp];
|
||||||
|
}
|
||||||
|
if (!m)
|
||||||
|
{
|
||||||
|
while (wildcards.size() > 0)
|
||||||
|
{
|
||||||
|
// Backtrack
|
||||||
|
pos = (++wildcards[wildcards.size()-1].str_pos);
|
||||||
|
if (pos > str.size())
|
||||||
|
wildcards.pop_back();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gp = wildcards[wildcards.size()-1].glob_pos;
|
||||||
|
goto back;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
gp++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
std::function<bool(void)> cli_tool_t::start_ls(json11::Json cfg)
|
std::function<bool(void)> cli_tool_t::start_ls(json11::Json cfg)
|
||||||
{
|
{
|
||||||
json11::Json::array cmd = cfg["command"].array_items();
|
json11::Json::array cmd = cfg["command"].array_items();
|
||||||
|
|
Loading…
Reference in New Issue