ClickHouse/programs/disks/CommandList.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

92 lines
3.0 KiB
C++
Raw Normal View History

2022-07-20 20:30:16 +00:00
#include <Interpreters/Context.h>
2022-12-08 17:53:05 +00:00
#include <Common/TerminalSize.h>
2024-05-27 11:44:45 +00:00
#include "DisksApp.h"
#include "DisksClient.h"
#include "ICommand.h"
namespace DB
{
class CommandList final : public ICommand
{
public:
2024-05-27 11:44:45 +00:00
explicit CommandList() : ICommand()
{
command_name = "list";
2023-08-29 12:25:04 +00:00
description = "List files at path[s]";
2024-05-27 11:44:45 +00:00
options_description.add_options()("recursive", "recursively list the directory")("all", "show hidden files")(
"path", po::value<String>(), "the path of listing (mandatory, positional)");
positional_options_description.add("path", 1);
}
2024-05-27 11:44:45 +00:00
void executeImpl(const CommandLineOptions & options, DisksClient & client) override
{
2024-05-27 11:44:45 +00:00
bool recursive = options.count("recursive");
bool show_hidden = options.count("all");
auto disk = client.getCurrentDiskWithPath();
String path = getValueFromCommandLineOptionsWithDefault<String>(options, "path", ".");
if (recursive)
listRecursive(disk, path, show_hidden);
else
2024-05-27 11:44:45 +00:00
list(disk, path, show_hidden);
}
private:
2024-05-27 11:44:45 +00:00
static void list(const DiskWithPath & disk, const std::string & path, bool show_hidden)
{
2024-05-27 11:44:45 +00:00
std::vector<String> file_names = disk.listAllFilesByPath(path);
std::vector<String> selected_and_sorted_file_names{};
for (const auto & file_name : file_names)
2024-05-27 11:44:45 +00:00
if (show_hidden || (!file_name.starts_with('.')))
selected_and_sorted_file_names.push_back(file_name);
std::sort(selected_and_sorted_file_names.begin(), selected_and_sorted_file_names.end());
for (const auto & file_name : selected_and_sorted_file_names)
{
std::cout << file_name << "\n";
}
}
static void listRecursive(const DiskWithPath & disk, const std::string & relative_path, bool show_hidden)
{
std::vector<String> file_names = disk.listAllFilesByPath(relative_path);
2024-05-27 11:44:45 +00:00
std::vector<String> selected_and_sorted_file_names{};
std::cout << relative_path << ":\n";
2024-06-17 17:40:52 +00:00
for (const auto & file_name : file_names)
if (show_hidden || (!file_name.starts_with('.')))
selected_and_sorted_file_names.push_back(file_name);
2024-05-27 11:44:45 +00:00
std::sort(selected_and_sorted_file_names.begin(), selected_and_sorted_file_names.end());
for (const auto & file_name : selected_and_sorted_file_names)
{
std::cout << file_name << "\n";
}
std::cout << "\n";
2024-05-27 11:49:59 +00:00
for (const auto & file_name : selected_and_sorted_file_names)
{
auto path = [&]() -> String
{
if (relative_path.ends_with("/"))
return relative_path + file_name;
2024-09-19 11:51:02 +00:00
return relative_path + "/" + file_name;
}();
2024-05-27 11:44:45 +00:00
if (disk.isDirectory(path))
2024-06-17 17:40:52 +00:00
{
2024-05-27 11:49:59 +00:00
listRecursive(disk, path, show_hidden);
2024-06-17 17:40:52 +00:00
}
}
}
};
2024-05-27 11:44:45 +00:00
CommandPtr makeCommandList()
{
2024-05-27 11:44:45 +00:00
return std::make_shared<DB::CommandList>();
}
}